UNPKG

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

Version:

OpenStack provider for strapi upload

103 lines (102 loc) 3.98 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.OpenStackStorage = void 0; const axios_1 = __importDefault(require("axios")); const node_fs_1 = require("node:fs"); class OpenStackStorage { constructor(authUrl, objectStorageUrl, region, applicationCredential, scope) { this.endpoint = `${objectStorageUrl}/AUTH_${scope.project.id}`; this.authUrl = authUrl; this.applicationCredential = applicationCredential; this.scope = scope; } getRFC7231Date() { const now = new Date(); const dayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; const day = dayNames[now.getUTCDay()]; const date = now.getUTCDate().toString().padStart(2, '0'); const monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]; const month = monthNames[now.getUTCMonth()]; const year = now.getUTCFullYear(); const hours = now.getUTCHours().toString().padStart(2, '0'); const minutes = now.getUTCMinutes().toString().padStart(2, '0'); const seconds = now.getUTCSeconds().toString().padStart(2, '0'); return `${day}, ${date} ${month} ${year} ${hours}:${minutes}:${seconds} GMT`; } async getToken() { this.token = await this.authenticate(this.authUrl, this.applicationCredential, this.scope); return this.token; } async authenticate(authUrl, applicationCredential, scope) { try { const response = await axios_1.default.post(`${authUrl}/auth/tokens`, { auth: { identity: { methods: ['application_credential'], application_credential: applicationCredential, }, }, }); const token = response.headers['x-subject-token']; if (!token) { throw new Error('Authentication failed. No token received.'); } return token; } catch (error) { console.error('Authentication error:', error); throw error; } } getRequestConfig(extraHeaders) { return { maxBodyLength: Infinity, headers: { 'Date': this.getRFC7231Date(), 'Content-Type': 'application/octet-stream', 'X-Auth-Token': this.token, ...extraHeaders, }, }; } async uploadFile(container, filePath, fileName, extraHeaders) { const fileStream = (0, node_fs_1.createReadStream)(filePath); return this.uploadStream(container, fileStream, fileName, extraHeaders); } async uploadStream(container, stream, fileName, extraHeaders) { return new Promise(async (resolve, reject) => { if (!this.token) await this.getToken(); const uploadUrl = `${this.endpoint}/${container}/${fileName}`; const params = this.getRequestConfig(extraHeaders); try { await axios_1.default.put(uploadUrl, stream, params); resolve(uploadUrl); } catch (error) { reject(error); } }); } async deleteFile(container, fileName) { return new Promise(async (resolve, reject) => { if (!this.token) await this.getToken(); const deleteUrl = `${this.endpoint}/${container}/${fileName}`; try { await axios_1.default.delete(deleteUrl, this.getRequestConfig()); resolve(); } catch (error) { reject(error); } }); } } exports.OpenStackStorage = OpenStackStorage;