UNPKG

@contentstack/datasync-asset-store-filesystem

Version:

Fillesystem asset store for DataSync libraries. Stores Contentstack asset-files in filesystem

155 lines (154 loc) 6.45 kB
"use strict"; /*! * contentstack-sync-asset-store-filesystem * copyright (c) Contentstack LLC * MIT Licensed */ Object.defineProperty(exports, "__esModule", { value: true }); exports.FSAssetStore = void 0; const debug_1 = require("debug"); const fs_1 = require("fs"); const lodash_1 = require("lodash"); const path_1 = require("path"); const undici_1 = require("undici"); const rimraf_1 = require("rimraf"); const index_1 = require("./index"); const utils_1 = require("./utils"); const debug = (0, debug_1.debug)('asset-store-filesystem'); /** * @class * @private * @summary Class that downloads and deletes assets from FS DB * @example * const assetStore = new FSAssetStore(config) * return assetStore.download(asset) * .then() * .catch() * @returns {FSAssetStore} */ class FSAssetStore { constructor(config) { this.config = config.assetStore; } /** * @public * @method download * @description Downloads the asset object onto local fs * @param {object} asset Asset object details * @returns {Promise} returns the asset object, if successful. */ download(asset) { debug('Asset download invoked ' + JSON.stringify(asset)); return new Promise((resolve, reject) => { try { (0, utils_1.validatePublishAsset)(asset); return (0, undici_1.request)(encodeURI(asset.url)) .then((resp) => { if (resp.statusCode === 200) { if (asset.hasOwnProperty('download_id')) { const attachment = resp.headers['content-disposition']; if (typeof attachment === 'string') { asset.filename = decodeURIComponent(attachment.split('=')[1]); } else if (Array.isArray(attachment) && attachment.length > 0) { asset.filename = decodeURIComponent(attachment[0].split('=')[1]); } } asset._internal_url = (0, index_1.getAssetLocation)(asset, this.config); const filePathArray = (0, index_1.getFileLocation)(asset, this.config); const folderPathArray = (0, lodash_1.cloneDeep)(filePathArray); folderPathArray.splice(folderPathArray.length - 1); const folderPath = (0, utils_1.sanitizePath)((0, path_1.resolve)(path_1.join.apply(this, folderPathArray))); const filePath = (0, utils_1.sanitizePath)((0, path_1.resolve)(path_1.join.apply(this, filePathArray))); if (!(0, fs_1.existsSync)(folderPath)) { (0, fs_1.mkdirSync)(folderPath, { recursive: true, mode: 0o755 }); } const localStream = (0, fs_1.createWriteStream)(filePath); localStream.on('error', (err) => { return reject(err); }); resp.body.pipe(localStream); localStream.on('close', () => { return resolve(asset); }); } else { return reject(`Failed to download asset ${JSON.stringify(asset)}`); } }) .catch(reject); } catch (error) { debug(`${asset.uid} asset download failed`); return reject(error); } }); } /** * @private * @method delete * @description Delete the asset from fs db * @param {array} assets Assets to be deleted * @returns {Promise} returns the asset object, if successful. */ delete(assets) { debug('Asset deletion called for', JSON.stringify(assets)); const asset = assets[0]; return new Promise((resolve, reject) => { try { (0, utils_1.validateUnPublishAsset)(asset); const folderPathArray = (0, index_1.getFileLocation)(asset, this.config); folderPathArray.splice(folderPathArray.length - 1, 1); const folderPath = (0, utils_1.sanitizePath)((0, path_1.resolve)(path_1.join.apply(this, folderPathArray))); if ((0, fs_1.existsSync)(folderPath)) { return Promise.resolve() .then(() => (0, rimraf_1.rimraf)(folderPath)) .then(() => resolve(asset)) .catch((error) => { debug(`Error while removing ${folderPath} asset file`); return reject(error); }); } else { debug(`${folderPath} did not exist!`); return resolve(asset); } } catch (error) { return reject(error); } }); } /** * @private * @method unpublish * @description Unpublish the asset from filesystem * @param {object} asset Asset to be unpublished * @returns {Promise} returns the asset object, if successful. */ unpublish(asset) { debug(`Asset unpublish called ${JSON.stringify(asset)}`); return new Promise((resolve, reject) => { try { (0, utils_1.validateUnPublishAsset)(asset); const filePathArray = (0, index_1.getFileLocation)(asset, this.config); const filePath = (0, utils_1.sanitizePath)((0, path_1.resolve)(path_1.join.apply(this, filePathArray))); if ((0, fs_1.existsSync)(filePath)) { return Promise.resolve() .then(() => (0, rimraf_1.rimraf)(filePath)) .then(() => resolve(asset)) .catch((error) => { debug(`Error while removing ${filePath} asset file`); return reject(error); }); } debug(`${filePath} did not exist!`); return resolve(asset); } catch (error) { return reject(error); } }); } } exports.FSAssetStore = FSAssetStore;