UNPKG

@aws-amplify/storage

Version:

Storage category of aws-amplify

50 lines (47 loc) 1.95 kB
import { Buffer } from 'buffer'; import { Md5 } from '@smithy/md5-js'; import '@aws-amplify/core/internals/aws-client-utils'; import './client/runtime/s3TransferHandler/fetch.mjs'; import 'fast-xml-parser'; import './client/runtime/s3TransferHandler/xhr.mjs'; import { toBase64 } from './client/runtime/base64/index.native.mjs'; import '@aws-amplify/core/internals/utils'; // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 // The FileReader in React Native 0.71 did not support `readAsArrayBuffer`. This native implementation accomodates this // by attempting to use `readAsArrayBuffer` and changing the file reading strategy if it throws an error. // TODO: This file should be removable when we drop support for React Native 0.71 const calculateContentMd5 = async (content) => { const hasher = new Md5(); const buffer = content instanceof Blob ? await readFile(content) : content; hasher.update(buffer); const digest = await hasher.digest(); return toBase64(digest); }; const readFile = (file) => new Promise((resolve, reject) => { const reader = new FileReader(); reader.onload = () => { resolve(reader.result); }; reader.onabort = () => { reject(new Error('Read aborted')); }; reader.onerror = () => { reject(reader.error); }; try { reader.readAsArrayBuffer(file); } catch (e) { reader.onload = () => { // reference: https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL // response from readAsDataURL is always prepended with "data:*/*;base64," const [, base64Data] = reader.result.split(','); const arrayBuffer = Buffer.from(base64Data, 'base64'); resolve(arrayBuffer); }; reader.readAsDataURL(file); } }); export { calculateContentMd5 }; //# sourceMappingURL=md5.native.mjs.map