add download manager init logic and refactor

This commit is contained in:
Abhinav 2023-11-30 17:53:50 +05:30
parent 570edd32dd
commit c3304571a9
5 changed files with 35 additions and 18 deletions

View file

@ -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());

View file

@ -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 {

View file

@ -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);
}
}

View file

@ -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);

View file

@ -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