Rearrange

This commit is contained in:
Manav Rathi 2024-05-04 14:18:44 +05:30
parent bf66697bcb
commit 80301b14f4
No known key found for this signature in database

View file

@ -91,14 +91,17 @@ export const renderableImageURLs = async function* (castData: CastData) {
const { collectionKey, castToken } = castData;
let previousURL: string | undefined;
while (true) {
const collection = await getCollection(castToken, collectionKey);
const collection = await getCastCollection(castToken, collectionKey);
await syncPublicFiles(castToken, collection, () => {});
const allFiles = await getLocalFiles(String(collection.id));
const files = allFiles.filter((file) => isFileEligibleForCast(file));
const files = await getLocalFiles(String(collection.id));
if (!files.length) return;
let haveEligibleFiles = false;
for (const file of files) {
if (!isFileEligibleForCast(file)) continue;
haveEligibleFiles = true;
if (!previousURL) {
previousURL = await createRenderableURL(castToken, file);
continue;
@ -109,9 +112,57 @@ export const renderableImageURLs = async function* (castData: CastData) {
previousURL = url;
yield urls;
}
// This collection does not have any files that we can show.
if (!haveEligibleFiles) return;
}
};
const getCastCollection = async (
castToken: string,
collectionKey: string,
): Promise<Collection> => {
const resp = await HTTPService.get(`${ENDPOINT}/cast/info`, null, {
"Cache-Control": "no-cache",
"X-Cast-Access-Token": castToken,
});
let collection = resp.data.collection;
const cryptoWorker = await ComlinkCryptoWorker.getInstance();
const collectionName =
collection.name ||
(await cryptoWorker.decryptToUTF8(
collection.encryptedName,
collection.nameDecryptionNonce,
collectionKey,
));
let collectionPublicMagicMetadata: CollectionPublicMagicMetadata;
if (collection.pubMagicMetadata?.data) {
collectionPublicMagicMetadata = {
...collection.pubMagicMetadata,
data: await cryptoWorker.decryptMetadata(
collection.pubMagicMetadata.data,
collection.pubMagicMetadata.header,
collectionKey,
),
};
}
collection = {
...collection,
name: collectionName,
key: collectionKey,
pubMagicMetadata: collectionPublicMagicMetadata,
};
await saveCollection(collection);
return collection;
};
const getLastSyncKey = (collectionUID: string) => `${collectionUID}-time`;
export const getLocalFiles = async (
@ -317,53 +368,6 @@ const fetchFiles = async (
}
};
const getCollection = async (
castToken: string,
collectionKey: string,
): Promise<Collection> => {
try {
const resp = await HTTPService.get(`${ENDPOINT}/cast/info`, null, {
"Cache-Control": "no-cache",
"X-Cast-Access-Token": castToken,
});
const fetchedCollection = resp.data.collection;
const cryptoWorker = await ComlinkCryptoWorker.getInstance();
const collectionName = (fetchedCollection.name =
fetchedCollection.name ||
(await cryptoWorker.decryptToUTF8(
fetchedCollection.encryptedName,
fetchedCollection.nameDecryptionNonce,
collectionKey,
)));
let collectionPublicMagicMetadata: CollectionPublicMagicMetadata;
if (fetchedCollection.pubMagicMetadata?.data) {
collectionPublicMagicMetadata = {
...fetchedCollection.pubMagicMetadata,
data: await cryptoWorker.decryptMetadata(
fetchedCollection.pubMagicMetadata.data,
fetchedCollection.pubMagicMetadata.header,
collectionKey,
),
};
}
const collection = {
...fetchedCollection,
name: collectionName,
key: collectionKey,
pubMagicMetadata: collectionPublicMagicMetadata,
};
await saveCollection(collection);
return collection;
} catch (e) {
log.error("failed to get cast collection", e);
throw e;
}
};
export function sortFiles(files: EnteFile[], sortAsc = false) {
// sort based on the time of creation time of the file,
// for files with same creation time, sort based on the time of last modification