2020-07-24 10:45:21 +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";
|
|
|
|
import * as LibraryManager from "~/node_common/managers/library";
|
|
|
|
|
|
|
|
const initCORS = MW.init(MW.CORS);
|
|
|
|
const initAuth = MW.init(MW.RequireCookieAuthentication);
|
|
|
|
|
|
|
|
const check = async (PG, jobId) =>
|
|
|
|
new Promise((resolve) => {
|
|
|
|
const jobsCancel = PG.ffs.watchJobs((j) => {
|
|
|
|
resolve(j);
|
|
|
|
}, jobId);
|
|
|
|
});
|
|
|
|
|
|
|
|
export default async (req, res) => {
|
|
|
|
initCORS(req, res);
|
|
|
|
initAuth(req, res);
|
|
|
|
|
|
|
|
if (!req.body.data) {
|
|
|
|
return res
|
|
|
|
.status(500)
|
|
|
|
.send({ decorator: "SERVER_NO_CIDS_TO_CHECK", error: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
const id = Utilities.getIdFromCookie(req);
|
|
|
|
const user = await Data.getUserById({
|
|
|
|
id,
|
|
|
|
});
|
|
|
|
const PG = Powergate.get(user);
|
|
|
|
|
2020-08-01 05:04:44 +03:00
|
|
|
const success = [];
|
2020-07-24 10:45:21 +03:00
|
|
|
const failed = [];
|
|
|
|
try {
|
|
|
|
for (let i = 0; i < req.body.data.length; i++) {
|
|
|
|
const x = req.body.data[i];
|
|
|
|
|
|
|
|
if (!x.job) {
|
2020-08-01 05:04:44 +03:00
|
|
|
console.log("-- NO JOB (3)");
|
2020-07-24 10:45:21 +03:00
|
|
|
failed.push(x);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-08-01 05:04:44 +03:00
|
|
|
if (!x.ipfs) {
|
|
|
|
console.log("-- NO IPFS (3)");
|
|
|
|
failed.push(x);
|
|
|
|
}
|
|
|
|
|
2020-07-24 10:45:21 +03:00
|
|
|
// TODO(jim): A hack, I just want the first job object from a stream.
|
|
|
|
const job = await check(PG, x.job);
|
2020-08-01 05:04:44 +03:00
|
|
|
console.log({ job });
|
2020-07-24 10:45:21 +03:00
|
|
|
|
|
|
|
// TODO(jim): This isn't correct.
|
|
|
|
// There is a bug where everything is going to hot storage.
|
|
|
|
// Which is fine... But its not real Filecoin storage then.
|
2020-08-01 05:04:44 +03:00
|
|
|
if (job.status === 3) {
|
|
|
|
console.log("CID ERROR (3): ", job.cid);
|
|
|
|
x.error = job.errCause;
|
|
|
|
failed.push(x);
|
2020-07-24 10:45:21 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-08-01 05:04:44 +03:00
|
|
|
if (job.status === 5) {
|
|
|
|
console.log("CID SUCCESS (5): ", job.cid);
|
|
|
|
success.push(x);
|
2020-07-24 10:45:21 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-08-01 05:04:44 +03:00
|
|
|
|
|
|
|
console.log("CID: ", job.cid);
|
2020-07-24 10:45:21 +03:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
|
2020-08-01 05:04:44 +03:00
|
|
|
let userDataFields = { ...user.data };
|
2020-07-24 10:45:21 +03:00
|
|
|
if (failed.length) {
|
|
|
|
for (let i = 0; i < failed.length; i++) {
|
2020-08-01 05:04:44 +03:00
|
|
|
let data = LibraryManager.getDataByIPFS(
|
|
|
|
{ ...user, data: userDataFields },
|
|
|
|
failed[i].ipfs
|
|
|
|
);
|
|
|
|
|
2020-07-24 10:45:21 +03:00
|
|
|
data.networks = data.networks.filter((each) => each !== "FILECOIN");
|
|
|
|
data.job = null;
|
|
|
|
data.storage = 0;
|
2020-08-01 05:04:44 +03:00
|
|
|
data.error = failed[i].error;
|
|
|
|
|
|
|
|
userDataFields = LibraryManager.updateDataById({
|
|
|
|
user: { ...user, data: userDataFields },
|
|
|
|
id: data.id,
|
|
|
|
data,
|
|
|
|
});
|
2020-07-24 10:45:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-01 05:04:44 +03:00
|
|
|
if (success.length) {
|
|
|
|
for (let i = 0; i < success.length; i++) {
|
|
|
|
let data = LibraryManager.getDataByIPFS(
|
|
|
|
{ ...user, data: userDataFields },
|
|
|
|
success[i].ipfs
|
|
|
|
);
|
|
|
|
|
2020-07-24 10:45:21 +03:00
|
|
|
data.storage = 1;
|
2020-08-01 05:04:44 +03:00
|
|
|
data.error = null;
|
|
|
|
|
|
|
|
userDataFields = LibraryManager.updateDataById({
|
|
|
|
user: { ...user, data: userDataFields },
|
|
|
|
id: data.id,
|
|
|
|
data,
|
|
|
|
});
|
2020-07-24 10:45:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-01 05:04:44 +03:00
|
|
|
let response;
|
|
|
|
if (success.length || failed.length) {
|
|
|
|
console.log("update");
|
|
|
|
response = await Data.updateUserById({
|
2020-07-24 10:45:21 +03:00
|
|
|
id: user.id,
|
2020-08-01 05:04:44 +03:00
|
|
|
data: userDataFields,
|
2020-07-24 10:45:21 +03:00
|
|
|
});
|
2020-08-01 05:04:44 +03:00
|
|
|
console.log(response);
|
2020-07-24 10:45:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return res.status(200).send({
|
|
|
|
decorator: "SERVER_CID_CHECK",
|
2020-08-01 05:04:44 +03:00
|
|
|
update: success.length || failed.length,
|
|
|
|
updateResponse: response,
|
2020-07-24 10:45:21 +03:00
|
|
|
});
|
|
|
|
};
|