2020-08-01 03:17:07 +03:00
|
|
|
import * as LibraryManager from "~/node_common/managers/library";
|
|
|
|
import * as Utilities from "~/node_common/utilities";
|
2020-09-22 03:36:45 +03:00
|
|
|
import * as Social from "~/node_common/social";
|
2020-08-01 03:17:07 +03:00
|
|
|
|
2020-08-16 12:23:02 +03:00
|
|
|
import B from "busboy";
|
2020-08-16 02:42:26 +03:00
|
|
|
|
2020-08-16 12:23:02 +03:00
|
|
|
const HIGH_WATER_MARK = 1024 * 1024 * 3;
|
2020-08-16 02:42:26 +03:00
|
|
|
|
2020-08-17 07:22:35 +03:00
|
|
|
export const formMultipart = async (req, res, { user }) => {
|
|
|
|
let data = null;
|
2020-08-16 02:42:26 +03:00
|
|
|
|
2020-08-17 07:22:35 +03:00
|
|
|
const upload = () =>
|
|
|
|
new Promise(async (resolve, reject) => {
|
|
|
|
let form = new B({
|
|
|
|
headers: req.headers,
|
|
|
|
highWaterMark: HIGH_WATER_MARK,
|
2020-08-16 02:42:26 +03:00
|
|
|
});
|
|
|
|
|
2020-09-22 03:36:45 +03:00
|
|
|
form.on("file", async function(
|
2020-09-21 00:34:31 +03:00
|
|
|
fieldname,
|
|
|
|
stream,
|
|
|
|
filename,
|
|
|
|
encoding,
|
|
|
|
mime
|
|
|
|
) {
|
2020-08-17 07:22:35 +03:00
|
|
|
data = LibraryManager.createLocalDataIncomplete({
|
2020-08-16 12:23:02 +03:00
|
|
|
name: filename,
|
2020-08-17 07:22:35 +03:00
|
|
|
type: mime,
|
|
|
|
});
|
2020-09-22 03:36:45 +03:00
|
|
|
|
2020-09-22 10:01:48 +03:00
|
|
|
const token = user.data.tokens.api;
|
|
|
|
const {
|
|
|
|
buckets,
|
|
|
|
bucketKey,
|
|
|
|
} = await Utilities.getBucketAPIFromUserToken(token, user);
|
|
|
|
|
|
|
|
if (!buckets) {
|
|
|
|
return reject({
|
|
|
|
decorator: "SERVER_BUCKET_INIT_FAILURE",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-17 07:22:35 +03:00
|
|
|
let push;
|
|
|
|
try {
|
2020-09-22 10:01:48 +03:00
|
|
|
console.log("[upload] pushing to textile");
|
2020-08-17 07:22:35 +03:00
|
|
|
push = await buckets.pushPath(bucketKey, data.id, stream);
|
2020-09-22 10:01:48 +03:00
|
|
|
console.log("[upload] finished pushing to textile");
|
2020-08-17 07:22:35 +03:00
|
|
|
} catch (e) {
|
2020-09-22 03:36:45 +03:00
|
|
|
Social.sendTextileSlackMessage({
|
|
|
|
file: "/node_common/upload.js",
|
|
|
|
user,
|
|
|
|
message: e.message,
|
|
|
|
code: e.code,
|
|
|
|
functionName: `buckets.pushPath`,
|
|
|
|
});
|
|
|
|
|
2020-08-17 07:22:35 +03:00
|
|
|
return reject({
|
|
|
|
decorator: "SERVER_BUCKETS_PUSH_ISSUE",
|
|
|
|
error: true,
|
|
|
|
message: e,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-21 00:34:31 +03:00
|
|
|
return resolve({
|
|
|
|
decorator: "SERVER_BUCKET_STREAM_SUCCESS",
|
|
|
|
data: push.path.path,
|
|
|
|
});
|
2020-08-16 12:23:02 +03:00
|
|
|
});
|
2020-08-04 04:35:31 +03:00
|
|
|
|
2020-08-17 07:22:35 +03:00
|
|
|
form.on("error", (e) => {
|
2020-09-22 05:31:17 +03:00
|
|
|
Social.sendTextileSlackMessage({
|
|
|
|
file: "/node_common/upload.js",
|
|
|
|
user,
|
|
|
|
message: e.message,
|
|
|
|
code: e.code,
|
|
|
|
functionName: `form`,
|
|
|
|
});
|
|
|
|
|
2020-08-01 03:17:07 +03:00
|
|
|
return reject({
|
2020-08-17 07:22:35 +03:00
|
|
|
decorator: "SERVER_BUCKET_STREAM_FAILURE",
|
2020-08-01 03:17:07 +03:00
|
|
|
error: true,
|
|
|
|
message: e,
|
|
|
|
});
|
2020-08-17 07:22:35 +03:00
|
|
|
});
|
2020-08-01 03:17:07 +03:00
|
|
|
|
2020-08-17 07:22:35 +03:00
|
|
|
req.pipe(form);
|
2020-08-01 03:17:07 +03:00
|
|
|
});
|
2020-08-16 12:23:02 +03:00
|
|
|
|
2020-08-17 07:22:35 +03:00
|
|
|
const response = await upload();
|
|
|
|
|
2020-09-22 05:31:17 +03:00
|
|
|
if (response && response.error) {
|
2020-08-17 07:22:35 +03:00
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2020-09-22 03:36:45 +03:00
|
|
|
// TODO(jim): Put this call into a file for all Textile related calls.
|
2020-09-22 10:01:48 +03:00
|
|
|
const token = user.data.tokens.api;
|
|
|
|
const { buckets } = await Utilities.getBucketAPIFromUserToken(token, user);
|
|
|
|
|
|
|
|
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) {
|
2020-09-22 03:36:45 +03:00
|
|
|
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 {
|
|
|
|
decorator: "SERVER_BUCKETS_VERIFY_ISSUE",
|
|
|
|
error: true,
|
|
|
|
message: e,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return { decorator: "SERVER_UPLOAD_SUCCESS", data, ipfs: response.data };
|
|
|
|
};
|