ente/src/services/fileService.ts

170 lines
4.9 KiB
TypeScript
Raw Normal View History

import { getEndpoint } from 'utils/common/apiUtil';
import HTTPService from './HTTPService';
import * as Comlink from 'comlink';
import localForage from 'localforage';
import { collection } from './collectionService';
2021-02-16 09:44:25 +00:00
import { MetadataObject } from './uploadService';
2020-09-27 17:18:57 +00:00
const CryptoWorker: any =
typeof window !== 'undefined' &&
Comlink.wrap(new Worker('worker/crypto.worker.js', { type: 'module' }));
const ENDPOINT = getEndpoint();
2021-02-24 16:05:26 +00:00
const DIFF_LIMIT: number = 2500;
2020-11-20 08:47:20 +00:00
localForage.config({
driver: localForage.INDEXEDDB,
name: 'ente-files',
version: 1.0,
storeName: 'files',
2020-11-20 08:47:20 +00:00
});
2021-02-09 05:43:05 +00:00
const FILES = 'files';
2020-10-19 03:01:34 +00:00
export interface fileAttribute {
2021-02-16 09:44:25 +00:00
encryptedData?: Uint8Array;
objectKey?: string;
decryptionHeader: string;
}
2020-10-19 03:01:34 +00:00
2020-11-07 11:10:49 +00:00
2020-10-19 03:01:34 +00:00
export interface file {
id: number;
collectionID: number;
file: fileAttribute;
thumbnail: fileAttribute;
2021-02-16 09:44:25 +00:00
metadata: MetadataObject;
encryptedKey: string;
keyDecryptionNonce: string;
2021-01-18 02:26:34 +00:00
key: string;
src: string;
msrc: string;
html: string;
w: number;
h: number;
isDeleted: boolean;
dataIndex: number;
updationTime: number;
}
export const syncData = async (token, collections) => {
const { files: resp, isUpdated } = await syncFiles(token, collections);
return {
data: resp.map((item) => ({
...item,
w: window.innerWidth,
h: window.innerHeight,
})),
isUpdated,
};
};
export const localFiles = async () => {
2021-02-09 05:43:05 +00:00
let files: Array<file> = (await localForage.getItem<file[]>(FILES)) || [];
return files;
};
export const syncFiles = async (token: string, collections: collection[]) => {
let files = await localFiles();
let isUpdated = false;
2021-02-09 05:07:46 +00:00
files = await removeDeletedCollectionFiles(collections, files);
for (let collection of collections) {
const lastSyncTime =
(await localForage.getItem<number>(`${collection.id}-time`)) ?? 0;
if (collection.updationTime === lastSyncTime) {
2021-02-08 07:33:46 +00:00
continue;
2021-02-08 11:01:05 +00:00
}
isUpdated = true;
let fetchedFiles =
2021-02-24 16:05:26 +00:00
(await getFiles(collection, lastSyncTime, DIFF_LIMIT, token)) ?? [];
2021-02-08 10:56:47 +00:00
files.push(...fetchedFiles);
var latestVersionFiles = new Map<number, file>();
files.forEach((file) => {
if (
!latestVersionFiles.has(file.id) ||
latestVersionFiles.get(file.id).updationTime < file.updationTime
) {
2021-02-08 10:56:47 +00:00
latestVersionFiles.set(file.id, file);
}
});
files = [];
for (const [_, file] of latestVersionFiles) {
if (file.isDeleted) {
2021-02-08 10:56:47 +00:00
continue;
}
files.push(file);
2021-02-06 07:54:36 +00:00
}
2021-02-08 10:56:47 +00:00
files = files.sort(
(a, b) => b.metadata.creationTime - a.metadata.creationTime
);
await localForage.setItem('files', files);
await localForage.setItem(
`${collection.id}-time`,
collection.updationTime
);
}
return { files, isUpdated };
};
export const getFiles = async (
collection: collection,
2021-02-09 05:07:46 +00:00
sinceTime: number,
limit: number,
token: string
): Promise<file[]> => {
2021-01-25 07:05:45 +00:00
try {
const worker = await new CryptoWorker();
let promises: Promise<file>[] = [];
let time =
sinceTime ||
2021-02-09 05:07:46 +00:00
(await localForage.getItem<number>(`${collection.id}-time`)) ||
0;
let resp;
do {
resp = await HTTPService.get(
`${ENDPOINT}/collections/diff`,
{
2021-02-17 08:35:19 +00:00
collectionID: collection.id,
sinceTime: time,
limit: limit,
},
{
'X-Auth-Token': token,
}
);
promises.push(
...resp.data.diff.map(async (file: file) => {
if (!file.isDeleted) {
file.key = await worker.decryptB64(
file.encryptedKey,
file.keyDecryptionNonce,
collection.key
);
file.metadata = await worker.decryptMetadata(file);
}
return file;
})
);
if (resp.data.diff.length) {
2021-02-17 08:35:19 +00:00
time = resp.data.diff.slice(-1)[0].updationTime;
}
} while (resp.data.diff.length === limit);
return await Promise.all(promises);
2021-01-25 07:05:45 +00:00
} catch (e) {
2021-02-24 16:05:26 +00:00
console.log('Get files failed', e);
}
};
const removeDeletedCollectionFiles = async (
collections: collection[],
files: file[]
) => {
const syncedCollectionIds = new Set<number>();
for (let collection of collections) {
syncedCollectionIds.add(collection.id);
}
files = files.filter((file) => syncedCollectionIds.has(file.collectionID));
2021-02-09 05:07:46 +00:00
return files;
};