mirror of
https://github.com/filecoin-project/slate.git
synced 2024-11-27 10:52:41 +03:00
library: refactor to support adding slates soon
This commit is contained in:
parent
01d4a3dc50
commit
c21dca1884
89
node_common/managers/library.js
Normal file
89
node_common/managers/library.js
Normal file
@ -0,0 +1,89 @@
|
||||
import { v4 as uuid } from "uuid";
|
||||
|
||||
// NOTE(jim):
|
||||
// There is some Navigation specific data here for folders.
|
||||
export const createBucket = ({ id, name }) => {
|
||||
return {
|
||||
decorator: "FOLDER",
|
||||
id,
|
||||
folderId: id,
|
||||
icon: "FOLDER",
|
||||
name: name,
|
||||
pageTitle: `Exploring ${name}`,
|
||||
date: new Date(),
|
||||
size: null,
|
||||
children: [],
|
||||
};
|
||||
};
|
||||
|
||||
// NOTE(jim):
|
||||
// Every root level user gets a bucket.
|
||||
const init = ({ bucketName, readableName }) => [
|
||||
createBucket({ id: bucketName, name: readableName }),
|
||||
];
|
||||
|
||||
export const createLocalDataIncomplete = ({ type, size, name }) => {
|
||||
return {
|
||||
id: `data-${uuid()}`,
|
||||
decorator: "FILE",
|
||||
icon: type,
|
||||
size: size,
|
||||
name,
|
||||
file: name,
|
||||
type: type,
|
||||
date: new Date(),
|
||||
networks: [],
|
||||
job: null,
|
||||
ipfs: null,
|
||||
storage: 0,
|
||||
retrieval: 0,
|
||||
};
|
||||
};
|
||||
|
||||
export const updateDataIPFS = (d, { ipfs }) => {
|
||||
if (!d.networks.includes("IPFS")) {
|
||||
d.networks.push("IPFS");
|
||||
}
|
||||
|
||||
return { ...d, ipfs };
|
||||
};
|
||||
|
||||
export const updateDataFilecoin = (d, { job, storage, retrieval }) => {
|
||||
if (!d.networks.includes("FILECOIN-PENDING")) {
|
||||
d.networks.push("FILECOIN-PENDING");
|
||||
}
|
||||
|
||||
return { ...d, job, storage, retrieval };
|
||||
};
|
||||
|
||||
export const add = (user, data) => {
|
||||
const { library } = user.data;
|
||||
|
||||
// TODO(jim): Since we don't support bucket organization... yet.
|
||||
// Add just pushes to the first set. But we can change this easily later.
|
||||
for (let i = 0; i < library.length; i++) {
|
||||
for (let j = 0; j < library[i].length; j++) {
|
||||
library[i].children[j].push(data);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return { ...user.data, library };
|
||||
};
|
||||
|
||||
export const removeById = (user, dataId) => {
|
||||
const { library } = user.data;
|
||||
|
||||
// TODO(jim): Since we don't support bucket organization... yet.
|
||||
// Add just pushes to the first set. But we can change this easily later.
|
||||
for (let i = 0; i < library.length; i++) {
|
||||
for (let j = 0; j < library[i].children.length; j++) {
|
||||
library[i].children[j] = library[i].children[j].filter(
|
||||
(e) => e.id !== dataId
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return { ...user.data, library };
|
||||
};
|
@ -2,6 +2,7 @@ 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 LibraryManager from "~/node_common/managers/library";
|
||||
|
||||
import FORM from "formidable";
|
||||
import FS from "fs-extra";
|
||||
@ -33,18 +34,8 @@ export default async (req, res) => {
|
||||
.send({ decorator: "SERVER_UPLOAD_NOT_IMAGE", error: true });
|
||||
}
|
||||
|
||||
const file = files.image;
|
||||
const data = {
|
||||
decorator: "FILE",
|
||||
icon: file.type,
|
||||
size: file.size,
|
||||
name: file.name,
|
||||
file: file.name,
|
||||
type: file.type,
|
||||
path: file._writeStream.path,
|
||||
date: new Date(),
|
||||
network: "IPFS",
|
||||
};
|
||||
const path = files.image._writeStream.path;
|
||||
const data = LibraryManager.createLocalDataIncomplete(files.image);
|
||||
|
||||
// TODO(jim): Send this file to buckets.
|
||||
const id = Utilities.getIdFromCookie(req);
|
||||
@ -59,21 +50,20 @@ export default async (req, res) => {
|
||||
} = await Utilities.getBucketAPIFromUserToken(user.data.tokens.api);
|
||||
|
||||
// NOTE(jim): Push pathPath to your bucket.
|
||||
const readFile = await FS.readFileSync(data.path).buffer;
|
||||
const push = await buckets.pushPath(bucketKey, file.name, readFile);
|
||||
data.ipfs = push.path.path;
|
||||
data.id = data.ipfs;
|
||||
const readFile = await FS.readFileSync(path).buffer;
|
||||
const push = await buckets.pushPath(bucketKey, data.name, readFile);
|
||||
const updatedData = LibraryManager.updateDataIPFS(data, {
|
||||
ipfs: push.path.path,
|
||||
});
|
||||
const updatedUserData = LibraryManager.add(user, updatedData);
|
||||
|
||||
user.data.library[0].children.push(data);
|
||||
|
||||
// TODO(jim): Update library on user.
|
||||
const response = await Data.updateUserById({
|
||||
id: user.id,
|
||||
data: { ...user.data },
|
||||
data: updatedUserData,
|
||||
});
|
||||
|
||||
// NOTE(jim): Remove the file when you're done with it.
|
||||
await FS.unlinkSync(`./${data.path}`);
|
||||
await FS.unlinkSync(`./${path}`);
|
||||
|
||||
return res.status(200).send({
|
||||
decorator: "SERVER_UPLOAD",
|
||||
|
@ -2,8 +2,9 @@ import * as Environment from "~/node_common/environment";
|
||||
import * as MW from "~/node_common/middleware";
|
||||
import * as Data from "~/node_common/data";
|
||||
import * as Utilities from "~/node_common/utilities";
|
||||
import * as Validations from "~/common/validations";
|
||||
import * as Powergate from "~/node_common/powergate";
|
||||
import * as LibraryManager from "~/node_common/managers/library";
|
||||
import * as Validations from "~/common/validations";
|
||||
|
||||
import JWT from "jsonwebtoken";
|
||||
import BCrypt from "bcrypt";
|
||||
@ -66,12 +67,7 @@ export default async (req, res) => {
|
||||
photo: "https://slate.host/static/a1.jpg",
|
||||
settings_deals_auto_approve: false,
|
||||
tokens: { pg, api },
|
||||
library: [
|
||||
{
|
||||
...Utilities.createFolder({ id: bucketName, name: "Data" }),
|
||||
children: [],
|
||||
},
|
||||
],
|
||||
library: LibraryManager.init({ bucketName, readableName: "data" }),
|
||||
},
|
||||
});
|
||||
|
||||
|
@ -189,7 +189,7 @@ export default class SceneEditAccount extends React.Component {
|
||||
<System.DescriptionGroup
|
||||
style={{ marginTop: 48 }}
|
||||
label="Reset password"
|
||||
description="Your new password must be a minimum of four characters."
|
||||
description="Your new password must be a minimum of eight characters."
|
||||
/>
|
||||
|
||||
<System.Input
|
||||
|
Loading…
Reference in New Issue
Block a user