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) {
|
2020-08-02 03:13:19 +03:00
|
|
|
return res.status(500).send({ decorator: "SERVER_NO_CIDS_TO_CHECK", error: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!req.body.data.length) {
|
|
|
|
return res.status(500).send({ decorator: "SERVER_NO_CIDS_TO_CHECK", error: true });
|
2020-07-24 10:45:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const id = Utilities.getIdFromCookie(req);
|
|
|
|
const user = await Data.getUserById({
|
|
|
|
id,
|
|
|
|
});
|
2020-08-02 03:13:19 +03:00
|
|
|
|
2020-07-24 10:45:21 +03:00
|
|
|
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 = [];
|
2020-08-02 07:09:49 +03:00
|
|
|
const reset = [];
|
2020-07-24 10:45:21 +03:00
|
|
|
|
2020-08-02 03:13:19 +03:00
|
|
|
for (let i = 0; i < req.body.data.length; i++) {
|
|
|
|
const x = req.body.data[i];
|
2020-07-24 10:45:21 +03:00
|
|
|
|
2020-08-02 03:13:19 +03:00
|
|
|
if (!x.job) {
|
|
|
|
failed.push(x);
|
|
|
|
continue;
|
|
|
|
}
|
2020-08-01 05:04:44 +03:00
|
|
|
|
2020-08-02 03:13:19 +03:00
|
|
|
if (!x.ipfs) {
|
|
|
|
failed.push(x);
|
2020-08-02 07:09:49 +03:00
|
|
|
continue;
|
2020-08-02 03:13:19 +03:00
|
|
|
}
|
2020-07-24 10:45:21 +03:00
|
|
|
|
2020-08-02 03:13:19 +03:00
|
|
|
let job;
|
|
|
|
try {
|
|
|
|
job = await check(PG, x.job);
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
2020-08-01 05:04:44 +03:00
|
|
|
|
2020-08-02 03:13:19 +03:00
|
|
|
if (job.status === 3) {
|
|
|
|
console.log({ message: "ERROR", job });
|
|
|
|
x.error = job.errCause;
|
|
|
|
failed.push(x);
|
|
|
|
continue;
|
2020-07-24 10:45:21 +03:00
|
|
|
}
|
2020-08-02 03:13:19 +03:00
|
|
|
|
|
|
|
if (job.status === 5) {
|
|
|
|
console.log({ message: "SUCCESS", job });
|
2020-08-02 07:09:49 +03:00
|
|
|
x.error = null;
|
2020-08-02 03:13:19 +03:00
|
|
|
success.push(x);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-08-02 07:09:49 +03:00
|
|
|
if (x.error) {
|
|
|
|
x.error = null;
|
|
|
|
reset.push(x);
|
|
|
|
}
|
|
|
|
|
2020-08-02 03:13:19 +03:00
|
|
|
console.log({ message: "NOOP", job });
|
2020-07-24 10:45:21 +03:00
|
|
|
}
|
|
|
|
|
2020-08-02 03:13:19 +03:00
|
|
|
let targetUser = { ...user };
|
|
|
|
|
2020-07-24 10:45:21 +03:00
|
|
|
if (failed.length) {
|
|
|
|
for (let i = 0; i < failed.length; i++) {
|
2020-08-02 03:13:19 +03:00
|
|
|
let data = LibraryManager.getDataByIPFS(targetUser, failed[i].ipfs);
|
|
|
|
if (!data) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-08-01 05:04:44 +03:00
|
|
|
|
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;
|
|
|
|
|
2020-08-02 03:13:19 +03:00
|
|
|
targetUser.data = LibraryManager.updateDataById({
|
|
|
|
user: targetUser,
|
2020-08-01 05:04:44 +03:00
|
|
|
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++) {
|
2020-08-02 03:13:19 +03:00
|
|
|
let data = LibraryManager.getDataByIPFS(targetUser, success[i].ipfs);
|
|
|
|
if (!data) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-08-01 05:04:44 +03:00
|
|
|
|
2020-07-24 10:45:21 +03:00
|
|
|
data.storage = 1;
|
2020-08-01 05:04:44 +03:00
|
|
|
data.error = null;
|
|
|
|
|
2020-08-02 03:13:19 +03:00
|
|
|
targetUser.data = LibraryManager.updateDataById({
|
|
|
|
user: targetUser,
|
2020-08-01 05:04:44 +03:00
|
|
|
id: data.id,
|
|
|
|
data,
|
|
|
|
});
|
2020-07-24 10:45:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-02 07:09:49 +03:00
|
|
|
if (reset.length) {
|
|
|
|
for (let i = 0; i < reset.length; i++) {
|
|
|
|
let data = LibraryManager.getDataByIPFS(targetUser, reset[i].ipfs);
|
|
|
|
if (!data) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
data.error = null;
|
|
|
|
|
|
|
|
targetUser.data = LibraryManager.updateDataById({
|
|
|
|
user: targetUser,
|
|
|
|
id: data.id,
|
|
|
|
data,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-01 05:04:44 +03:00
|
|
|
let response;
|
2020-08-02 07:09:49 +03:00
|
|
|
if (success.length || failed.length || reset.length) {
|
2020-08-01 05:04:44 +03:00
|
|
|
response = await Data.updateUserById({
|
2020-08-02 03:13:19 +03:00
|
|
|
id: targetUser.id,
|
|
|
|
data: targetUser.data,
|
2020-07-24 10:45:21 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.status(200).send({
|
|
|
|
decorator: "SERVER_CID_CHECK",
|
2020-08-02 07:09:49 +03:00
|
|
|
update: success.length || failed.length || reset.length,
|
2020-08-01 05:04:44 +03:00
|
|
|
updateResponse: response,
|
2020-07-24 10:45:21 +03:00
|
|
|
});
|
|
|
|
};
|