2020-07-29 02:08:38 +03:00
|
|
|
import * as MW from "~/node_common/middleware";
|
|
|
|
import * as Data from "~/node_common/data";
|
|
|
|
import * as LibraryManager from "~/node_common/managers/library";
|
|
|
|
import * as Strings from "~/common/strings";
|
2020-08-17 07:22:35 +03:00
|
|
|
import * as Upload from "~/node_common/upload";
|
2020-07-29 02:08:38 +03:00
|
|
|
|
|
|
|
const initCORS = MW.init(MW.CORS);
|
|
|
|
|
2020-08-01 03:17:07 +03:00
|
|
|
// NOTE(jim): To support multipart request.
|
2020-07-29 02:08:38 +03:00
|
|
|
export const config = {
|
|
|
|
api: {
|
|
|
|
bodyParser: false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export default async (req, res) => {
|
|
|
|
initCORS(req, res);
|
|
|
|
|
|
|
|
if (Strings.isEmpty(req.headers.authorization)) {
|
|
|
|
return res.status(404).send({
|
|
|
|
decorator: "SERVER_API_KEY_MISSING",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let slate = await Data.getSlateById({ id: req.query.id });
|
|
|
|
|
|
|
|
if (!slate) {
|
|
|
|
return res.status(404).json({
|
|
|
|
decorator: "V1_SERVER_UPLOAD_SLATE_NOT_FOUND",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (slate.error) {
|
|
|
|
return res.status(500).json({
|
|
|
|
decorator: "V1_SERVER_UPLOAD_SLATE_NOT_FOUND",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const parsed = Strings.getKey(req.headers.authorization);
|
|
|
|
const key = await Data.getAPIKeyByKey({
|
|
|
|
key: parsed,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!key) {
|
|
|
|
return res.status(403).send({
|
|
|
|
decorator: "V1_SERVER_API_KEY_NOT_FOUND",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key.error) {
|
|
|
|
return res.status(500).send({
|
|
|
|
decorator: "V1_SERVER_API_KEY_NOT_FOUND",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-01 03:17:07 +03:00
|
|
|
const user = await Data.getUserById({
|
|
|
|
id: key.owner_id,
|
|
|
|
});
|
|
|
|
|
|
|
|
const uploadResponse = await Upload.formMultipart(req, res, {
|
|
|
|
user,
|
|
|
|
});
|
2020-07-29 02:08:38 +03:00
|
|
|
|
2020-08-01 03:17:07 +03:00
|
|
|
if (!uploadResponse) {
|
2020-08-16 12:23:02 +03:00
|
|
|
return res.status(404).send({ decorator: "V1_SERVER_API_UPLOAD_ERROR", error: true });
|
2020-08-01 03:17:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (uploadResponse.error) {
|
|
|
|
// NOTE(jim): Just to debug potential textile issues with matching CIDs.
|
|
|
|
console.log({ message: uploadResponse.message });
|
|
|
|
return res.status(500).send({
|
|
|
|
decorator: uploadResponse.decorator,
|
|
|
|
error: uploadResponse.error,
|
2020-07-29 02:08:38 +03:00
|
|
|
});
|
2020-08-01 03:17:07 +03:00
|
|
|
}
|
2020-07-29 02:08:38 +03:00
|
|
|
|
2020-08-01 03:17:07 +03:00
|
|
|
const { data, ipfs } = uploadResponse;
|
2020-07-29 02:08:38 +03:00
|
|
|
|
2020-08-01 03:17:07 +03:00
|
|
|
const updatedData = LibraryManager.updateDataIPFS(data, {
|
|
|
|
ipfs,
|
|
|
|
});
|
|
|
|
|
|
|
|
const updatedUserDataFields = LibraryManager.addData({
|
|
|
|
user,
|
|
|
|
data: updatedData,
|
|
|
|
});
|
|
|
|
|
|
|
|
await Data.updateUserById({
|
|
|
|
id: user.id,
|
|
|
|
data: updatedUserDataFields,
|
|
|
|
});
|
|
|
|
|
|
|
|
slate = await Data.getSlateById({ id: req.query.id });
|
|
|
|
|
|
|
|
if (!slate) {
|
|
|
|
return res.status(404).json({
|
|
|
|
decorator: "V1_SERVER_UPLOAD_SLATE_NOT_FOUND",
|
|
|
|
error: true,
|
2020-07-29 02:08:38 +03:00
|
|
|
});
|
2020-08-01 03:17:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (slate.error) {
|
|
|
|
return res.status(500).json({
|
|
|
|
decorator: "V1_SERVER_UPLOAD_SLATE_NOT_FOUND",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
2020-07-29 02:08:38 +03:00
|
|
|
|
2020-08-01 03:17:07 +03:00
|
|
|
const url = `https://hub.textile.io${updatedData.ipfs}`;
|
|
|
|
const newSlateObjectEntity = {
|
|
|
|
id: updatedData.id,
|
|
|
|
ownerId: user.id,
|
|
|
|
name: updatedData.name,
|
|
|
|
url,
|
|
|
|
};
|
|
|
|
|
|
|
|
const updatedSlate = await Data.updateSlateById({
|
|
|
|
id: slate.id,
|
|
|
|
updated_at: new Date(),
|
|
|
|
data: {
|
|
|
|
...slate.data,
|
|
|
|
objects: [newSlateObjectEntity, ...slate.data.objects],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!updatedSlate) {
|
|
|
|
return res.status(500).json({
|
|
|
|
decorator: "V1_SERVER_UPLOAD_TO_SLATE_ERROR",
|
|
|
|
error: true,
|
2020-07-29 02:08:38 +03:00
|
|
|
});
|
2020-08-01 03:17:07 +03:00
|
|
|
}
|
2020-07-29 02:08:38 +03:00
|
|
|
|
2020-08-01 03:17:07 +03:00
|
|
|
if (updatedSlate.error) {
|
|
|
|
return res.status(500).json({
|
|
|
|
decorator: "V1_SERVER_UPLOAD_TO_SLATE_ERROR",
|
|
|
|
error: true,
|
2020-07-29 02:08:38 +03:00
|
|
|
});
|
2020-08-01 03:17:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return res.status(200).send({
|
|
|
|
decorator: "V1_UPLOAD_DATA_TO_SLATE",
|
|
|
|
data: updatedData,
|
|
|
|
slate: updatedSlate,
|
|
|
|
url,
|
2020-07-29 02:08:38 +03:00
|
|
|
});
|
|
|
|
};
|