Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

add download manager init logic and refactor

Abhinavc3304571a907
diff --git a/apps/photos/src/pages/gallery/index.tsx b/apps/photos/src/pages/gallery/index.tsx
index 1eeb3e165b..5e4aa9bb47 100644
--- a/apps/photos/src/pages/gallery/index.tsx
+++ b/apps/photos/src/pages/gallery/index.tsx
@@ -129,6 +129,8 @@ import InMemoryStore, { MS_KEYS } from '@ente/shared/storage/InMemoryStore';
import { syncEmbeddings } from 'services/embeddingService';
import { ClipService } from 'services/clipService';
import isElectron from 'is-electron';
+import downloadManager from 'services/downloadManager';
+import { APPS } from '@ente/shared/apps/constants';
export const DeadCenter = styled('div')`
flex: 1;
@@ -296,7 +298,8 @@ export default function Gallery() {
useEffect(() => {
appContext.showNavBar(true);
const key = getKey(SESSION_KEYS.ENCRYPTION_KEY);
- if (!key) {
+ const token = getToken();
+ if (!key || !token) {
InMemoryStore.set(MS_KEYS.REDIRECT_URL, PAGES.GALLERY);
router.push(PAGES.ROOT);
return;
@@ -307,6 +310,7 @@ export default function Gallery() {
if (!valid) {
return;
}
+ downloadManager.init(APPS.PHOTOS, token);
setupSelectAllKeyBoardShortcutHandler();
setActiveCollectionID(ALL_SECTION);
setIsFirstLoad(isFirstLogin());
diff --git a/apps/photos/src/pages/shared-albums/index.tsx b/apps/photos/src/pages/shared-albums/index.tsx
index 4a521a0ace..775f799910 100644
--- a/apps/photos/src/pages/shared-albums/index.tsx
+++ b/apps/photos/src/pages/shared-albums/index.tsx
@@ -58,6 +58,8 @@ import MoreHoriz from '@mui/icons-material/MoreHoriz';
import OverflowMenu from '@ente/shared/components/OverflowMenu/menu';
import { OverflowMenuOption } from '@ente/shared/components/OverflowMenu/option';
import { ENTE_WEBSITE_LINK } from '@ente/shared/constants/urls';
+import { APPS } from '@ente/shared/apps/constants';
+import downloadManager from 'services/downloadManager';
const defaultThumbStore = new Map();
const defaultFileStore = new Map();
@@ -195,6 +197,10 @@ export default function PublicCollectionGallery() {
setPublicFiles(localPublicFiles);
passwordJWTToken.current =
await getLocalPublicCollectionPassword(collectionUID);
+ downloadManager.init(APPS.PHOTOS, {
+ token: token.current,
+ passwordToken: passwordJWTToken.current,
+ });
}
await syncWithRemote();
} finally {
diff --git a/apps/photos/src/services/downloadManager.ts b/apps/photos/src/services/downloadManager.ts
index cb0cf87a5a..d49cb8b72a 100644
--- a/apps/photos/src/services/downloadManager.ts
+++ b/apps/photos/src/services/downloadManager.ts
@@ -56,16 +56,10 @@ class DownloadManager {
async init(
app: APPS,
- token: string,
- passwordToken?: string,
+ tokens: { token: string; passwordToken?: string } | { token: string },
timeout?: number
) {
- this.downloadClient = createDownloadClient(
- app,
- token,
- passwordToken,
- timeout
- );
+ this.downloadClient = createDownloadClient(app, tokens, timeout);
this.thumbnailCache = await openThumbnailCache();
this.cryptoWorker = await ComlinkCryptoWorker.getInstance();
}
@@ -405,13 +399,20 @@ async function openThumbnailCache() {
function createDownloadClient(
app: APPS,
- token: string,
- passwordToken?: string,
+ tokens: { token: string; passwordToken?: string } | { token: string },
timeout?: number
): DownloadClient {
+ if (!timeout) {
+ timeout = 300000; // 5 minute
+ }
if (app === APPS.ALBUMS) {
+ const { token, passwordToken } = tokens as {
+ token: string;
+ passwordToken: string;
+ };
return new PublicAlbumsDownloadClient(token, passwordToken, timeout);
} else {
+ const { token } = tokens;
return new PhotosDownloadClient(token, timeout);
}
}
diff --git a/apps/photos/src/services/downloadManagerClients/photos.ts b/apps/photos/src/services/downloadManagerClients/photos.ts
index 38dab41cf4..da77b45f30 100644
--- a/apps/photos/src/services/downloadManagerClients/photos.ts
+++ b/apps/photos/src/services/downloadManagerClients/photos.ts
@@ -6,7 +6,7 @@ import { CustomError } from '@ente/shared/error';
import { retryAsyncFunction } from 'utils/network';
export class PhotosDownloadClient implements DownloadClient {
- constructor(private token: string, private timeout) {}
+ constructor(private token: string, private timeout: number) {}
updateTokens(token: string) {
this.token = token;
}
@@ -15,15 +15,17 @@ export class PhotosDownloadClient implements DownloadClient {
this.timeout = timeout;
}
- async downloadThumbnail(file: EnteFile) {
+ async downloadThumbnail(file: EnteFile): Promise<Uint8Array> {
if (!this.token) {
return;
}
- const resp = await HTTPService.get(
- getThumbnailURL(file.id),
- null,
- { 'X-Auth-Token': this.token },
- { responseType: 'arraybuffer', timeout: this.timeout }
+ const resp = await retryAsyncFunction(() =>
+ HTTPService.get(
+ getThumbnailURL(file.id),
+ null,
+ { 'X-Auth-Token': this.token },
+ { responseType: 'arraybuffer', timeout: this.timeout }
+ )
);
if (typeof resp.data === 'undefined') {
throw Error(CustomError.REQUEST_FAILED);
diff --git a/apps/photos/src/services/machineLearning/machineLearningService.ts b/apps/photos/src/services/machineLearning/machineLearningService.ts
index ffd59144b8..db777c2f50 100644
--- a/apps/photos/src/services/machineLearning/machineLearningService.ts
+++ b/apps/photos/src/services/machineLearning/machineLearningService.ts
@@ -33,6 +33,9 @@ import ObjectService from './objectService';
import ReaderService from './readerService';
import { logError } from '@ente/shared/sentry';
import { addLogLine } from '@ente/shared/logging';
+import downloadManager from 'services/downloadManager';
+import { APPS } from '@ente/shared/apps/constants';
+
class MachineLearningService {
private initialized = false;
// private faceDetectionService: FaceDetectionService;
@@ -60,6 +63,7 @@ class MachineLearningService {
throw Error('Token needed by ml service to sync file');
}
+ downloadManager.init(APPS.PHOTOS, { token });
// await this.init();
// Used to debug tf memory leak, all tf memory