slate/node_common/upload.js

143 lines
3.3 KiB
JavaScript
Raw Normal View History

import * as LibraryManager from "~/node_common/managers/library";
import * as Utilities from "~/node_common/utilities";
import * as Social from "~/node_common/social";
2020-10-09 03:40:26 +03:00
import AbortController from "abort-controller";
import B from "busboy";
const HIGH_WATER_MARK = 1024 * 1024 * 3;
2020-09-23 14:17:56 +03:00
export const formMultipart = async (req, res, { user, bucketName }) => {
2020-08-17 07:22:35 +03:00
let data = null;
2020-10-09 03:40:26 +03:00
const controller = new AbortController();
const { signal } = controller;
2020-08-17 07:22:35 +03:00
const upload = () =>
new Promise(async (resolve, reject) => {
2020-10-09 05:32:30 +03:00
signal.onabort = () => {
req.unpipe();
return reject({
decorator: "SERVER_SIGNAL_ABORT",
error: true,
message:
"We triggered an abort from a Textile writable stream failure.",
});
};
2020-08-17 07:22:35 +03:00
let form = new B({
headers: req.headers,
highWaterMark: HIGH_WATER_MARK,
2020-10-05 07:35:42 +03:00
fileHwm: HIGH_WATER_MARK,
});
form.on("file", async function(
fieldname,
stream,
filename,
encoding,
mime
) {
2020-08-17 07:22:35 +03:00
data = LibraryManager.createLocalDataIncomplete({
name: filename,
2020-08-17 07:22:35 +03:00
type: mime,
});
const {
buckets,
bucketKey,
2020-10-09 03:40:26 +03:00
bucketRoot,
2020-09-23 14:17:56 +03:00
} = await Utilities.getBucketAPIFromUserToken({
user,
bucketName,
});
if (!buckets) {
return reject({
decorator: "SERVER_BUCKET_INIT_FAILURE",
error: true,
});
}
2020-08-17 07:22:35 +03:00
let push;
try {
2020-10-09 05:32:30 +03:00
console.log("[upload] PUSHING FILE");
2020-10-09 03:40:26 +03:00
push = await buckets.pushPath(bucketKey, data.id, stream, {
root: bucketRoot,
signal,
});
2020-10-09 05:32:30 +03:00
console.log("[upload] SUCCESSFUL PUSH");
2020-08-17 07:22:35 +03:00
} catch (e) {
Social.sendTextileSlackMessage({
file: "/node_common/upload.js",
user,
message: e.message,
code: e.code,
2020-10-09 03:40:26 +03:00
functionName: `buckets.pushPath (aborting)`,
});
2020-10-09 05:32:30 +03:00
return controller.abort();
2020-08-17 07:22:35 +03:00
}
return resolve({
decorator: "SERVER_BUCKET_STREAM_SUCCESS",
data: push.path.path,
});
});
2020-08-17 07:22:35 +03:00
form.on("error", (e) => {
Social.sendTextileSlackMessage({
file: "/node_common/upload.js",
user,
message: e.message,
code: e.code,
2020-10-09 03:40:26 +03:00
functionName: `form (aborting)`,
});
2020-10-09 03:40:26 +03:00
controller.abort();
2020-08-17 07:22:35 +03:00
});
2020-08-17 07:22:35 +03:00
req.pipe(form);
});
2020-08-17 07:22:35 +03:00
const response = await upload();
2020-10-09 05:32:30 +03:00
console.log("[ upload ]", response);
if (response && response.error) {
2020-10-09 05:32:30 +03:00
console.log("[ upload ] ending due to errors.");
2020-08-17 07:22:35 +03:00
return response;
}
2020-09-23 14:17:56 +03:00
const { buckets } = await Utilities.getBucketAPIFromUserToken({
user,
bucketName,
});
if (!buckets) {
return {
decorator: "SERVER_BUCKET_INIT_FAILURE",
error: true,
};
}
2020-08-17 07:22:35 +03:00
try {
const newUpload = await buckets.listIpfsPath(response.data);
data.size = newUpload.size;
} catch (e) {
Social.sendTextileSlackMessage({
file: "/node_common/upload.js",
user,
message: e.message,
code: e.code,
functionName: `buckets.listIpfsPath`,
});
2020-08-17 07:22:35 +03:00
return {
2020-10-07 13:02:08 +03:00
decorator: "SERVER_UPLOAD_ERROR",
2020-08-17 07:22:35 +03:00
error: true,
2020-10-07 13:02:08 +03:00
message: e.message,
2020-08-17 07:22:35 +03:00
};
}
return { decorator: "SERVER_UPLOAD_SUCCESS", data, ipfs: response.data };
};