2020-07-22 12:37:40 +03:00
|
|
|
import * as MW from "~/node_common/middleware";
|
|
|
|
import * as Constants from "~/node_common/constants";
|
|
|
|
import * as Data from "~/node_common/data";
|
|
|
|
import * as Utilities from "~/node_common/utilities";
|
2020-07-24 06:09:58 +03:00
|
|
|
import * as LibraryManager from "~/node_common/managers/library";
|
2020-07-22 12:37:40 +03:00
|
|
|
|
|
|
|
import FORM from "formidable";
|
|
|
|
import FS from "fs-extra";
|
|
|
|
|
|
|
|
const initCORS = MW.init(MW.CORS);
|
|
|
|
const initAuth = MW.init(MW.RequireCookieAuthentication);
|
|
|
|
|
|
|
|
export const config = {
|
|
|
|
api: {
|
|
|
|
bodyParser: false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export default async (req, res) => {
|
|
|
|
initCORS(req, res);
|
|
|
|
initAuth(req, res);
|
|
|
|
|
|
|
|
const f = new FORM.IncomingForm();
|
|
|
|
f.uploadDir = Constants.FILE_STORAGE_URL;
|
|
|
|
f.keepExtensions = true;
|
|
|
|
f.parse(req, async (e, fields, files) => {
|
|
|
|
if (e) {
|
2020-07-27 05:06:45 +03:00
|
|
|
return res
|
|
|
|
.status(500)
|
|
|
|
.send({ decorator: "SERVER_UPLOAD_PARSE_FAILURE", error: true });
|
2020-07-22 12:37:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!files.image) {
|
2020-07-23 13:05:13 +03:00
|
|
|
return res
|
|
|
|
.status(500)
|
2020-07-27 05:06:45 +03:00
|
|
|
.send({ decorator: "SERVER_UPLOAD_NOT_IMAGE_TYPE", error: true });
|
2020-07-22 12:37:40 +03:00
|
|
|
}
|
|
|
|
|
2020-07-24 06:09:58 +03:00
|
|
|
const path = files.image._writeStream.path;
|
|
|
|
const data = LibraryManager.createLocalDataIncomplete(files.image);
|
2020-07-22 12:37:40 +03:00
|
|
|
|
|
|
|
// TODO(jim): Send this file to buckets.
|
2020-07-22 13:51:40 +03:00
|
|
|
const id = Utilities.getIdFromCookie(req);
|
|
|
|
const user = await Data.getUserById({
|
|
|
|
id,
|
2020-07-22 12:37:40 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
const {
|
|
|
|
buckets,
|
|
|
|
bucketKey,
|
|
|
|
bucketName,
|
|
|
|
} = await Utilities.getBucketAPIFromUserToken(user.data.tokens.api);
|
|
|
|
|
2020-07-24 10:45:21 +03:00
|
|
|
let readFile;
|
|
|
|
let push;
|
|
|
|
try {
|
|
|
|
// NOTE(jim): Push pathPath to your bucket.
|
|
|
|
readFile = await FS.readFileSync(path).buffer;
|
|
|
|
push = await buckets.pushPath(bucketKey, data.name, readFile);
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
return res
|
|
|
|
.status(500)
|
|
|
|
.send({ decorator: "SERVER_BUCKETS_PUSH_ISSUE", error: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE(jim): Update your user flag.
|
|
|
|
const updated = LibraryManager.updateDataIPFS(data, {
|
2020-07-24 06:09:58 +03:00
|
|
|
ipfs: push.path.path,
|
|
|
|
});
|
2020-07-22 12:37:40 +03:00
|
|
|
|
2020-07-24 10:45:21 +03:00
|
|
|
// NOTE(jim): Update your library
|
|
|
|
const updatedUserData = LibraryManager.addData({ user, data: updated });
|
|
|
|
|
|
|
|
// NOTE(jim): Update your user
|
2020-07-22 12:37:40 +03:00
|
|
|
const response = await Data.updateUserById({
|
|
|
|
id: user.id,
|
2020-07-24 06:09:58 +03:00
|
|
|
data: updatedUserData,
|
2020-07-22 12:37:40 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// NOTE(jim): Remove the file when you're done with it.
|
2020-07-24 06:09:58 +03:00
|
|
|
await FS.unlinkSync(`./${path}`);
|
2020-07-22 12:37:40 +03:00
|
|
|
|
|
|
|
return res.status(200).send({
|
|
|
|
decorator: "SERVER_UPLOAD",
|
2020-07-24 10:45:21 +03:00
|
|
|
data: updated,
|
2020-07-22 12:37:40 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|