2020-07-21 14:36:50 +03:00
|
|
|
import * as Environment from "~/node_common/environment";
|
2020-07-22 08:53:29 +03:00
|
|
|
import * as Strings from "~/common/strings";
|
2020-08-01 03:17:07 +03:00
|
|
|
import * as Constants from "~/node_common/constants";
|
2021-05-19 05:58:14 +03:00
|
|
|
import * as Data from "~/node_common/data";
|
2020-09-22 03:36:45 +03:00
|
|
|
import * as Social from "~/node_common/social";
|
2021-06-11 22:25:58 +03:00
|
|
|
import * as Logging from "~/common/logging";
|
2021-07-07 23:50:57 +03:00
|
|
|
import * as ArrayUtilities from "~/node_common/array-utilities";
|
|
|
|
import * as Monitor from "~/node_common/monitor";
|
2021-08-07 04:13:45 +03:00
|
|
|
import * as Arrays from "~/common/arrays";
|
2021-11-01 23:38:47 +03:00
|
|
|
import SearchManager from "~/node_common/managers/search";
|
2020-06-17 21:05:13 +03:00
|
|
|
|
2020-10-27 07:41:42 +03:00
|
|
|
import crypto from "crypto";
|
2020-07-22 08:53:29 +03:00
|
|
|
import JWT from "jsonwebtoken";
|
2020-08-11 08:15:39 +03:00
|
|
|
import BCrypt from "bcrypt";
|
2020-07-20 09:06:36 +03:00
|
|
|
|
2020-10-27 07:41:42 +03:00
|
|
|
const ENCRYPTION_ALGORITHM = "aes-256-ctr";
|
|
|
|
const ENCRYPTION_IV = crypto.randomBytes(16);
|
|
|
|
|
2020-12-04 00:45:17 +03:00
|
|
|
import { Buckets, PrivateKey, Filecoin, Client, ThreadID } from "@textile/hub";
|
2020-07-20 09:06:36 +03:00
|
|
|
|
|
|
|
const TEXTILE_KEY_INFO = {
|
2020-07-21 14:36:50 +03:00
|
|
|
key: Environment.TEXTILE_HUB_KEY,
|
|
|
|
secret: Environment.TEXTILE_HUB_SECRET,
|
|
|
|
};
|
|
|
|
|
2020-08-20 08:57:46 +03:00
|
|
|
export const checkTextile = async () => {
|
|
|
|
try {
|
2020-08-25 20:46:41 +03:00
|
|
|
const response = await fetch("https://slate.textile.io/health", {
|
2020-08-20 08:57:46 +03:00
|
|
|
headers: {
|
|
|
|
Accept: "application/json",
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (response.status === 204) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-09-23 03:34:17 +03:00
|
|
|
|
|
|
|
Social.sendTextileSlackMessage({
|
|
|
|
file: "/node_common/utilities.js",
|
|
|
|
user: { username: "UNDEFINED" },
|
|
|
|
message: "https://slate.textile.io/health is down",
|
|
|
|
code: "N/A",
|
|
|
|
functionName: `checkTextile`,
|
|
|
|
});
|
2020-08-20 08:57:46 +03:00
|
|
|
} catch (e) {
|
2020-09-23 03:34:17 +03:00
|
|
|
Social.sendTextileSlackMessage({
|
|
|
|
file: "/node_common/utilities.js",
|
|
|
|
user: { username: "UNDEFINED" },
|
|
|
|
message: e.message,
|
|
|
|
code: e.code,
|
|
|
|
functionName: `checkTextile`,
|
|
|
|
});
|
2020-08-20 08:57:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2021-09-15 01:58:00 +03:00
|
|
|
export const getIdFromCookieValue = (token) => {
|
|
|
|
if (!Strings.isEmpty(token)) {
|
|
|
|
try {
|
|
|
|
const decoded = JWT.verify(token, Environment.JWT_SECRET);
|
2021-09-15 21:20:17 +03:00
|
|
|
return decoded.id;
|
2021-09-15 01:58:00 +03:00
|
|
|
} catch (e) {
|
|
|
|
Logging.error(e.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-22 13:51:40 +03:00
|
|
|
export const getIdFromCookie = (req) => {
|
2020-09-03 03:35:41 +03:00
|
|
|
let id = null;
|
|
|
|
if (Strings.isEmpty(req.headers.cookie)) {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
const token = req.headers.cookie.replace(
|
|
|
|
/(?:(?:^|.*;\s*)WEB_SERVICE_SESSION_KEY\s*\=\s*([^;]*).*$)|^.*$/,
|
|
|
|
"$1"
|
|
|
|
);
|
|
|
|
|
2021-09-15 01:58:00 +03:00
|
|
|
return getIdFromCookieValue(token);
|
2020-07-22 08:53:29 +03:00
|
|
|
};
|
|
|
|
|
2020-10-27 07:41:42 +03:00
|
|
|
export const encryptWithSecret = async (text, secret) => {
|
|
|
|
const cipher = crypto.createCipheriv(ENCRYPTION_ALGORITHM, secret, ENCRYPTION_IV);
|
|
|
|
const encrypted = Buffer.concat([cipher.update(text), cipher.final()]);
|
|
|
|
|
|
|
|
return {
|
|
|
|
iv: ENCRYPTION_IV.toString("hex"),
|
|
|
|
hex: encrypted.toString("hex"),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-08-11 08:19:02 +03:00
|
|
|
export const encryptPassword = async (text, salt) => {
|
2020-08-11 08:15:39 +03:00
|
|
|
if (!text) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
let hash = text;
|
|
|
|
for (let i = 0; i < Environment.LOCAL_PASSWORD_ROUNDS_MANUAL; i++) {
|
|
|
|
hash = await BCrypt.hash(hash, salt);
|
|
|
|
}
|
|
|
|
hash = await BCrypt.hash(hash, Environment.LOCAL_PASSWORD_SECRET);
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
};
|
|
|
|
|
2020-07-21 14:36:50 +03:00
|
|
|
export const parseAuthHeader = (value) => {
|
|
|
|
if (typeof value !== "string") {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var matches = value.match(/(\S+)\s+(\S+)/);
|
|
|
|
return matches && { scheme: matches[1], value: matches[2] };
|
2020-07-20 09:06:36 +03:00
|
|
|
};
|
|
|
|
|
2020-12-04 00:45:17 +03:00
|
|
|
export const getFilecoinAPIFromUserToken = async ({ user }) => {
|
2021-09-25 04:20:28 +03:00
|
|
|
const textileKey = user.textileKey;
|
|
|
|
const identity = await PrivateKey.fromString(textileKey);
|
2020-12-04 00:45:17 +03:00
|
|
|
const filecoin = await Filecoin.withKeyInfo(TEXTILE_KEY_INFO);
|
|
|
|
await filecoin.getToken(identity);
|
2020-09-22 03:36:45 +03:00
|
|
|
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log(`filecoin init`);
|
2020-11-25 12:47:29 +03:00
|
|
|
|
2020-09-09 20:56:35 +03:00
|
|
|
return {
|
2020-12-04 00:45:17 +03:00
|
|
|
filecoin,
|
2020-09-09 20:56:35 +03:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-09-27 05:08:07 +03:00
|
|
|
export const setupWithThread = async ({ buckets }) => {
|
|
|
|
const client = new Client(buckets.context);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const res = await client.getThread("buckets");
|
|
|
|
|
|
|
|
buckets.withThread(res.id.toString());
|
|
|
|
} catch (error) {
|
|
|
|
if (error.message !== "Thread not found") {
|
|
|
|
throw new Error(error.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
const newId = ThreadID.fromRandom();
|
|
|
|
await client.newDB(newId, "buckets");
|
|
|
|
const threadID = newId.toString();
|
|
|
|
|
|
|
|
buckets.withThread(threadID);
|
|
|
|
}
|
|
|
|
|
|
|
|
return buckets;
|
|
|
|
};
|
|
|
|
|
2020-10-05 00:30:28 +03:00
|
|
|
export const addExistingCIDToData = async ({ buckets, key, path, cid }) => {
|
|
|
|
try {
|
|
|
|
await buckets.setPath(key, path || "/", cid);
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-20 09:06:36 +03:00
|
|
|
// NOTE(jim): Requires @textile/hub
|
2021-09-25 04:20:28 +03:00
|
|
|
// export const getBucketAPIFromUserToken = async ({
|
|
|
|
// user,
|
|
|
|
// bucketName = Constants.textile.mainBucket,
|
|
|
|
// encrypted = false,
|
|
|
|
// }) => {
|
|
|
|
// const token = user.textileToken;
|
|
|
|
// const name = bucketName;
|
|
|
|
// const identity = await PrivateKey.fromString(token);
|
|
|
|
// let buckets = await Buckets.withKeyInfo(TEXTILE_KEY_INFO);
|
|
|
|
|
|
|
|
// const textileToken = await buckets.getToken(identity);
|
|
|
|
|
|
|
|
// let root = null;
|
|
|
|
// Logging.log(`buckets.getOrCreate() init ${name}`);
|
|
|
|
// try {
|
|
|
|
// Logging.log("before buckets get or create");
|
|
|
|
// const created = await buckets.getOrCreate(name, { encrypted });
|
|
|
|
// Logging.log("after buckets get or create");
|
|
|
|
// root = created.root;
|
|
|
|
// } catch (e) {
|
|
|
|
// Logging.log(`buckets.getOrCreate() warning: ${e.message}`);
|
|
|
|
// Social.sendTextileSlackMessage({
|
|
|
|
// file: "/node_common/utilities.js",
|
|
|
|
// user,
|
|
|
|
// message: e.message,
|
|
|
|
// code: e.code,
|
|
|
|
// functionName: `buckets.getOrCreate`,
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if (!root) {
|
|
|
|
// Logging.error(`buckets.getOrCreate() failed for ${name}`);
|
|
|
|
// return { buckets: null, bucketKey: null, bucketRoot: null };
|
|
|
|
// }
|
|
|
|
|
|
|
|
// Logging.log(`buckets.getOrCreate() success for ${name}`);
|
|
|
|
// return {
|
|
|
|
// buckets,
|
|
|
|
// bucketKey: root.key,
|
|
|
|
// bucketRoot: root,
|
|
|
|
// bucketName: name,
|
|
|
|
// };
|
|
|
|
// };
|
|
|
|
|
|
|
|
//NOTE(martina): only use this upon creating a new user. This creates their bucket without checking for an existing bucket
|
2021-09-30 00:53:46 +03:00
|
|
|
export const createBucket = async ({ bucketName, encrypted = false }) => {
|
|
|
|
bucketName = bucketName || Constants.textile.mainBucket;
|
2021-09-25 04:20:28 +03:00
|
|
|
try {
|
|
|
|
const identity = await PrivateKey.fromRandom();
|
|
|
|
const textileKey = identity.toString();
|
2020-09-23 12:46:59 +03:00
|
|
|
|
2021-09-25 04:20:28 +03:00
|
|
|
let buckets = await Buckets.withKeyInfo(TEXTILE_KEY_INFO);
|
2020-09-22 03:36:45 +03:00
|
|
|
|
2021-09-25 04:20:28 +03:00
|
|
|
const textileToken = await buckets.getToken(identity);
|
|
|
|
buckets.context.withToken(textileToken);
|
|
|
|
|
|
|
|
const client = new Client(buckets.context);
|
|
|
|
const newId = ThreadID.fromRandom();
|
|
|
|
await client.newDB(newId, Constants.textile.threadName);
|
|
|
|
const textileThreadID = newId.toString();
|
|
|
|
buckets.context.withThread(textileThreadID);
|
|
|
|
|
|
|
|
const created = await buckets.create(bucketName, { encrypted });
|
|
|
|
let ipfs = created.root.path;
|
|
|
|
const textileBucketCID = Strings.ipfsToCid(ipfs);
|
|
|
|
|
|
|
|
return {
|
|
|
|
textileKey,
|
|
|
|
textileToken,
|
|
|
|
textileThreadID,
|
|
|
|
textileBucketCID,
|
|
|
|
buckets,
|
|
|
|
bucketKey: created.root.key,
|
|
|
|
bucketRoot: created.root,
|
|
|
|
bucketName,
|
|
|
|
};
|
|
|
|
} catch (e) {
|
|
|
|
Logging.error(e?.message);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//NOTE(martina): only use this for existing users. This grabs their bucket without checking for an existing bucket
|
2021-09-30 00:53:46 +03:00
|
|
|
export const getBucket = async ({ user, bucketName }) => {
|
|
|
|
bucketName = bucketName || Constants.textile.mainBucket;
|
2021-09-25 04:20:28 +03:00
|
|
|
let updateUser = false;
|
|
|
|
let { textileKey, textileToken, textileThreadID, textileBucketCID } = user;
|
|
|
|
|
|
|
|
if (!textileKey) {
|
|
|
|
return await createBucket({ user, bucketName });
|
|
|
|
}
|
|
|
|
|
|
|
|
let buckets = await Buckets.withKeyInfo(TEXTILE_KEY_INFO);
|
|
|
|
|
|
|
|
if (!textileToken) {
|
|
|
|
const identity = PrivateKey.fromString(textileKey);
|
|
|
|
textileToken = await buckets.getToken(identity);
|
|
|
|
updateUser = true;
|
2020-11-21 19:49:01 +03:00
|
|
|
}
|
2021-09-25 04:20:28 +03:00
|
|
|
buckets.context.withToken(textileToken);
|
2020-09-22 03:36:45 +03:00
|
|
|
|
2021-09-25 04:20:28 +03:00
|
|
|
if (!textileThreadID) {
|
|
|
|
const client = new Client(buckets.context);
|
|
|
|
const res = await client.getThread("buckets");
|
|
|
|
textileThreadID = typeof res.id === "string" ? res.id : ThreadID.fromBytes(res.id).toString();
|
|
|
|
updateUser = true;
|
2020-09-22 03:36:45 +03:00
|
|
|
}
|
2021-09-25 04:20:28 +03:00
|
|
|
buckets.context.withThread(textileThreadID);
|
|
|
|
|
|
|
|
const roots = await buckets.list();
|
|
|
|
const existing = roots.find((bucket) => bucket.name === bucketName);
|
|
|
|
|
|
|
|
if (!existing) {
|
|
|
|
return { buckets: null, bucketKey: null, bucketRoot: null, bucketName };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!textileBucketCID) {
|
|
|
|
let ipfs = existing.path;
|
|
|
|
textileBucketCID = Strings.ipfsToCid(ipfs);
|
|
|
|
updateUser = true;
|
|
|
|
}
|
|
|
|
if (updateUser) {
|
|
|
|
Data.updateUserById({ id: user.id, textileToken, textileThreadID, textileBucketCID });
|
|
|
|
}
|
|
|
|
|
2020-09-23 00:39:57 +03:00
|
|
|
return {
|
|
|
|
buckets,
|
2021-09-25 04:20:28 +03:00
|
|
|
bucketKey: existing.key,
|
|
|
|
bucketRoot: existing,
|
|
|
|
bucketName,
|
2020-09-23 00:39:57 +03:00
|
|
|
};
|
2020-07-20 09:06:36 +03:00
|
|
|
};
|
|
|
|
|
2020-06-17 21:05:13 +03:00
|
|
|
export const getFileName = (s) => {
|
|
|
|
let target = s;
|
2020-07-09 06:19:08 +03:00
|
|
|
if (target.endsWith("/")) {
|
2020-06-17 21:05:13 +03:00
|
|
|
target = target.substring(0, target.length - 1);
|
|
|
|
}
|
|
|
|
|
2020-07-09 06:19:08 +03:00
|
|
|
return target.substr(target.lastIndexOf("/") + 1);
|
2020-06-17 21:05:13 +03:00
|
|
|
};
|
|
|
|
|
2020-07-21 08:45:15 +03:00
|
|
|
export const createFolder = ({ id, name }) => {
|
2020-06-17 21:05:13 +03:00
|
|
|
return {
|
2020-07-09 06:19:08 +03:00
|
|
|
decorator: "FOLDER",
|
2020-06-17 21:05:13 +03:00
|
|
|
id,
|
|
|
|
folderId: id,
|
2020-07-09 06:19:08 +03:00
|
|
|
icon: "FOLDER",
|
2020-07-21 08:45:15 +03:00
|
|
|
name: name ? name : getFileName(id),
|
|
|
|
pageTitle: `Exploring ${getFileName(id)}`,
|
2020-06-17 21:05:13 +03:00
|
|
|
date: null,
|
|
|
|
size: null,
|
|
|
|
children: [],
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const updateStateData = async (state, newState) => {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
...newState,
|
|
|
|
};
|
|
|
|
};
|
2021-03-19 19:37:09 +03:00
|
|
|
|
2021-06-09 01:53:30 +03:00
|
|
|
export const generateRandomNumberInRange = (min, max) =>
|
|
|
|
Math.floor(Math.random() * (max - min)) + min;
|
|
|
|
|
2021-03-19 19:37:09 +03:00
|
|
|
// NOTE(daniel): get all tags on slates and files
|
2021-08-25 00:04:10 +03:00
|
|
|
export const getUserTags = ({ library }) => {
|
2021-03-19 19:37:09 +03:00
|
|
|
let tags = new Set();
|
|
|
|
|
|
|
|
const isNotEmptyArray = (arr) => Array.isArray(arr) && arr?.length > 0;
|
|
|
|
|
|
|
|
library.forEach((item) => {
|
|
|
|
if (isNotEmptyArray(item.tags)) {
|
|
|
|
for (let tag of item.tags) {
|
|
|
|
tags.add(tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return Array.from(tags);
|
|
|
|
};
|
2021-07-07 23:50:57 +03:00
|
|
|
|
|
|
|
export const addToSlate = async ({ slate, files, user, saveCopy = false }) => {
|
|
|
|
let { filteredFiles } = await ArrayUtilities.removeDuplicateSlateFiles({
|
|
|
|
files,
|
|
|
|
slate,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!filteredFiles.length) {
|
|
|
|
return { added: 0 };
|
|
|
|
}
|
|
|
|
|
|
|
|
let response = await Data.createSlateFiles({ owner: user, slate, files: filteredFiles });
|
|
|
|
if (!response || response.error) {
|
|
|
|
return {
|
|
|
|
decorator: saveCopy
|
|
|
|
? "SERVER_SAVE_COPY_ADD_TO_SLATE_FAILED"
|
|
|
|
: "SERVER_CREATE_FILE_ADD_TO_SLATE_FAILED",
|
|
|
|
added: 0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (saveCopy) {
|
|
|
|
Monitor.saveCopy({ user, slate, files: filteredFiles });
|
|
|
|
} else {
|
|
|
|
Monitor.upload({ user, slate, files: filteredFiles });
|
|
|
|
}
|
|
|
|
|
|
|
|
await Data.updateSlateById({ id: slate.id, updatedAt: new Date() });
|
2021-09-08 22:30:59 +03:00
|
|
|
|
|
|
|
if (slate.isPublic) {
|
|
|
|
addToPublicCollectionUpdatePrivacy({ filteredFiles });
|
|
|
|
}
|
2021-07-07 23:50:57 +03:00
|
|
|
|
|
|
|
return { added: response.length };
|
|
|
|
};
|
2021-08-07 04:13:45 +03:00
|
|
|
|
|
|
|
export const removeFromPublicCollectionUpdatePrivacy = async ({ files }) => {
|
|
|
|
let targetFiles = Arrays.filterPublic(files);
|
|
|
|
let madePrivate = [];
|
|
|
|
for (let file of targetFiles) {
|
|
|
|
let updatedFile = await Data.recalcFilePrivacy({ fileId: file.id });
|
|
|
|
if (!updatedFile) continue;
|
|
|
|
if (file.isPublic && !updatedFile.isPublic) {
|
|
|
|
madePrivate.push(updatedFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (madePrivate.length) {
|
2021-10-19 00:06:27 +03:00
|
|
|
SearchManager.updateFile(madePrivate);
|
2021-08-07 04:13:45 +03:00
|
|
|
}
|
|
|
|
return madePrivate;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const addToPublicCollectionUpdatePrivacy = async ({ files }) => {
|
|
|
|
let targetFiles = Arrays.filterPrivate(files);
|
|
|
|
let madePublic = [];
|
|
|
|
for (let file of targetFiles) {
|
|
|
|
let updatedFile = await Data.recalcFilePrivacy({ fileId: file.id });
|
|
|
|
if (!updatedFile) continue;
|
|
|
|
if (!file.isPublic && updatedFile.isPublic) {
|
|
|
|
madePublic.push(updatedFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (madePublic.length) {
|
2021-10-19 00:06:27 +03:00
|
|
|
SearchManager.updateFile(madePublic);
|
2021-08-07 04:13:45 +03:00
|
|
|
}
|
|
|
|
return madePublic;
|
|
|
|
};
|