cid-check: refactor

This commit is contained in:
jimmylee 2020-08-01 17:13:19 -07:00
parent 3cf867d6ea
commit 4a842a0c42
2 changed files with 57 additions and 60 deletions

View File

@ -17,9 +17,7 @@ export const createBucket = ({ id, name }) => {
// NOTE(jim):
// Every root level user gets a bucket.
export const init = ({ bucketName, readableName }) => [
createBucket({ id: bucketName, name: readableName }),
];
export const init = ({ bucketName, readableName }) => [createBucket({ id: bucketName, name: readableName })];
export const createLocalDataIncomplete = ({ type, size, name }) => {
return {
@ -75,7 +73,7 @@ export const updateDataById = ({ user, id, data }) => {
for (let j = 0; j < library[i].children.length; j++) {
if (library[i].children[j].id === id) {
library[i].children[j] = data;
break;
return { ...user.data, library };
}
}
}

View File

@ -21,75 +21,76 @@ export default async (req, res) => {
initAuth(req, res);
if (!req.body.data) {
return res
.status(500)
.send({ decorator: "SERVER_NO_CIDS_TO_CHECK", error: true });
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 });
}
const id = Utilities.getIdFromCookie(req);
const user = await Data.getUserById({
id,
});
const PG = Powergate.get(user);
const success = [];
const failed = [];
try {
for (let i = 0; i < req.body.data.length; i++) {
const x = req.body.data[i];
if (!x.job) {
console.log("-- NO JOB (3)");
failed.push(x);
continue;
}
for (let i = 0; i < req.body.data.length; i++) {
const x = req.body.data[i];
if (!x.ipfs) {
console.log("-- NO IPFS (3)");
failed.push(x);
}
// TODO(jim): A hack, I just want the first job object from a stream.
const job = await check(PG, x.job);
console.log({ job });
// 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.
if (job.status === 3) {
console.log("CID ERROR (3): ", job.cid);
x.error = job.errCause;
failed.push(x);
continue;
}
if (job.status === 5) {
console.log("CID SUCCESS (5): ", job.cid);
success.push(x);
continue;
}
console.log("CID: ", job.cid);
if (!x.job) {
console.log("-- NO JOB (3)");
failed.push(x);
continue;
}
} catch (e) {
console.log(e);
if (!x.ipfs) {
console.log("-- NO IPFS (3)");
failed.push(x);
}
let job;
try {
job = await check(PG, x.job);
} catch (e) {
console.log(e);
}
if (job.status === 3) {
console.log({ message: "ERROR", job });
x.error = job.errCause;
failed.push(x);
continue;
}
if (job.status === 5) {
console.log({ message: "SUCCESS", job });
success.push(x);
continue;
}
console.log({ message: "NOOP", job });
}
let userDataFields = { ...user.data };
let targetUser = { ...user };
if (failed.length) {
for (let i = 0; i < failed.length; i++) {
let data = LibraryManager.getDataByIPFS(
{ ...user, data: userDataFields },
failed[i].ipfs
);
let data = LibraryManager.getDataByIPFS(targetUser, failed[i].ipfs);
if (!data) {
continue;
}
data.networks = data.networks.filter((each) => each !== "FILECOIN");
data.job = null;
data.storage = 0;
data.error = failed[i].error;
userDataFields = LibraryManager.updateDataById({
user: { ...user, data: userDataFields },
targetUser.data = LibraryManager.updateDataById({
user: targetUser,
id: data.id,
data,
});
@ -98,16 +99,16 @@ export default async (req, res) => {
if (success.length) {
for (let i = 0; i < success.length; i++) {
let data = LibraryManager.getDataByIPFS(
{ ...user, data: userDataFields },
success[i].ipfs
);
let data = LibraryManager.getDataByIPFS(targetUser, success[i].ipfs);
if (!data) {
continue;
}
data.storage = 1;
data.error = null;
userDataFields = LibraryManager.updateDataById({
user: { ...user, data: userDataFields },
targetUser.data = LibraryManager.updateDataById({
user: targetUser,
id: data.id,
data,
});
@ -116,12 +117,10 @@ export default async (req, res) => {
let response;
if (success.length || failed.length) {
console.log("update");
response = await Data.updateUserById({
id: user.id,
data: userDataFields,
id: targetUser.id,
data: targetUser.data,
});
console.log(response);
}
return res.status(200).send({