Changeset View
Changeset View
Standalone View
Standalone View
web/media/media-utils.js
// @flow | // @flow | ||||
import * as React from 'react'; | |||||
import { thumbHashToDataURL } from 'thumbhash'; | |||||
import type { | import type { | ||||
MediaType, | MediaType, | ||||
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 { probeFile } from './blob-utils.js'; | import { probeFile } from './blob-utils.js'; | ||||
import { decryptThumbhashToDataURL } from './encryption-utils.js'; | |||||
import { getOrientation } from './image-utils.js'; | import { getOrientation } from './image-utils.js'; | ||||
import { base64DecodeBuffer } from '../utils/base64-utils.js'; | |||||
async function preloadImage(uri: string): Promise<{ | async function preloadImage(uri: string): Promise<{ | ||||
steps: $ReadOnlyArray<MediaMissionStep>, | steps: $ReadOnlyArray<MediaMissionStep>, | ||||
result: ?Image, | result: ?Image, | ||||
}> { | }> { | ||||
let image, exceptionMessage; | let image, exceptionMessage; | ||||
const start = Date.now(); | const start = Date.now(); | ||||
try { | try { | ||||
▲ Show 20 Lines • Show All 167 Lines • ▼ Show 20 Lines | result: { | ||||
file: fixedFile, | file: fixedFile, | ||||
mediaType, | mediaType, | ||||
uri, | uri, | ||||
dimensions, | dimensions, | ||||
}, | }, | ||||
}; | }; | ||||
} | } | ||||
export { preloadImage, validateFile }; | function usePlaceholder(thumbHash: ?string, encryptionKey: ?string): ?string { | ||||
const [placeholder, setPlaceholder] = React.useState(null); | |||||
React.useEffect(() => { | |||||
if (!thumbHash) { | |||||
setPlaceholder(null); | |||||
return; | |||||
} | |||||
if (!encryptionKey) { | |||||
const binaryThumbHash = base64DecodeBuffer(thumbHash); | |||||
const placeholderImage = thumbHashToDataURL(binaryThumbHash); | |||||
setPlaceholder(placeholderImage); | |||||
return; | |||||
} | |||||
(async () => { | |||||
try { | |||||
const decryptedThumbHash = await decryptThumbhashToDataURL( | |||||
thumbHash, | |||||
encryptionKey, | |||||
); | |||||
setPlaceholder(decryptedThumbHash); | |||||
} catch { | |||||
setPlaceholder(null); | |||||
} | |||||
})(); | |||||
}, [thumbHash, encryptionKey]); | |||||
return placeholder; | |||||
} | |||||
export { preloadImage, validateFile, usePlaceholder }; |