UNPKG

@filerobot-strapi/content-plugin

Version:

Scaleflex DAM normalizes, resizes, optimizes and distributes your images rocket fast around the world.

340 lines (339 loc) 9.18 kB
"use strict"; const path = require("path"); const fs = require("fs"); const _interopDefault = (e) => e && e.__esModule ? e : { default: e }; const path__default = /* @__PURE__ */ _interopDefault(path); const fs__default = /* @__PURE__ */ _interopDefault(fs); const bootstrap = ({ strapi }) => { }; const destroy = ({ strapi }) => { }; const register = ({ strapi }) => { }; const config = { default: {}, validator() { } }; const contentTypes = {}; const controller = ({ strapi }) => ({ async index(ctx) { ctx.body = strapi.plugin("scaleflex-dam").service("service").getWelcomeMessage(); }, async getConfig(ctx) { ctx.body = await strapi.plugin("scaleflex-dam").service("service").getConfig(); }, async updateConfig(ctx) { ctx.body = await strapi.plugin("scaleflex-dam").service("service").updateConfig(ctx); }, async checkDbFiles(ctx) { ctx.body = await strapi.plugin("scaleflex-dam").service("service").checkDbFiles(ctx); }, async recordFile(ctx) { ctx.body = await strapi.plugin("scaleflex-dam").service("service").recordFile(ctx); }, async syncUp(ctx) { ctx.body = await strapi.plugin("scaleflex-dam").service("service").syncUp(ctx); }, async getMedia(ctx) { ctx.body = await strapi.plugin("scaleflex-dam").service("service").getMedia(ctx); }, async getMediaCount(ctx) { ctx.body = await strapi.plugin("scaleflex-dam").service("service").getMediaCount(ctx); } }); const controllers = { controller }; const middlewares = {}; const policies = {}; const routes = [ { method: "GET", path: "/", // name of the controller file & the method. handler: "controller.index", config: { policies: [], auth: false } }, { method: "GET", path: "/config", handler: "controller.getConfig", config: { policies: [], auth: false } }, { method: "PUT", path: "/update-config", handler: "controller.updateConfig", config: { policies: [], auth: false } }, { method: "GET", path: "/db-files", handler: "controller.checkDbFiles", config: { policies: [], auth: false } }, { method: "POST", path: "/record-file", handler: "controller.recordFile", config: { policies: [], auth: false } }, { method: "POST", path: "/sync-up", handler: "controller.syncUp", config: { policies: [], auth: false } }, { method: "GET", path: "/media", handler: "controller.getMedia", config: { policies: [], auth: false } }, { method: "GET", path: "/media-count", handler: "controller.getMediaCount", config: { policies: [], auth: false } } ]; const filerobotApiDomain = "https://api.filerobot.com"; const service = ({ strapi }) => ({ getWelcomeMessage() { return "Thank you for using Scaleflex DAM"; }, getPluginStore() { return strapi.store({ environment: strapi.config.environment, type: "plugin", name: "scaleflex-dam" }); }, async getConfig() { const pluginStore = this.getPluginStore(); let config2 = { cname: "", token: "", sec_temp: "", folder: "" }; const storedConfig = await pluginStore.get({ key: "options" }); if (storedConfig) { config2 = storedConfig; } return config2; }, async updateConfig(ctx) { const pluginStore = this.getPluginStore(); await pluginStore.set({ key: "options", value: ctx.request.body }); return await pluginStore.get({ key: "options" }); }, async checkDbFiles() { const nonFilerobotMedia = await strapi.entityService.findMany("plugin::upload.file", { filters: { $not: { provider: "filerobot" } } }); const filerobotMedia = await strapi.entityService.findMany("plugin::upload.file", { filters: { provider: "filerobot" } }); const media = { filerobot: filerobotMedia, nonFilerobot: nonFilerobotMedia }; return media; }, async recordFile(ctx) { const file = ctx.request.body.file; const action = ctx.request.body.action; const config2 = ctx.request.body.config; let url = action === "export" ? file.link : file.url.cdn; const name = action === "export" ? file.file.name : file.name; const alt = action === "export" ? file.file.uuid : file.uuid; const ext = action === "export" ? file.file.extension : file.extension; const mime = action === "export" ? file.file.type : file.type; const size = parseFloat(action === "export" ? file.file.size.pretty : file.size.pretty); const hash = action === "export" ? file.file.hash.sha1 : file.hash.sha1; const width = action === "export" ? file.file.info.img_w : file.info.img_w; const height = action === "export" ? file.file.info.img_h : file.info.img_h; url = this.removeQueryParam(url, "vh"); url = this.adjustForCname(url, config2); const admins = await strapi.entityService.findMany("admin::user"); const admin1 = admins[0]; const result = await strapi.entityService.create("plugin::upload.file", { data: { url, name, caption: name, alternativeText: alt, provider: "filerobot", ext: `.${ext}`, mime, size, hash, width, height, created_by_id: admin1.id, updated_by_id: admin1.id, folderPath: "/" } }); return result; }, async syncUp(ctx) { const file = ctx.request.body.file; const config2 = ctx.request.body.config; const imagePath = strapi.dirs.public ? path__default.default.join(strapi.dirs.public, file.url) : path__default.default.join(strapi.dirs.static.public, file.url); let base64 = ""; try { base64 = fs__default.default.readFileSync(imagePath, { encoding: "base64" }); } catch (err) { return false; } const pluginStore = this.getPluginStore(); const pluginConfig = await pluginStore.get({ key: "options" }); const sass = await this.getSass(pluginConfig); if (!sass) { return false; } const uploadHeaders = { "Content-Type": "application/json", "X-Filerobot-Key": sass }; const raw = JSON.stringify({ "name": file.name, "data": base64, "postactions": "decode_base64" }); const uploadRequestOptions = { method: "POST", headers: uploadHeaders, body: raw }; const uploadRes = await fetch(`${filerobotApiDomain}/${config2.token}/v4/files?folder=${config2.folder}`, uploadRequestOptions); if (uploadRes.status !== 200) { return false; } const uploadResult = await uploadRes.json(); if (uploadResult.status !== "success") { return false; } let url = uploadResult.file.url.cdn; url = this.removeQueryParam(url, "vh"); url = this.adjustForCname(url, config2); const updatedFileEntry = await strapi.entityService.update("plugin::upload.file", file.id, { data: { url, hash: uploadResult.file.hash.sha1, provider: "filerobot", alternativeText: uploadResult.file.uuid, formats: null } }); return updatedFileEntry; }, async getMedia(ctx) { const queryParams = ctx.request.query; const media = await strapi.entityService.findMany("plugin::upload.file", { limit: queryParams.limit, start: queryParams.offset * queryParams.limit }); return media; }, async getMediaCount(ctx) { const media = await strapi.entityService.count("plugin::upload.file"); return media; }, async getNewSassKey(config2) { const sassReqHeaders = { "Content-Type": "application/json" }; const sassReqOpt = { method: "GET", headers: sassReqHeaders }; const sassRes = await fetch(`${filerobotApiDomain}/${config2.token}/key/${config2.sec_temp}`, sassReqOpt); if (sassRes.status != 200) { return false; } const sassInfo = await sassRes.json(); if (sassInfo.status !== "success") { return false; } const sass = sassInfo.key; if (typeof Storage !== "undefined") { sessionStorage.setItem("sassKey", sass); } return sass; }, async getSass(config2) { let sass = ""; sass = await this.getNewSassKey(config2); if (sass === false) { return false; } return sass; }, removeQueryParam(link, paramName) { const url = new URL(link); const params = new URLSearchParams(url.search); params.delete(paramName); const newUrl = params.toString() ? `${url.origin}${url.pathname}?${params.toString()}` : `${url.origin}${url.pathname}`; return newUrl; }, adjustForCname(link, config2) { if (!config2.cname) { return link; } link = link.replace(`https://${config2.token}.filerobot.com/v7`, `https://${config2.cname}`); return link; } }); const services = { service }; const index = { bootstrap, destroy, register, config, controllers, contentTypes, middlewares, policies, routes, services }; module.exports = index;