Changeset View
Changeset View
Standalone View
Standalone View
native/media/image-utils.js
// @flow | // @flow | ||||
import * as ImageManipulator from 'expo-image-manipulator'; | import * as ImageManipulator from 'expo-image-manipulator'; | ||||
import { getImageProcessingPlan } from 'lib/media/image-utils.js'; | import { getImageProcessingPlan } from 'lib/media/image-utils.js'; | ||||
import type { | import type { | ||||
Dimensions, | Dimensions, | ||||
MediaMissionStep, | MediaMissionStep, | ||||
MediaMissionFailure, | MediaMissionFailure, | ||||
} from 'lib/types/media-types.js'; | } from 'lib/types/media-types.js'; | ||||
import { getMessageForException } from 'lib/utils/errors.js'; | import { getMessageForException } from 'lib/utils/errors.js'; | ||||
import { generateThumbhashStep } from './media-utils.js'; | |||||
type ProcessImageInfo = { | type ProcessImageInfo = { | ||||
uri: string, | uri: string, | ||||
dimensions: Dimensions, | dimensions: Dimensions, | ||||
mime: string, | mime: string, | ||||
fileSize: number, | fileSize: number, | ||||
orientation: ?number, | orientation: ?number, | ||||
}; | }; | ||||
type ProcessImageResponse = { | type ProcessImageResponse = { | ||||
success: true, | success: true, | ||||
uri: string, | uri: string, | ||||
mime: string, | mime: string, | ||||
dimensions: Dimensions, | dimensions: Dimensions, | ||||
thumbHash: ?string, | |||||
}; | }; | ||||
async function processImage(input: ProcessImageInfo): Promise<{ | async function processImage(input: ProcessImageInfo): Promise<{ | ||||
steps: $ReadOnlyArray<MediaMissionStep>, | steps: $ReadOnlyArray<MediaMissionStep>, | ||||
result: MediaMissionFailure | ProcessImageResponse, | result: MediaMissionFailure | ProcessImageResponse, | ||||
}> { | }> { | ||||
const steps = []; | const steps = []; | ||||
let { uri, dimensions, mime } = input; | let { uri, dimensions, mime } = input; | ||||
const { fileSize, orientation } = input; | const { fileSize, orientation } = input; | ||||
const plan = getImageProcessingPlan({ | const plan = getImageProcessingPlan({ | ||||
inputMIME: mime, | inputMIME: mime, | ||||
inputDimensions: dimensions, | inputDimensions: dimensions, | ||||
inputFileSize: fileSize, | inputFileSize: fileSize, | ||||
inputOrientation: orientation, | inputOrientation: orientation, | ||||
}); | }); | ||||
if (plan.action === 'none') { | if (plan.action === 'none') { | ||||
const thumbhashStep = await generateThumbhashStep(uri); | |||||
steps.push(thumbhashStep); | |||||
const { thumbHash } = thumbhashStep; | |||||
return { | return { | ||||
steps, | steps, | ||||
result: { success: true, uri, dimensions, mime }, | result: { success: true, uri, dimensions, mime, thumbHash }, | ||||
}; | }; | ||||
} | } | ||||
const { targetMIME, compressionRatio, fitInside } = plan; | const { targetMIME, compressionRatio, fitInside } = plan; | ||||
const transforms = []; | const transforms = []; | ||||
if (fitInside) { | if (fitInside) { | ||||
const fitInsideRatio = fitInside.width / fitInside.height; | const fitInsideRatio = fitInside.width / fitInside.height; | ||||
if (dimensions.width / dimensions.height > fitInsideRatio) { | if (dimensions.width / dimensions.height > fitInsideRatio) { | ||||
▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | return { | ||||
result: { | result: { | ||||
success: false, | success: false, | ||||
reason: 'photo_manipulation_failed', | reason: 'photo_manipulation_failed', | ||||
size: fileSize, | size: fileSize, | ||||
}, | }, | ||||
}; | }; | ||||
} | } | ||||
return { steps, result: { success: true, uri, dimensions, mime } }; | const thumbhashStep = await generateThumbhashStep(uri); | ||||
steps.push(thumbhashStep); | |||||
const { thumbHash } = thumbhashStep; | |||||
return { | |||||
steps, | |||||
result: { success: true, uri, dimensions, mime, thumbHash }, | |||||
}; | |||||
} | } | ||||
export { processImage }; | export { processImage }; |