This commit is contained in:
Manav Rathi 2024-04-13 08:52:37 +05:30
parent b32c13fe31
commit 4055f02a1d
No known key found for this signature in database
2 changed files with 16 additions and 20 deletions

View file

@ -99,6 +99,13 @@ class DownloadManagerImpl {
eventBus.on(Events.LOGOUT, this.logoutHandler.bind(this), this);
}
private ensureInitialized() {
if (!this.ready)
throw new Error(
"Attempting to use an uninitialized download manager",
);
}
private async logoutHandler() {
try {
log.info("downloadManger logoutHandler started");
@ -154,27 +161,21 @@ class DownloadManagerImpl {
};
async getThumbnail(file: EnteFile, localOnly = false) {
if (!this.ready) {
throw Error(CustomError.DOWNLOAD_MANAGER_NOT_READY);
}
this.ensureInitialized();
const key = `${file.id}`;
const cachedThumb = await this.thumbnailCache.get(key);
if (cachedThumb) {
return new Uint8Array(await cachedThumb.arrayBuffer());
}
if (localOnly) {
return null;
}
const cached = await this.thumbnailCache.get(key);
if (cached) return new Uint8Array(await cached.arrayBuffer());
if (localOnly) return null;
const thumb = await this.downloadThumb(file);
this.thumbnailCache?.put2(key, new Blob([thumb]));
return thumb;
}
async getThumbnailForPreview(file: EnteFile, localOnly = false) {
this.ensureInitialized();
try {
if (!this.ready) {
throw Error(CustomError.DOWNLOAD_MANAGER_NOT_READY);
}
if (!this.thumbnailObjectURLPromises.has(file.id)) {
const thumbPromise = this.getThumbnail(file, localOnly);
const thumbURLPromise = thumbPromise.then(
@ -199,10 +200,8 @@ class DownloadManagerImpl {
file: EnteFile,
forceConvert = false,
): Promise<SourceURLs> => {
this.ensureInitialized();
try {
if (!this.ready) {
throw Error(CustomError.DOWNLOAD_MANAGER_NOT_READY);
}
const getFileForPreviewPromise = async () => {
const fileBlob = await new Response(
await this.getFile(file, true),
@ -237,10 +236,8 @@ class DownloadManagerImpl {
file: EnteFile,
cacheInMemory = false,
): Promise<ReadableStream<Uint8Array>> {
this.ensureInitialized();
try {
if (!this.ready) {
throw Error(CustomError.DOWNLOAD_MANAGER_NOT_READY);
}
const getFilePromise = async (): Promise<SourceURLs> => {
const fileStream = await this.downloadFile(file);
const fileBlob = await new Response(fileStream).blob();

View file

@ -86,7 +86,6 @@ export const CustomError = {
UNSUPPORTED_PLATFORM: "Unsupported platform",
MODEL_DOWNLOAD_PENDING:
"Model download pending, skipping clip search request",
DOWNLOAD_MANAGER_NOT_READY: "Download manager not initialized",
UPDATE_URL_FILE_ID_MISMATCH: "update url file id mismatch",
URL_ALREADY_SET: "url already set",
FILE_CONVERSION_FAILED: "file conversion failed",