2021-01-13 05:31:02 +00:00
|
|
|
import { getEndpoint } from "utils/common/apiUtil";
|
|
|
|
import { getData, LS_KEYS } from "utils/storage/localStorage";
|
2021-01-14 11:31:20 +00:00
|
|
|
import { file, user, getFiles } from "./fileService";
|
|
|
|
import localForage from 'localforage';
|
|
|
|
|
2021-01-13 05:31:02 +00:00
|
|
|
import HTTPService from "./HTTPService";
|
|
|
|
import * as Comlink from 'comlink';
|
2021-01-13 12:43:46 +00:00
|
|
|
import { keyEncryptionResult } from "./uploadService";
|
2021-01-15 11:11:06 +00:00
|
|
|
import { getActualKey, getToken } from "utils/common/key";
|
2021-01-13 05:31:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
const CryptoWorker: any =
|
|
|
|
typeof window !== 'undefined' &&
|
|
|
|
Comlink.wrap(new Worker('worker/crypto.worker.js', { type: 'module' }));
|
|
|
|
const ENDPOINT = getEndpoint();
|
|
|
|
|
|
|
|
|
2021-01-13 12:43:46 +00:00
|
|
|
enum CollectionType {
|
2021-01-14 09:41:09 +00:00
|
|
|
folder = "folder",
|
|
|
|
favorites = "favorites",
|
|
|
|
album = "album",
|
2021-01-13 12:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface collection {
|
|
|
|
id: string;
|
|
|
|
owner: user;
|
2021-01-18 02:27:00 +00:00
|
|
|
key?: string;
|
2021-01-21 02:24:17 +00:00
|
|
|
name?: string;
|
|
|
|
encryptedName?: string;
|
|
|
|
nameDecryptionNonce?: string;
|
2021-01-14 09:41:09 +00:00
|
|
|
type: string;
|
2021-01-13 12:43:46 +00:00
|
|
|
attributes: collectionAttributes
|
|
|
|
sharees: user[];
|
|
|
|
updationTime: number;
|
|
|
|
encryptedKey: string;
|
|
|
|
keyDecryptionNonce: string;
|
|
|
|
isDeleted: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface collectionAttributes {
|
|
|
|
encryptedPath?: string;
|
|
|
|
pathDecryptionNonce?: string
|
|
|
|
};
|
|
|
|
|
2021-01-14 11:31:20 +00:00
|
|
|
export interface collectionLatestFile {
|
|
|
|
collection: collection
|
|
|
|
file: file;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-21 02:24:17 +00:00
|
|
|
const getCollectionSecrets = async (collection: collection, masterKey: string) => {
|
2021-01-13 05:31:02 +00:00
|
|
|
const worker = await new CryptoWorker();
|
|
|
|
const userID = getData(LS_KEYS.USER).id;
|
2021-01-18 02:27:00 +00:00
|
|
|
let decryptedKey: string;
|
2021-01-13 05:31:02 +00:00
|
|
|
if (collection.owner.id == userID) {
|
2021-01-18 02:27:00 +00:00
|
|
|
decryptedKey = await worker.decryptB64(
|
|
|
|
collection.encryptedKey,
|
|
|
|
collection.keyDecryptionNonce,
|
|
|
|
masterKey
|
2021-01-13 05:31:02 +00:00
|
|
|
);
|
2021-01-21 05:45:50 +00:00
|
|
|
|
2021-01-13 05:31:02 +00:00
|
|
|
} else {
|
|
|
|
const keyAttributes = getData(LS_KEYS.KEY_ATTRIBUTES);
|
2021-01-18 02:27:00 +00:00
|
|
|
const secretKey = await worker.decryptB64(
|
|
|
|
keyAttributes.encryptedSecretKey,
|
|
|
|
keyAttributes.secretKeyDecryptionNonce,
|
|
|
|
masterKey
|
2021-01-13 05:31:02 +00:00
|
|
|
);
|
|
|
|
decryptedKey = await worker.boxSealOpen(
|
2021-01-21 02:24:17 +00:00
|
|
|
collection.encryptedKey,
|
|
|
|
keyAttributes.publicKey,
|
|
|
|
secretKey
|
2021-01-13 05:31:02 +00:00
|
|
|
);
|
|
|
|
}
|
2021-01-25 12:00:05 +00:00
|
|
|
collection.name = collection.name || await worker.decryptString(
|
2021-01-21 05:45:50 +00:00
|
|
|
collection.encryptedName,
|
|
|
|
collection.nameDecryptionNonce,
|
|
|
|
decryptedKey);
|
2021-01-13 05:31:02 +00:00
|
|
|
return {
|
|
|
|
...collection,
|
|
|
|
key: decryptedKey,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const getCollections = async (
|
|
|
|
token: string,
|
|
|
|
sinceTime: string,
|
2021-01-18 02:27:00 +00:00
|
|
|
key: string
|
2021-01-13 05:31:02 +00:00
|
|
|
): Promise<collection[]> => {
|
2021-01-25 07:05:45 +00:00
|
|
|
try {
|
|
|
|
const resp = await HTTPService.get(`${ENDPOINT}/collections`, {
|
|
|
|
sinceTime: sinceTime,
|
2021-01-26 06:39:10 +00:00
|
|
|
}, { 'X-Auth-Token': token, });
|
2021-01-25 12:00:05 +00:00
|
|
|
const promises: Promise<collection>[] = resp.data.collections.map(
|
2021-01-25 07:05:45 +00:00
|
|
|
(collection: collection) => getCollectionSecrets(collection, key)
|
|
|
|
);
|
|
|
|
return await Promise.all(promises);
|
|
|
|
}
|
|
|
|
catch (e) {
|
2021-02-03 08:02:54 +00:00
|
|
|
console.log("getCollections failed- " + e);
|
2021-01-25 07:05:45 +00:00
|
|
|
}
|
2021-01-13 05:31:02 +00:00
|
|
|
};
|
|
|
|
|
2021-01-31 12:10:45 +00:00
|
|
|
export const getLocalCollections = async (): Promise<collection[]> => {
|
|
|
|
const collections = await localForage.getItem('collections') as collection[] ?? [];
|
|
|
|
return collections;
|
|
|
|
}
|
|
|
|
export const fetchUpdatedCollections = async (token: string, key: string) => {
|
2021-01-31 11:44:02 +00:00
|
|
|
const collectionUpdateTime = await localForage.getItem('collection-update-time') as string;
|
2021-02-03 08:02:54 +00:00
|
|
|
const updatedCollections = await getCollections(token, collectionUpdateTime ?? '0', key) || [];
|
2021-01-31 12:10:45 +00:00
|
|
|
const favCollection = await localForage.getItem('fav-collection') as collection[] ?? updatedCollections.filter(collection => collection.type === CollectionType.favorites);
|
|
|
|
const localCollections = await getLocalCollections();
|
2021-02-03 08:02:54 +00:00
|
|
|
const allCollectionsInstances = [...localCollections, ...updatedCollections];
|
|
|
|
var latestCollectionsInstances = new Map<string, collection>();
|
|
|
|
allCollectionsInstances.forEach((collection) => {
|
|
|
|
if (!latestCollectionsInstances.has(collection.id) || latestCollectionsInstances.get(collection.id).updationTime < collection.updationTime) {
|
|
|
|
latestCollectionsInstances.set(collection.id, collection);
|
|
|
|
}
|
|
|
|
});
|
2021-02-05 18:59:34 +00:00
|
|
|
let collections = [];
|
2021-02-03 08:02:54 +00:00
|
|
|
for (const [_, collection] of latestCollectionsInstances) {
|
2021-02-06 07:51:49 +00:00
|
|
|
if (!collection.isDeleted)
|
|
|
|
collections.push(collection);
|
2021-02-03 08:02:54 +00:00
|
|
|
}
|
2021-01-15 11:11:06 +00:00
|
|
|
await localForage.setItem('fav-collection', favCollection);
|
2021-02-03 08:02:54 +00:00
|
|
|
await localForage.setItem('collections', collections);
|
2021-01-31 12:10:45 +00:00
|
|
|
return updatedCollections;
|
2021-01-13 05:31:02 +00:00
|
|
|
};
|
2021-01-13 12:43:46 +00:00
|
|
|
|
2021-02-05 16:57:41 +00:00
|
|
|
export const getCollectionLatestFile = (
|
2021-01-14 11:31:20 +00:00
|
|
|
collections: collection[],
|
2021-02-05 16:57:41 +00:00
|
|
|
files: file[]
|
|
|
|
): collectionLatestFile[] => {
|
|
|
|
const latestFile = new Map<number, file>();
|
|
|
|
const collectionMap = new Map<number, collection>();
|
|
|
|
|
|
|
|
collections.forEach(collection => collectionMap.set(Number(collection.id), collection));
|
|
|
|
files.forEach(file => {
|
|
|
|
if (!latestFile.has(file.collectionID)) {
|
|
|
|
latestFile.set(file.collectionID, file)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let allCollectionLatestFile: collectionLatestFile[] = [];
|
|
|
|
for (const [collectionID, file] of latestFile) {
|
|
|
|
allCollectionLatestFile.push({ collection: collectionMap.get(collectionID), file });
|
|
|
|
}
|
|
|
|
return allCollectionLatestFile;
|
|
|
|
}
|
2021-01-14 11:31:20 +00:00
|
|
|
|
2021-01-20 12:05:04 +00:00
|
|
|
export const getFavItemIds = async (files: file[]): Promise<Set<number>> => {
|
|
|
|
|
|
|
|
let favCollection: collection = (await localForage.getItem<collection>('fav-collection'))[0];
|
|
|
|
if (!favCollection)
|
|
|
|
return new Set();
|
2021-01-20 13:04:27 +00:00
|
|
|
|
2021-01-20 12:05:04 +00:00
|
|
|
return new Set(files.filter(file => file.collectionID === Number(favCollection.id)).map((file): number => file.id));
|
|
|
|
}
|
|
|
|
|
2021-01-15 17:04:33 +00:00
|
|
|
export const createAlbum = async (albumName: string) => {
|
2021-01-15 11:11:06 +00:00
|
|
|
return AddCollection(albumName, CollectionType.album);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-21 02:24:17 +00:00
|
|
|
export const AddCollection = async (collectionName: string, type: CollectionType) => {
|
2021-01-13 12:43:46 +00:00
|
|
|
const worker = await new CryptoWorker();
|
2021-01-15 11:11:06 +00:00
|
|
|
const encryptionKey = await getActualKey();
|
|
|
|
const token = getToken();
|
2021-01-19 04:32:12 +00:00
|
|
|
const collectionKey: string = await worker.generateMasterKey();
|
2021-01-15 11:11:06 +00:00
|
|
|
const { encryptedData: encryptedKey, nonce: keyDecryptionNonce }: keyEncryptionResult = await worker.encryptToB64(collectionKey, encryptionKey);
|
2021-01-21 05:45:50 +00:00
|
|
|
const { encryptedData: encryptedName, nonce: nameDecryptionNonce }: keyEncryptionResult = await worker.encryptToB64(collectionName, collectionKey);
|
2021-01-13 12:43:46 +00:00
|
|
|
const newCollection: collection = {
|
|
|
|
id: null,
|
|
|
|
owner: null,
|
|
|
|
encryptedKey,
|
|
|
|
keyDecryptionNonce,
|
2021-01-21 02:24:17 +00:00
|
|
|
encryptedName,
|
|
|
|
nameDecryptionNonce,
|
2021-01-15 11:11:06 +00:00
|
|
|
type,
|
2021-01-13 12:43:46 +00:00
|
|
|
attributes: {},
|
|
|
|
sharees: null,
|
|
|
|
updationTime: null,
|
|
|
|
isDeleted: false
|
|
|
|
};
|
|
|
|
let createdCollection: collection = await createCollection(newCollection, token);
|
2021-01-21 02:24:17 +00:00
|
|
|
createdCollection = await getCollectionSecrets(createdCollection, encryptionKey);
|
2021-01-13 12:43:46 +00:00
|
|
|
return createdCollection;
|
|
|
|
}
|
|
|
|
|
|
|
|
const createCollection = async (collectionData: collection, token: string): Promise<collection> => {
|
2021-01-25 07:05:45 +00:00
|
|
|
try {
|
2021-01-26 06:39:10 +00:00
|
|
|
const response = await HTTPService.post(`${ENDPOINT}/collections`, collectionData, null, { 'X-Auth-Token': token });
|
2021-01-25 07:05:45 +00:00
|
|
|
return response.data.collection;
|
|
|
|
} catch (e) {
|
|
|
|
console.log("create Collection failed " + e);
|
|
|
|
}
|
2021-01-13 12:43:46 +00:00
|
|
|
}
|
2021-01-15 11:11:06 +00:00
|
|
|
|
|
|
|
export const addToFavorites = async (file: file) => {
|
|
|
|
let favCollection: collection = (await localForage.getItem<collection>('fav-collection'))[0];
|
|
|
|
if (!favCollection) {
|
|
|
|
favCollection = await AddCollection("Favorites", CollectionType.favorites);
|
|
|
|
await localForage.setItem('fav-collection', favCollection);
|
|
|
|
}
|
|
|
|
await addtoCollection(favCollection, [file])
|
|
|
|
}
|
|
|
|
|
2021-01-20 13:04:27 +00:00
|
|
|
export const removeFromFavorites = async (file: file) => {
|
|
|
|
let favCollection: collection = (await localForage.getItem<collection>('fav-collection'))[0];
|
|
|
|
await removeFromCollection(favCollection, [file])
|
|
|
|
}
|
|
|
|
|
2021-01-15 11:11:06 +00:00
|
|
|
const addtoCollection = async (collection: collection, files: file[]) => {
|
2021-01-25 07:05:45 +00:00
|
|
|
try {
|
|
|
|
const params = new Object();
|
|
|
|
const worker = await new CryptoWorker();
|
|
|
|
const token = getToken();
|
|
|
|
params["collectionID"] = collection.id;
|
|
|
|
await Promise.all(files.map(async file => {
|
|
|
|
file.collectionID = Number(collection.id);
|
|
|
|
const newEncryptedKey: keyEncryptionResult = await worker.encryptToB64(file.key, collection.key);
|
|
|
|
file.encryptedKey = newEncryptedKey.encryptedData;
|
|
|
|
file.keyDecryptionNonce = newEncryptedKey.nonce;
|
|
|
|
if (params["files"] == undefined) {
|
|
|
|
params["files"] = [];
|
|
|
|
}
|
|
|
|
params["files"].push({
|
|
|
|
id: file.id,
|
|
|
|
encryptedKey: file.encryptedKey,
|
|
|
|
keyDecryptionNonce: file.keyDecryptionNonce
|
|
|
|
})
|
|
|
|
return file;
|
|
|
|
}));
|
2021-01-26 06:39:10 +00:00
|
|
|
await HTTPService.post(`${ENDPOINT}/collections/add-files`, params, null, { 'X-Auth-Token': token });
|
2021-01-25 07:05:45 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.log("Add to collection Failed " + e);
|
|
|
|
}
|
2021-01-20 12:05:04 +00:00
|
|
|
}
|
2021-01-20 13:04:27 +00:00
|
|
|
const removeFromCollection = async (collection: collection, files: file[]) => {
|
2021-01-25 07:05:45 +00:00
|
|
|
try {
|
|
|
|
const params = new Object();
|
|
|
|
const token = getToken();
|
|
|
|
params["collectionID"] = collection.id;
|
|
|
|
await Promise.all(files.map(async file => {
|
|
|
|
if (params["fileIDs"] == undefined) {
|
|
|
|
params["fileIDs"] = [];
|
|
|
|
}
|
|
|
|
params["fileIDs"].push(file.id);
|
|
|
|
}));
|
2021-01-26 06:39:10 +00:00
|
|
|
await HTTPService.post(`${ENDPOINT}/collections/remove-files`, params, null, { 'X-Auth-Token': token });
|
2021-01-25 07:05:45 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.log("remove from collection failed " + e);
|
|
|
|
}
|
2021-01-20 13:04:27 +00:00
|
|
|
}
|
|
|
|
|