Skip to content

armgit / ente

Public Imported from GitHub
0

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

Use for HLS gen

Manav Rathicc5b2697d94c
diff --git a/web/packages/gallery/services/download.ts b/web/packages/gallery/services/download.ts
index b71ef57854..4926ae14de 100644
--- a/web/packages/gallery/services/download.ts
+++ b/web/packages/gallery/services/download.ts
@@ -14,7 +14,7 @@ import {
} from "ente-base/http";
import { ensureAuthToken } from "ente-base/local-user";
import log from "ente-base/log";
-import { customAPIOrigin } from "ente-base/origins";
+import { apiURL, customAPIOrigin } from "ente-base/origins";
import type { EnteFile } from "ente-media/file";
import { FileType } from "ente-media/file-type";
import { decodeLivePhoto } from "ente-media/live-photo";
@@ -359,6 +359,7 @@ class DownloadManager {
*/
async fileStream(
file: EnteFile,
+ opts?: FileDownloadOpts,
): Promise<ReadableStream<Uint8Array> | null> {
const cachedURL = this.fileURLPromises.get(file.id);
if (cachedURL) {
@@ -372,7 +373,7 @@ class DownloadManager {
}
}
- return this.downloadFile(file);
+ return this.downloadFile(file, opts);
}
/**
@@ -397,10 +398,11 @@ class DownloadManager {
private async downloadFile(
file: EnteFile,
+ opts?: FileDownloadOpts,
): Promise<ReadableStream<Uint8Array> | null> {
log.info(`download attempted for file id ${file.id}`);
- const res = await wrapErrors(() => this._downloadFile(file));
+ const res = await wrapErrors(() => this._downloadFile(file, opts));
if (
file.metadata.fileType === FileType.image ||
@@ -505,14 +507,19 @@ class DownloadManager {
});
}
- private async _downloadFile(file: EnteFile) {
+ /**
+ * Download the full contents of {@link file}, automatically choosing the
+ * credentials for the logged in user or the public albums depending on the
+ * current app context we are in.
+ */
+ private async _downloadFile(file: EnteFile, opts?: FileDownloadOpts) {
if (this.publicAlbumsCredentials) {
return publicAlbums_downloadFile(
file,
this.publicAlbumsCredentials,
);
} else {
- return photos_downloadFile(file);
+ return photos_downloadFile(file, opts);
}
}
@@ -678,10 +685,28 @@ const photos_downloadThumbnail = async (file: EnteFile) => {
return new Uint8Array(await res.arrayBuffer());
};
+interface FileDownloadOpts {
+ /**
+ * `true` if the request is for a background task. These are considered less
+ * latency sensitive than user initiated interactive requests.
+ *
+ * See: [Note: User initiated vs background downloads of files].
+ *
+ * This parameter is ignored for requests made when using public albums
+ * credentials to download files; those are always considered interactive.
+ */
+ background?: boolean;
+}
+
+/**
+ * Download the full contents of the given {@link EnteFile}
+ */
const photos_downloadFile = async (
file: EnteFile,
- userInitiated: boolean,
+ opts?: FileDownloadOpts,
): Promise<Response> => {
+ const { background } = opts ?? {};
+
const customOrigin = await customAPIOrigin();
// [Note: Passing credentials for self-hosted file fetches]
@@ -719,6 +744,8 @@ const photos_downloadFile = async (
// to get the pre-signed URL, and (b) fetch that pre-signed URL and
// stream back the response.
//
+ // [Note: User initiated vs background downloads of files]
+ //
// The faster proxy approach is used for interactive requests to reduce the
// latency for the user (e.g. when the user is waiting to see a full
// resolution file). It can be faster than a direct connection as the proxy
@@ -728,13 +755,10 @@ const photos_downloadFile = async (
// S3 connection (as what'd happen when self hosting) gets used.
const getFile = async () => {
- if (customOrigin) {
+ if (customOrigin || background) {
const token = await ensureAuthToken();
- const params = new URLSearchParams({ token });
- return fetch(
- `${customOrigin}/files/download/${file.id}?${params.toString()}`,
- { headers: publicRequestHeaders() },
- );
+ const url = await apiURL(`/files/download/${file.id}`, { token });
+ return fetch(url, { headers: publicRequestHeaders() });
} else {
return fetch(`https://files.ente.io/?fileID=${file.id}`, {
headers: await authenticatedRequestHeaders(),
diff --git a/web/packages/gallery/services/video.ts b/web/packages/gallery/services/video.ts
index 1db8315a09..1c02011467 100644
--- a/web/packages/gallery/services/video.ts
+++ b/web/packages/gallery/services/video.ts
@@ -969,7 +969,9 @@ const processQueueItem = async ({
uploadItem;
if (!sourceVideo) {
try {
- sourceVideo = (await downloadManager.fileStream(file))!;
+ sourceVideo = (await downloadManager.fileStream(file, {
+ background: true,
+ }))!;
} catch (e) {
if (!isNetworkDownloadError(e)) await markFailedVideoFile(file);
throw e;