2020-07-24 02:56:50 +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";
|
|
|
|
import * as Strings from "~/node_common/utilities";
|
|
|
|
import * as Powergate from "~/node_common/powergate";
|
2020-07-24 10:45:21 +03:00
|
|
|
import * as LibraryManager from "~/node_common/managers/library";
|
2020-07-24 02:56:50 +03:00
|
|
|
|
2020-07-24 19:35:00 +03:00
|
|
|
import { ffsOptions } from "@textile/powergate-client";
|
|
|
|
|
2020-07-24 02:56:50 +03:00
|
|
|
const initCORS = MW.init(MW.CORS);
|
|
|
|
const initAuth = MW.init(MW.RequireCookieAuthentication);
|
|
|
|
|
|
|
|
export default async (req, res) => {
|
|
|
|
initCORS(req, res);
|
|
|
|
initAuth(req, res);
|
|
|
|
|
|
|
|
if (!req.body.ipfs) {
|
2020-07-24 10:45:21 +03:00
|
|
|
return res.status(500).send({ decorator: "SERVER_NO_CID", error: true });
|
2020-07-24 02:56:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const cid = req.body.ipfs.replace("/ipfs/", "");
|
|
|
|
const id = Utilities.getIdFromCookie(req);
|
|
|
|
const user = await Data.getUserById({
|
|
|
|
id,
|
|
|
|
});
|
|
|
|
|
2020-07-24 10:45:21 +03:00
|
|
|
const data = LibraryManager.getDataByIPFS(user, req.body.ipfs);
|
|
|
|
if (!data) {
|
|
|
|
return res.status(500).send({ decorator: "SERVER_NO_IPFS", error: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
const PG = Powergate.get(user);
|
2020-07-24 02:56:50 +03:00
|
|
|
let jobId;
|
|
|
|
try {
|
2020-07-24 19:35:00 +03:00
|
|
|
const Deal = await PG.ffs.pushStorageConfig(
|
|
|
|
cid,
|
|
|
|
ffsOptions.withOverride(true)
|
|
|
|
);
|
2020-07-24 02:56:50 +03:00
|
|
|
jobId = Deal && Deal.jobId ? Deal.jobId : null;
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
2020-07-24 03:25:38 +03:00
|
|
|
return res.status(500).send({
|
|
|
|
decorator: "SERVER_FILECOIN_STORAGE_DEAL_CID_ERROR",
|
|
|
|
error: true,
|
|
|
|
});
|
2020-07-24 02:56:50 +03:00
|
|
|
}
|
|
|
|
|
2020-07-24 10:45:21 +03:00
|
|
|
if (!jobId) {
|
|
|
|
return res.status(500).send({ decorator: "SERVER_NO_JOB", error: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE(jim): Update the file to be network ready.
|
|
|
|
const updated = LibraryManager.updateDataFilecoin(data, {
|
|
|
|
job: jobId,
|
|
|
|
storage: 2,
|
|
|
|
retrieval: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
// NOTE(jim): Update your library
|
|
|
|
const updatedUserData = LibraryManager.updateDataById({
|
|
|
|
user,
|
|
|
|
id: updated.id,
|
|
|
|
data: updated,
|
|
|
|
});
|
|
|
|
|
|
|
|
// NOTE(jim): Save changes
|
|
|
|
const response = await Data.updateUserById({
|
|
|
|
id: user.id,
|
|
|
|
data: updatedUserData,
|
|
|
|
});
|
|
|
|
|
2020-07-24 02:56:50 +03:00
|
|
|
return res.status(200).send({
|
|
|
|
decorator: "SERVER_FILECOIN_STORAGE_DEAL",
|
2020-07-24 10:45:21 +03:00
|
|
|
data,
|
2020-07-24 02:56:50 +03:00
|
|
|
});
|
|
|
|
};
|