Skip to content

Commit

Permalink
Move image download to image task
Browse files Browse the repository at this point in the history
  • Loading branch information
AWare committed Oct 27, 2019
1 parent e0be212 commit 58c2274
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
14 changes: 5 additions & 9 deletions projects/archiver/src/tasks/front/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { Handler } from 'aws-lambda'
import { unnest } from 'ramda'
import { attempt, hasFailed } from '../../../../backend/utils/try'
import { frontPath, Image } from '../../../common'
import { getImagesFromFront } from '../image/helpers/media'
import { frontPath } from '../../../common'
import { handleAndNotifyOnError } from '../../services/task-handler'
import { getFront } from '../../utils/backend-client'
import { Bucket, ONE_WEEK, upload } from '../../utils/s3'
import { IssueTaskOutput } from '../issue'
import { Bucket, upload, ONE_WEEK } from '../../utils/s3'
import { handleAndNotifyOnError } from '../../services/task-handler'

type FrontTaskInput = IssueTaskOutput
export interface FrontTaskOutput extends IssueTaskOutput {
images: Image[]
frontId: string
}
export const handler: Handler<
FrontTaskInput,
Expand All @@ -29,8 +27,6 @@ export const handler: Handler<

console.log(`succesfully download front ${frontId}`, maybeFront)

const images: Image[] = unnest(getImagesFromFront(maybeFront))

const frontUpload = await attempt(
upload(
frontPath(publishedId, frontId),
Expand All @@ -51,7 +47,7 @@ export const handler: Handler<
return {
issuePublication,
issue: { ...issue, fronts: publishedFronts },
images,
frontId,
fronts: remainingFronts,
remainingFronts: remainingFronts.length,
message: `Succesfully published ${frontId}`,
Expand Down
21 changes: 19 additions & 2 deletions projects/archiver/src/tasks/image/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import { Handler } from 'aws-lambda'
import { unnest } from 'ramda'
import { attempt, hasFailed } from '../../../../backend/utils/try'
import { Image, ImageSize, imageSizes } from '../../../common'
import { getAndUploadColours, getAndUploadImage } from './helpers/media'
import {
getAndUploadColours,
getAndUploadImage,
getImagesFromFront,
} from './helpers/media'
import pAll = require('p-all')
import { FrontTaskOutput } from '../front'
import { handleAndNotifyOnError } from '../../services/task-handler'
import { getFront } from '../../utils/backend-client'

type ImageTaskInput = FrontTaskOutput
export interface ImageTaskOutput extends Omit<FrontTaskOutput, 'images'> {
Expand All @@ -16,9 +21,20 @@ export const handler: Handler<
ImageTaskInput,
ImageTaskOutput
> = handleAndNotifyOnError(
async ({ issuePublication, issue, images, ...params }) => {
async ({ issuePublication, issue, frontId, ...params }) => {
const { publishedId } = issue

const maybeFront = await getFront(publishedId, frontId)

if (hasFailed(maybeFront)) {
console.error(JSON.stringify(attempt))
throw new Error(`Could not download front ${frontId}`)
}

console.log(`succesfully download front ${frontId}`, maybeFront)

const images: Image[] = unnest(getImagesFromFront(maybeFront))

const imagesWithSizes: [Image, ImageSize][] = unnest(
images.map(image =>
imageSizes.map((size): [Image, ImageSize] => [image, size]),
Expand Down Expand Up @@ -56,6 +72,7 @@ export const handler: Handler<
return {
issuePublication,
issue,
frontId,
...params,
failedColours,
failedImages,
Expand Down

0 comments on commit 58c2274

Please sign in to comment.