Skip to content

Commit

Permalink
Put image use code into the new location
Browse files Browse the repository at this point in the history
  • Loading branch information
AWare committed Oct 30, 2019
1 parent 79a2ba0 commit 53cd114
Showing 1 changed file with 34 additions and 16 deletions.
50 changes: 34 additions & 16 deletions projects/archiver/src/tasks/front/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import { Handler } from 'aws-lambda'
import { unnest } from 'ramda'
import { attempt, hasFailed } from '../../../../backend/utils/try'
import { Image, ImageSize, imageSizes, Issue, frontPath } from '../../../common'
import {
frontPath,
Image,
ImageSize,
imageSizes,
ImageUse,
Issue,
} from '../../../common'
import { handleAndNotifyOnError } from '../../services/task-handler'
import { getFront } from '../../utils/backend-client'
import { getAndUploadImage, getImagesFromFront } from './helpers/media'
import pAll = require('p-all')
import { ONE_WEEK, upload } from '../../utils/s3'
import { IssueParams } from '../issue'
import { upload, ONE_WEEK } from '../../utils/s3'
import {
getAndUploadImageUse,
getImagesFromFront,
getImageUses,
} from './helpers/media'
import pAll = require('p-all')

export interface FrontTaskInput extends IssueParams {
issue: Issue
Expand Down Expand Up @@ -46,26 +57,33 @@ export const handler: Handler<
console.log(`front uploaded`, front)

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

const imagesWithSizes: [Image, ImageSize][] = unnest(
images.map(image =>
imageSizes.map((size): [Image, ImageSize] => [image, size]),
type ImageSizeUse = [Image, ImageSize, ImageUse]
const imagesWithUses: ImageSizeUse[] = unnest(
unnest(
images.map(image =>
imageSizes.map(size =>
getImageUses(image).map(
(use): ImageSizeUse => [image, size, use],
),
),
),
),
)

const imageUploadActions = imagesWithSizes.map(
([image, size]) => async () =>
attempt(getAndUploadImage(publishedId, image, size)),
const imageUseUploadActions = imagesWithUses.map(
([image, size, use]) => async () =>
attempt(getAndUploadImageUse(publishedId, image, size, use)),
)

const imageUploads = await pAll(imageUploadActions, { concurrency: 20 })

const failedImageUploads = imageUploads.filter(hasFailed)
failedImageUploads.map(failure => console.error(JSON.stringify(failure)))
const imageUseUploads = await pAll(imageUseUploadActions, {
concurrency: 20,
})

const failedImageUseUploads = imageUseUploads.filter(hasFailed)
failedImageUseUploads.map(failure => console.error(JSON.stringify(failure)))
console.log('Uploaded images')

const failedImages = failedImageUploads.length
const failedImages = failedImageUseUploads.length
const success = failedImages === 0

return {
Expand Down

0 comments on commit 53cd114

Please sign in to comment.