Changeset View
Changeset View
Standalone View
Standalone View
keyserver/src/uploads/media-utils.js
// @flow | // @flow | ||||
import bmp from '@vingle/bmp-js'; | import bmp from '@vingle/bmp-js'; | ||||
import invariant from 'invariant'; | import invariant from 'invariant'; | ||||
import sharp from 'sharp'; | import sharp from 'sharp'; | ||||
import { | import { | ||||
serverTranscodableTypes, | serverTranscodableTypes, | ||||
serverCanHandleTypes, | serverCanHandleTypes, | ||||
readableFilename, | readableFilename, | ||||
mediaConfig, | |||||
} from 'lib/media/file-utils.js'; | } from 'lib/media/file-utils.js'; | ||||
import { getImageProcessingPlan } from 'lib/media/image-utils.js'; | import { getImageProcessingPlan } from 'lib/media/image-utils.js'; | ||||
import type { Dimensions } from 'lib/types/media-types.js'; | import type { Dimensions } from 'lib/types/media-types.js'; | ||||
import { deepFileInfoFromData } from 'web/media/file-utils.js'; | import { deepFileInfoFromData } from 'web/media/file-utils.js'; | ||||
import type { UploadInput } from '../creators/upload-creator.js'; | import type { UploadInput } from '../creators/upload-creator.js'; | ||||
function initializeSharp(buffer: Buffer, mime: string) { | function initializeSharp(buffer: Buffer, mime: string) { | ||||
if (mime !== 'image/bmp') { | if (mime !== 'image/bmp') { | ||||
return sharp(buffer); | return sharp(buffer); | ||||
} | } | ||||
const bitmap = bmp.decode(buffer, true); | const bitmap = bmp.decode(buffer, true); | ||||
return sharp(bitmap.data, { | return sharp(bitmap.data, { | ||||
raw: { | raw: { | ||||
width: bitmap.width, | width: bitmap.width, | ||||
height: bitmap.height, | height: bitmap.height, | ||||
channels: 4, | channels: 4, | ||||
}, | }, | ||||
}); | }); | ||||
} | } | ||||
type ValidateAndConvertInput = { | |||||
+initialBuffer: Buffer, | |||||
+initialName: string, | |||||
+inputDimensions: ?Dimensions, | |||||
+inputLoop: boolean, | |||||
+inputEncryptionKey: ?string, | |||||
+inputMimeType: ?string, | |||||
+size: number, // in bytes | |||||
}; | |||||
async function validateAndConvert( | async function validateAndConvert( | ||||
initialBuffer: Buffer, | input: ValidateAndConvertInput, | ||||
initialName: string, | |||||
inputDimensions: ?Dimensions, | |||||
inputLoop: boolean, | |||||
size: number, // in bytes | |||||
): Promise<?UploadInput> { | ): Promise<?UploadInput> { | ||||
const { | |||||
initialBuffer, | |||||
initialName, | |||||
inputDimensions, | |||||
inputLoop, | |||||
inputEncryptionKey, | |||||
inputMimeType, | |||||
size, // in bytes | |||||
} = input; | |||||
// we don't want to transcode encrypted files | |||||
if (inputEncryptionKey) { | |||||
invariant( | |||||
inputMimeType, | |||||
'inputMimeType should be set in validateAndConvert for encrypted files', | |||||
); | |||||
invariant( | |||||
inputDimensions, | |||||
'inputDimensions should be set in validateAndConvert for encrypted files', | |||||
); | |||||
if (!serverCanHandleTypes.has(inputMimeType)) { | |||||
return null; | |||||
} | |||||
const mediaType = mediaConfig[inputMimeType]?.mediaType; | |||||
invariant( | |||||
mediaType === 'photo' || mediaType === 'video', | |||||
`mediaType for ${inputMimeType} should be photo or video`, | |||||
); | |||||
return { | |||||
name: initialName, | |||||
mime: inputMimeType, | |||||
mediaType, | |||||
buffer: initialBuffer, | |||||
dimensions: inputDimensions, | |||||
loop: inputLoop, | |||||
encryptionKey: inputEncryptionKey, | |||||
}; | |||||
} | |||||
const { mime, mediaType } = deepFileInfoFromData(initialBuffer); | const { mime, mediaType } = deepFileInfoFromData(initialBuffer); | ||||
if (!mime || !mediaType) { | if (!mime || !mediaType) { | ||||
return null; | return null; | ||||
} | } | ||||
if (!serverCanHandleTypes.has(mime)) { | if (!serverCanHandleTypes.has(mime)) { | ||||
return null; | return null; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 126 Lines • Show Last 20 Lines |