UNPKG

@wgr-sa/strapi-provider-upload-open-stack

Version:

OpenStack provider for strapi upload

77 lines (76 loc) 3.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const logger_1 = require("@strapi/logger"); const streamifier_1 = require("streamifier"); const openstackStorage_1 = require("./openstackStorage"); ; console.log('init'); module.exports = { init: (config) => { const logger = (0, logger_1.createLogger)({ level: 'info' }); const { authUrl, objectStorageUrl, region, application_credential_id, application_credential_secret, project_id, container, prefix } = config; const os = new openstackStorage_1.OpenStackStorage(authUrl, objectStorageUrl, region, { id: application_credential_id, secret: application_credential_secret }, { project: { id: project_id } }); const upload = async (file, customParams = {}) => { return new Promise(async (resolve, reject) => { if (!file.stream && !file.buffer) { reject(new Error('Missing file stream or buffer')); return; } try { // Extract the necessary file information let fileName = file.ext ? `${file.hash}${file.ext}` : file.hash; fileName = prefix ? `${prefix}/${fileName}` : fileName; const extraHeaders = { "Content-Type": file.mime }; logger.info(`Uploading file "${fileName}" to container "${container}"...`); //strapi.log.info(`File "${fileName}" uploaded successfully.`); // Perform the upload using openstackStorage.uploadFile if (file.stream) { file.url = await os.uploadStream(container, file.stream, fileName, extraHeaders); } else if (file.buffer) { // If you want to support buffer uploads as well // Convert the buffer to a readable stream using `streamifier` const stream = (0, streamifier_1.createReadStream)(file.buffer); file.url = await os.uploadStream(container, stream, fileName, extraHeaders); } // Resolve the promise if the upload is successful resolve(); } catch (error) { // Reject the promise if there's an error during upload reject(error); } }); }; const deleteFile = async (file) => { return new Promise(async (resolve, reject) => { try { // Extract the necessary file information let fileName = file.ext ? `${file.hash}${file.ext}` : file.hash; fileName = prefix ? `${prefix}/${fileName}` : fileName; await os.deleteFile(container, fileName); // Resolve the promise if the upload is successful resolve(); } catch (error) { // Reject the promise if there's an error during upload reject(error); } }); }; return { async upload(file, customParams = {}) { return upload(file, customParams); }, async uploadStream(file, customParams = {}) { return upload(file, customParams); }, async delete(file) { return deleteFile(file); }, async isPrivate() { return false; }, }; }, };