@acf-int/strapi-plugin-editorjs
Version:
Plugin for Strapi Headless CMS, hiding the standard WYSIWYG editor and replacing it with Editor.js
130 lines (111 loc) • 3.16 kB
JavaScript
const fs = require("fs");
const path = require("path");
const ogs = require("open-graph-scraper");
const { parseMultipartData } = require("@strapi/utils");
const axios = require("axios");
const { LocalFileData } = require("get-file-object-from-local-path");
const pluginId = require("../../pluginId");
module.exports = ({ strapi }) => ({
link: async (ctx) => {
const result = await new Promise((resolve) => {
ogs(ctx.query, (error, results, response) => {
const imageUrl = results.ogImage && results.ogImage.url ? { url: results.ogImage.url } : undefined;
resolve({
success: 1,
meta: {
title: results.ogTitle,
description: results.ogDescription,
image: imageUrl,
},
});
});
});
ctx.send(result);
},
byFile: async (ctx) => {
try {
const { files } = parseMultipartData(ctx);
const [uploadedFile] = await strapi
.plugin("upload")
.service("upload")
.upload({
data: {},
files: Object.values(files),
});
ctx.send({
success: 1,
file: uploadedFile,
});
} catch (e) {
ctx.send(
{
success: 0,
message: e.message,
},
500
);
}
},
settings: async (ctx) => {
const { config } = strapi.plugin(pluginId);
ctx.send({
quoteWithPicture: config("quoteWithPicture"),
gallery: config("gallery"),
tooltip: config("tooltip"),
numbers: config("numbers"),
collapse: config("collapse"),
image: config("image"),
typograf: config("typograf"),
highlight: config("highlight"),
image: config("image"),
embed: config("embed"),
table: config("table"),
list: config("list"),
warning: config("warning"),
code: config("code"),
LinkTool: config("LinkTool"),
raw: config("raw"),
header: config("header"),
quote: config("quote"),
checklist: config("checklist"),
delimiter: config("delimiter"),
marker: config("marker"),
inlineCode: config("inlineCode"),
attach: config("attach"),
});
},
byURL: async (ctx) => {
try {
const { url } = ctx.request.body;
const { name, ext } = path.parse(url);
const filePath = `./public/${name}${ext}`;
const response = await axios.get(url, { responseType: "arraybuffer" });
const buffer = Buffer.from(response.data, "binary");
await fs.promises.writeFile(filePath, buffer);
const fileData = new LocalFileData(filePath);
const file = {
path: filePath,
name: fileData.name,
type: fileData.type,
size: Buffer.byteLength(buffer),
};
const [uploadedFile] = await strapi.plugin("upload").service("upload").upload({
data: {},
files: file,
});
await fs.promises.unlink(filePath);
ctx.send({
success: 1,
file: uploadedFile,
});
} catch (e) {
ctx.send(
{
success: 0,
message: e.message,
},
500
);
}
},
});