add save data tool
This commit is contained in:
parent
389c1c1cdc
commit
3e6935d4a0
1 changed files with 50 additions and 1 deletions
51
index.js
51
index.js
|
|
@ -183,6 +183,52 @@ async function saveFile(structureId, req, uploadedFile, asset = false) {
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function saveData(structureId, req, fileData, asset = false) {
|
||||||
|
const name = fileData.name;
|
||||||
|
let mimetype = fileData.mimetype || "application/octet-stream";
|
||||||
|
let data = fileData.data;
|
||||||
|
|
||||||
|
if (typeof data === "string") {
|
||||||
|
const dataUrl = data.match(/^data:([^;,]+);base64,([\s\S]+)$/);
|
||||||
|
if (dataUrl) {
|
||||||
|
mimetype = fileData.mimetype || dataUrl[1];
|
||||||
|
data = Buffer.from(dataUrl[2].replace(/\s/g, ""), "base64");
|
||||||
|
} else {
|
||||||
|
data = Buffer.from(data.replace(/\s/g, ""), fileData.encoding || "base64");
|
||||||
|
}
|
||||||
|
} else if (data instanceof ArrayBuffer) {
|
||||||
|
data = Buffer.from(data);
|
||||||
|
} else if (ArrayBuffer.isView(data)) {
|
||||||
|
data = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Buffer.isBuffer(data)) {
|
||||||
|
throw new Error("files.saveData requires base64, ArrayBuffer, Uint8Array, or Buffer data");
|
||||||
|
}
|
||||||
|
|
||||||
|
const [mime_type, mime_subtype] = mimetype.split("/");
|
||||||
|
const sid = String(structureId);
|
||||||
|
const uploadPath = path.join(__dirname, "public", sid);
|
||||||
|
const storedPath = path.join(sid, name);
|
||||||
|
|
||||||
|
fs.mkdirSync(uploadPath, { recursive: true });
|
||||||
|
await fs.promises.writeFile(path.join(uploadPath, name), data);
|
||||||
|
|
||||||
|
let id = model.createFile(
|
||||||
|
structureId,
|
||||||
|
name,
|
||||||
|
storedPath,
|
||||||
|
mime_type,
|
||||||
|
mime_subtype,
|
||||||
|
asset,
|
||||||
|
);
|
||||||
|
|
||||||
|
let file = model.getFile(id);
|
||||||
|
file.url = prefixUrlWithHost(req, file.path);
|
||||||
|
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
// A console whose log() mirrors to stdout and to the structure's logs table.
|
// A console whose log() mirrors to stdout and to the structure's logs table.
|
||||||
function makeConsole(structureId, routeId) {
|
function makeConsole(structureId, routeId) {
|
||||||
return {
|
return {
|
||||||
|
|
@ -238,7 +284,10 @@ function makeLibs(structureId, console, memberUserId) {
|
||||||
},
|
},
|
||||||
push: push,
|
push: push,
|
||||||
notifications: makeNotifications(structureId, memberUserId),
|
notifications: makeNotifications(structureId, memberUserId),
|
||||||
files: { saveFile: (...args) => saveFile(structureId, ...args) },
|
files: {
|
||||||
|
saveFile: (...args) => saveFile(structureId, ...args),
|
||||||
|
saveData: (...args) => saveData(structureId, ...args),
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue