slate/pages/api/data/bucket-remove.js

105 lines
2.7 KiB
JavaScript
Raw Normal View History

2020-09-23 14:17:56 +03:00
import * as Data from "~/node_common/data";
import * as Utilities from "~/node_common/utilities";
import * as Strings from "~/common/strings";
import * as Social from "~/node_common/social";
import * as RequestUtilities from "~/node_common/request-utilities";
2020-09-23 14:17:56 +03:00
export default async (req, res) => {
const userInfo = await RequestUtilities.checkAuthorizationInternal(req, res);
if (!userInfo) return;
const { id, user } = userInfo;
if (Strings.isEmpty(req.body.data?.cid)) {
return res.status(500).send({ decorator: "SERVER_BUCKET_REMOVE_NO_CID", error: true });
2020-09-23 14:17:56 +03:00
}
2021-09-25 04:20:28 +03:00
const { buckets, bucketKey } = await Utilities.getBucket({
2020-09-23 14:17:56 +03:00
user,
bucketName: req.body.data.bucketName,
});
if (!buckets) {
return res.status(500).send({
decorator: "SERVER_NO_BUCKET_DATA",
2020-09-23 14:17:56 +03:00
error: true,
});
}
// TODO(jim): Put this call into a file for all Textile related calls.
let r = null;
try {
r = await buckets.list();
} catch (e) {
Social.sendTextileSlackMessage({
file: "/pages/api/data/bucket-remove.js",
user,
message: e.message,
code: e.code,
functionName: `buckets.list`,
});
}
if (!r) {
return res.status(500).send({ decorator: "SERVER_NO_BUCKET_DATA", error: true });
2020-09-23 14:17:56 +03:00
}
const targetBucket = r.find((d) => d.name === req.body.data.bucketName);
if (!targetBucket) {
return res
.status(404)
.send({ decorator: "SERVER_BUCKET_REMOVE_BUCKET_NOT_FOUND", error: true });
2020-09-23 14:17:56 +03:00
}
let items = null;
try {
const path = await buckets.listPath(targetBucket.key, "/");
items = path.item;
} catch (e) {
Social.sendTextileSlackMessage({
file: "/pages/api/data/bucket-remove.js",
user,
message: e.message,
code: e.code,
functionName: `buckets.listPath`,
});
2020-09-23 14:17:56 +03:00
}
if (!items) {
return res.status(500).send({ decorator: "SERVER_BUCKET_REMOVE_NO_BUCKET_ITEMS", error: true });
2020-09-23 14:17:56 +03:00
}
let entity;
for (let i = 0; i < items.items.length; i++) {
if (items.items[i].cid === req.body.data.cid) {
entity = items.items[i];
break;
}
}
if (!entity) {
return res.status(500).send({ decorator: "SERVER_BUCKET_REMOVE_NO_MATCHING_CID", error: true });
2020-09-23 14:17:56 +03:00
}
let bucketRemoval;
try {
bucketRemoval = await buckets.removePath(bucketKey, entity.name);
} catch (e) {
Social.sendTextileSlackMessage({
file: "/pages/api/data/bucket-remove.js",
user,
message: e.message,
code: e.code,
functionName: `buckets.removePath`,
});
return res.status(500).send({ decorator: "SERVER_BUCKET_REMOVE_FAILED", error: true });
2020-09-23 14:17:56 +03:00
}
return res.status(200).send({
decorator: "SERVER_BUCKET_REMOVE",
2020-09-23 14:17:56 +03:00
success: true,
bucketItems: items.items,
});
};