UNPKG

@aws-amplify/storage

Version:

Storage category of aws-amplify

89 lines (87 loc) 2.96 kB
'use strict'; // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 Object.defineProperty(exports, "__esModule", { value: true }); exports.loadOrCreateMultipartUpload = void 0; const uploadCache_1 = require("./uploadCache"); const client_1 = require("../../../utils/client"); const utils_1 = require("../../../../../utils"); /** * Load the in-progress multipart upload from local storage or async storage(RN) if it exists, or create a new multipart * upload. * * @internal */ const loadOrCreateMultipartUpload = async ({ s3Config, data, size, contentType, bucket, accessLevel, keyPrefix, key, contentDisposition, contentEncoding, metadata, abortSignal, }) => { const finalKey = keyPrefix + key; let cachedUpload; if (size === undefined) { utils_1.logger.debug('uploaded data size cannot be determined, skipping cache.'); cachedUpload = undefined; } else { const uploadCacheKey = (0, uploadCache_1.getUploadsCacheKey)({ size, contentType, file: data instanceof File ? data : undefined, bucket, accessLevel, key, }); const cachedUploadParts = await (0, uploadCache_1.findCachedUploadParts)({ s3Config, cacheKey: uploadCacheKey, bucket, finalKey, }); cachedUpload = cachedUploadParts ? { ...cachedUploadParts, uploadCacheKey } : undefined; } if (cachedUpload) { return { uploadId: cachedUpload.uploadId, cachedParts: cachedUpload.parts, }; } else { const { UploadId } = await (0, client_1.createMultipartUpload)({ ...s3Config, abortSignal, }, { Bucket: bucket, Key: finalKey, ContentType: contentType, ContentDisposition: contentDisposition, ContentEncoding: contentEncoding, Metadata: metadata, }); if (size === undefined) { utils_1.logger.debug('uploaded data size cannot be determined, skipping cache.'); return { uploadId: UploadId, cachedParts: [], }; } const uploadCacheKey = (0, uploadCache_1.getUploadsCacheKey)({ size, contentType, file: data instanceof File ? data : undefined, bucket, accessLevel, key, }); await (0, uploadCache_1.cacheMultipartUpload)(uploadCacheKey, { uploadId: UploadId, bucket, key, fileName: data instanceof File ? data.name : '', }); return { uploadId: UploadId, cachedParts: [], }; } }; exports.loadOrCreateMultipartUpload = loadOrCreateMultipartUpload; //# sourceMappingURL=initialUpload.js.map