Update the file fetching APIs
This commit is contained in:
parent
a9e0821db0
commit
e2c0c381c8
3 changed files with 61 additions and 37 deletions
|
@ -2,7 +2,7 @@ import React, { useEffect, useState } from 'react';
|
|||
import { useRouter } from 'next/router';
|
||||
import Spinner from 'react-bootstrap/Spinner';
|
||||
import { getKey, SESSION_KEYS } from 'utils/storage/sessionStorage';
|
||||
import { fileData, getFiles } from 'services/fileService';
|
||||
import { file, getFiles } from 'services/fileService';
|
||||
import { getData, LS_KEYS } from 'utils/storage/localStorage';
|
||||
import PreviewCard from './components/PreviewCard';
|
||||
import { getActualKey } from 'utils/common/key';
|
||||
|
@ -24,7 +24,7 @@ const Container = styled.div`
|
|||
export default function Gallery() {
|
||||
const router = useRouter();
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [data, setData] = useState<fileData[]>();
|
||||
const [data, setData] = useState<file[]>();
|
||||
|
||||
useEffect(() => {
|
||||
const key = getKey(SESSION_KEYS.ENCRYPTION_KEY);
|
||||
|
@ -44,12 +44,12 @@ export default function Gallery() {
|
|||
|
||||
if (!data || loading) {
|
||||
return <div className="text-center">
|
||||
<Spinner animation="border" variant="primary"/>;
|
||||
<Spinner animation="border" variant="primary" />;
|
||||
</div>
|
||||
}
|
||||
|
||||
const getThumbnail = (item) => (
|
||||
<PreviewCard data={item}/>
|
||||
<PreviewCard data={item} />
|
||||
)
|
||||
|
||||
return (<Container>
|
||||
|
|
|
@ -26,14 +26,14 @@ export default function Verify() {
|
|||
const [loading, setLoading] = useState(false);
|
||||
const [resend, setResend] = useState(0);
|
||||
const router = useRouter();
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
router.prefetch('/credentials');
|
||||
router.prefetch('/generate');
|
||||
const user = getData(LS_KEYS.USER);
|
||||
if (!user?.email) {
|
||||
router.push("/");
|
||||
} else if (user.token) {
|
||||
} else if (user.token) {
|
||||
router.push("/credentials")
|
||||
} else {
|
||||
setEmail(user.email);
|
||||
|
@ -47,6 +47,7 @@ export default function Verify() {
|
|||
setData(LS_KEYS.USER, {
|
||||
email,
|
||||
token: resp.data.token,
|
||||
id: resp.data.id,
|
||||
});
|
||||
setData(LS_KEYS.KEY_ATTRIBUTES, resp.data.keyAttributes);
|
||||
if (resp.data.keyAttributes?.encryptedKey) {
|
||||
|
@ -82,7 +83,7 @@ export default function Verify() {
|
|||
<Card.Title>{constants.VERIFY_EMAIL}</Card.Title>
|
||||
{constants.EMAIL_SENT({ email })}
|
||||
{constants.CHECK_INBOX}<br />
|
||||
<br/>
|
||||
<br />
|
||||
<Formik<formValues>
|
||||
initialValues={{ ott: '' }}
|
||||
validationSchema={Yup.object().shape({
|
||||
|
@ -108,7 +109,7 @@ export default function Verify() {
|
|||
</Form.Control.Feedback>
|
||||
</Form.Group>
|
||||
<Button type="submit" block disabled={loading}>{constants.VERIFY}</Button>
|
||||
<br/>
|
||||
<br />
|
||||
{resend === 0 && <a href="#" onClick={resendEmail}>{constants.RESEND_MAIL}</a>}
|
||||
{resend === 1 && <span>{constants.SENDING}</span>}
|
||||
{resend === 2 && <span>{constants.SENT}</span>}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
import { getEndpoint } from "utils/common/apiUtil";
|
||||
import HTTPService from "./HTTPService";
|
||||
import * as Comlink from "comlink";
|
||||
import { getData, LS_KEYS } from "utils/storage/localStorage";
|
||||
import { decrypt } from "utils/crypto/libsodium";
|
||||
|
||||
const CryptoWorker: any = typeof window !== 'undefined'
|
||||
&& Comlink.wrap(new Worker("worker/crypto.worker.js", { type: 'module' }));
|
||||
|
@ -11,13 +13,21 @@ export interface fileAttribute {
|
|||
decryptionHeader: string;
|
||||
};
|
||||
|
||||
export interface user {
|
||||
id: number;
|
||||
name: string;
|
||||
email: string;
|
||||
}
|
||||
|
||||
export interface collection {
|
||||
id: number;
|
||||
ownerID: number;
|
||||
owner: user;
|
||||
key: string;
|
||||
name: string;
|
||||
type: string;
|
||||
creationTime: number;
|
||||
encryptedKey: string;
|
||||
keyDecryptionNonce: string;
|
||||
}
|
||||
|
||||
export interface file {
|
||||
|
@ -34,50 +44,63 @@ export interface file {
|
|||
h: number;
|
||||
};
|
||||
|
||||
const getCollectionKeyUsingWorker = async (collection: any, key: Uint8Array) => {
|
||||
const getCollectionKey = async (collection: collection, key: Uint8Array) => {
|
||||
const worker = await new CryptoWorker();
|
||||
const collectionKey = await worker.decrypt(
|
||||
await worker.fromB64(collection.encryptedKey),
|
||||
await worker.fromB64(collection.keyDecryptionNonce),
|
||||
key);
|
||||
const userID = getData(LS_KEYS.USER).id;
|
||||
var decryptedKey;
|
||||
if (collection.owner.id == userID) {
|
||||
decryptedKey = await worker.decrypt(
|
||||
await worker.fromB64(collection.encryptedKey),
|
||||
await worker.fromB64(collection.keyDecryptionNonce),
|
||||
key);
|
||||
} else {
|
||||
// TODO
|
||||
decryptedKey = null;
|
||||
}
|
||||
return {
|
||||
...collection,
|
||||
key: collectionKey
|
||||
key: decryptedKey
|
||||
};
|
||||
}
|
||||
|
||||
const getCollections = async (token: string, key: Uint8Array): Promise<collection[]> => {
|
||||
const resp = await HTTPService.get(`${ENDPOINT}/collections/owned`, {
|
||||
token
|
||||
const getCollections = async (token: string, sinceTime: string, key: Uint8Array): Promise<collection[]> => {
|
||||
const resp = await HTTPService.get(`http://localhost/collections/`, {
|
||||
'token': token,
|
||||
'sinceTime': sinceTime,
|
||||
});
|
||||
|
||||
const promises: Promise<collection>[] = resp.data.collections.map(
|
||||
(collection: collection) => getCollectionKeyUsingWorker(collection, key));
|
||||
(collection: collection) => getCollectionKey(collection, key));
|
||||
return await Promise.all(promises);
|
||||
}
|
||||
|
||||
export const getFiles = async (sinceTime: string, token: string, limit: string, key: string) => {
|
||||
const worker = await new CryptoWorker();
|
||||
|
||||
const collections = await getCollections(token, await worker.fromB64(key));
|
||||
const collectionMap = {}
|
||||
for (const collectionIndex in collections) {
|
||||
collectionMap[collections[collectionIndex].id] = collections[collectionIndex];
|
||||
}
|
||||
const resp = await HTTPService.get(`${ENDPOINT}/files/diff`, {
|
||||
sinceTime, token, limit,
|
||||
});
|
||||
|
||||
const promises: Promise<file>[] = resp.data.diff.map(
|
||||
async (file: file) => {
|
||||
file.key = await worker.decrypt(
|
||||
await worker.fromB64(file.encryptedKey),
|
||||
await worker.fromB64(file.keyDecryptionNonce),
|
||||
collectionMap[file.collectionID].key)
|
||||
file.metadata = await worker.decryptMetadata(file);
|
||||
return file;
|
||||
const collections = await getCollections(token, "0", await worker.fromB64(key));
|
||||
var files: Array<file> = [];
|
||||
for (const index in collections) {
|
||||
const collection = collections[index];
|
||||
if (collection.key == null) {
|
||||
continue;
|
||||
}
|
||||
const resp = await HTTPService.get(`${ENDPOINT}/collections/diff/`, {
|
||||
'collectionID': collection.id.toString(), sinceTime, token, limit,
|
||||
});
|
||||
return await Promise.all(promises);
|
||||
|
||||
const promises: Promise<file>[] = resp.data.diff.map(
|
||||
async (file: file) => {
|
||||
file.key = await worker.decrypt(
|
||||
await worker.fromB64(file.encryptedKey),
|
||||
await worker.fromB64(file.keyDecryptionNonce),
|
||||
collection.key)
|
||||
file.metadata = await worker.decryptMetadata(file);
|
||||
return file;
|
||||
});
|
||||
files.push(...await Promise.all(promises));
|
||||
console.log(files);
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
export const getPreview = async (token: string, file: file) => {
|
||||
|
|
Loading…
Add table
Reference in a new issue