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-07-24 02:56:50 +03:00
|
|
|
import * as Powergate from "~/node_common/powergate";
|
2020-06-17 21:05:13 +03:00
|
|
|
|
2020-07-22 08:53:29 +03:00
|
|
|
import JWT from "jsonwebtoken";
|
2020-07-20 09:06:36 +03:00
|
|
|
|
|
|
|
import { Buckets } from "@textile/hub";
|
|
|
|
import { Libp2pCryptoIdentity } from "@textile/threads-core";
|
2020-07-23 13:05:13 +03:00
|
|
|
import { ffsOptions } from "@textile/powergate-client";
|
2020-07-20 09:06:36 +03:00
|
|
|
|
|
|
|
const BUCKET_NAME = "data";
|
|
|
|
|
|
|
|
const TEXTILE_KEY_INFO = {
|
2020-07-21 14:36:50 +03:00
|
|
|
key: Environment.TEXTILE_HUB_KEY,
|
|
|
|
secret: Environment.TEXTILE_HUB_SECRET,
|
|
|
|
};
|
|
|
|
|
2020-07-22 13:51:40 +03:00
|
|
|
export const getIdFromCookie = (req) => {
|
|
|
|
let id;
|
2020-07-22 08:53:29 +03:00
|
|
|
if (!Strings.isEmpty(req.headers.cookie)) {
|
|
|
|
const token = req.headers.cookie.replace(
|
|
|
|
/(?:(?:^|.*;\s*)WEB_SERVICE_SESSION_KEY\s*\=\s*([^;]*).*$)|^.*$/,
|
|
|
|
"$1"
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!Strings.isEmpty(token)) {
|
|
|
|
try {
|
|
|
|
const decoded = JWT.verify(token, Environment.JWT_SECRET);
|
2020-07-22 13:51:40 +03:00
|
|
|
id = decoded.id;
|
2020-07-22 10:41:29 +03:00
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
2020-07-22 08:53:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-22 13:51:40 +03:00
|
|
|
return id;
|
2020-07-22 08:53:29 +03:00
|
|
|
};
|
|
|
|
|
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-07-22 14:16:30 +03:00
|
|
|
export const getBucketAPI = async () => {
|
2020-07-22 12:58:25 +03:00
|
|
|
const buckets = await Buckets.withKeyInfo(TEXTILE_KEY_INFO);
|
|
|
|
return { buckets };
|
|
|
|
};
|
|
|
|
|
2020-07-20 09:06:36 +03:00
|
|
|
// NOTE(jim): Requires @textile/hub
|
|
|
|
export const getBucketAPIFromUserToken = async (token) => {
|
|
|
|
const identity = await Libp2pCryptoIdentity.fromString(token);
|
|
|
|
const buckets = await Buckets.withKeyInfo(TEXTILE_KEY_INFO);
|
|
|
|
await buckets.getToken(identity);
|
|
|
|
const root = await buckets.open(BUCKET_NAME);
|
|
|
|
|
|
|
|
return { buckets, bucketKey: root.key, bucketName: BUCKET_NAME };
|
|
|
|
};
|
|
|
|
|
|
|
|
// NOTE(jim): Requires Powergate, does not require token.
|
2020-07-24 02:56:50 +03:00
|
|
|
export const refresh = async (user) => {
|
|
|
|
const PG = Powergate.get(user);
|
2020-06-17 21:05:13 +03:00
|
|
|
const Health = await PG.health.check();
|
|
|
|
const status = Health.status ? Health.status : null;
|
|
|
|
const messageList = Health.messageList ? Health.messageList : null;
|
|
|
|
|
2020-07-27 04:51:51 +03:00
|
|
|
return { messageList, status };
|
2020-06-17 21:05:13 +03:00
|
|
|
};
|
|
|
|
|
2020-07-20 09:06:36 +03:00
|
|
|
// NOTE(jim): Requires Powergate & authentication
|
2020-07-24 02:56:50 +03:00
|
|
|
export const refreshWithToken = async (user) => {
|
|
|
|
const PG = Powergate.get(user);
|
2020-06-17 21:05:13 +03:00
|
|
|
const Addresses = await PG.ffs.addrs();
|
|
|
|
const addrsList = Addresses.addrsList ? Addresses.addrsList : null;
|
|
|
|
|
|
|
|
const NetworkInfo = await PG.ffs.info();
|
|
|
|
const info = NetworkInfo.info ? NetworkInfo.info : null;
|
|
|
|
|
2020-07-23 13:05:13 +03:00
|
|
|
const includeFinal = ffsOptions.withIncludeFinal(true);
|
|
|
|
const includePending = ffsOptions.withIncludePending(true);
|
|
|
|
const fromAddresses = ffsOptions.withFromAddresses(
|
|
|
|
info.defaultStorageConfig.cold.filecoin.addr
|
|
|
|
);
|
|
|
|
|
|
|
|
const s = await PG.ffs.listStorageDealRecords(
|
|
|
|
includeFinal,
|
|
|
|
includePending,
|
|
|
|
fromAddresses
|
|
|
|
);
|
2020-06-17 21:05:13 +03:00
|
|
|
|
2020-07-23 13:05:13 +03:00
|
|
|
const r = await PG.ffs.listRetrievalDealRecords();
|
2020-06-17 21:05:13 +03:00
|
|
|
|
2020-07-23 13:05:13 +03:00
|
|
|
return {
|
2020-06-17 21:05:13 +03:00
|
|
|
addrsList,
|
|
|
|
info,
|
2020-07-23 13:05:13 +03:00
|
|
|
storageList: s.recordsList,
|
|
|
|
retrievalList: r.recordsList,
|
|
|
|
};
|
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,
|
|
|
|
};
|
|
|
|
};
|