UNPKG

@esri/solution-common

Version:

Provides general helper functions for @esri/solution.js.

154 lines 6.56 kB
"use strict"; /** @license * Copyright 2024 Esri * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.zipObjectToZipFile = exports.modifyFilesinZipObject = exports.jsonToZipFile = exports.jsonToZipObject = exports.jsonFilesToZipObject = exports.getZipObjectContents = exports.fetchZipObject = exports.blobToZipObject = void 0; const tslib_1 = require("tslib"); const jszip_1 = tslib_1.__importDefault(require("jszip")); const copyDataIntoItem_1 = require("./resources/copyDataIntoItem"); const get_blob_1 = require("./resources/get-blob"); // ------------------------------------------------------------------------------------------------------------------ // /** * Converts a blob to a zip file. * * @param blob Blob to convert * @returns Promise resolving to zip object */ async function blobToZipObject(blob) { const zipObject = new jszip_1.default(); return zipObject.loadAsync(blob); } exports.blobToZipObject = blobToZipObject; /** * Fetches a zip object. * * @param formZipFilePath Path to the zip file * @param authentication Credentials to zip file * @returns Promise resolving to zip object */ async function fetchZipObject(formZipFilePath, authentication) { return blobToZipObject(await (0, get_blob_1.getBlob)(formZipFilePath, authentication)); } exports.fetchZipObject = fetchZipObject; /** * Gets the contents of the files in the zip. * * @param zip Zip file * @param filesOfInterest Array of file names to extract from the zip file. If empty, all files are extracted. * @param blobExtensions Array of file extensions to treat as blobs; defaults to * ["png", "jpeg", "jpg", "gif", "svg", "xls", "xlsx"] * @returns Promise that resolves to an array of objects containing the file name and contents */ async function getZipObjectContents(zipObject, filesOfInterest = [], blobExtensions = ["png", "jpeg", "jpg", "gif", "svg", "xls", "xlsx"]) { const extractedZipFiles = []; const fileContentsRetrievalPromises = []; zipObject.forEach((relativePath, file) => { const getContents = async () => { if (filesOfInterest.length === 0 || filesOfInterest.includes(relativePath)) { const fileType = blobExtensions.includes(relativePath.split(".").pop()) ? "blob" : "string"; const fileContentsFetch = file.async(fileType); fileContentsRetrievalPromises.push(fileContentsFetch); extractedZipFiles.push({ file: relativePath, content: await fileContentsFetch, }); } }; void getContents(); }); await Promise.all(fileContentsRetrievalPromises); // Sort the files by name because the order of the files in the zip object is not guaranteed return extractedZipFiles.sort((a, b) => a.file.localeCompare(b.file)); } exports.getZipObjectContents = getZipObjectContents; /** * Converts a JSON object of keys (filenames)/content (stringified JSON) to a zip object. * * @param zippedFileJson JSON object to convert * @returns Created zip object */ function jsonFilesToZipObject(zippedFileJson) { const zipObject = new jszip_1.default(); Object.keys(zippedFileJson).forEach((key) => { zipObject.file(key, zippedFileJson[key]); }); return zipObject; } exports.jsonFilesToZipObject = jsonFilesToZipObject; /** * Converts a JSON object to a zip object. * * @param zippedFileName Name of the file in the zip * @param zippedFileJson JSON object to convert * @returns Created zip object */ function jsonToZipObject(zippedFileName, zippedFileJson) { const zipObject = new jszip_1.default(); zipObject.file(zippedFileName, JSON.stringify(zippedFileJson)); return zipObject; } exports.jsonToZipObject = jsonToZipObject; /** * Converts a JSON object to a zip file. * * @param zippedFileName Name of the file in the zip file * @param zippedFileJson JSON object to convert * @param filename Name to use for zip file; ".zip" added if missing * @returns Promise resolving to zip file */ async function jsonToZipFile(zippedFileName, zippedFileJson, filename) { const zipObject = jsonToZipObject(zippedFileName, zippedFileJson); return zipObjectToZipFile(zipObject, filename); } exports.jsonToZipFile = jsonToZipFile; /** * Extracts files of interest from a zip object, calls a supplied function to modify them, and * restores the files into the zip object. * * @param modificationCallback Function that modifies the specified files * @param zip Zip file that contains the files to modify; modified in place * @param filesOfInterest Array of file names to extract from the zip file. If empty, all files are extracted. * @returns Promise that resolves to the modified zip file if the swizzle was successful */ async function modifyFilesinZipObject(modificationCallback, zipObject, filesOfInterest = []) { // Get the contents of the form.json file const extractedZipFiles = await getZipObjectContents(zipObject, filesOfInterest); extractedZipFiles.forEach((extractedZipFile) => { // Run the modification callback const content = modificationCallback(extractedZipFile); // Update the zip file zipObject.file(extractedZipFile.file, content); }); return Promise.resolve(zipObject); } exports.modifyFilesinZipObject = modifyFilesinZipObject; /** * Converts a zip object to a zip file. * * @param zipObject Zip object * @param filename Name to use for zip file; ".zip" added if missing * @returns Promise resolving to zip file */ async function zipObjectToZipFile(zipObject, filename) { const completeFilename = filename.endsWith(".zip") ? filename : `${filename}.zip`; return (0, copyDataIntoItem_1.createMimeTypedFile)({ blob: await zipObject.generateAsync({ type: "blob" }), filename: completeFilename, mimeType: "application/zip", }); } exports.zipObjectToZipFile = zipObjectToZipFile; //# sourceMappingURL=zip-utils.js.map