Retain older behaviour for default fetches

This commit is contained in:
Manav Rathi
2024-06-04 13:52:03 +05:30
parent 515c28530e
commit d44b951f0d
2 changed files with 51 additions and 32 deletions

View File

@@ -1,6 +1,6 @@
import { CustomError } from "@ente/shared/error";
import HTTPService from "@ente/shared/network/HTTPService";
import { fileURL, thumbnailURL } from "@ente/shared/network/api";
import { customAPIOrigin, thumbnailURL } from "@ente/shared/network/api";
import { retryAsyncFunction } from "@ente/shared/utils";
import { DownloadClient } from "services/download";
import { EnteFile } from "types/file";
@@ -42,18 +42,31 @@ export class PhotosDownloadClient implements DownloadClient {
if (!token) throw Error(CustomError.TOKEN_MISSING);
// See: [Note: Passing credentials for self-hosted file fetches]
const params = new URLSearchParams({ token });
const getFile = () =>
HTTPService.get(
`${fileURL(file.id)}?${params.toString()}`,
undefined,
undefined,
{
responseType: "arraybuffer",
timeout: this.timeout,
onDownloadProgress,
},
);
const getFile = () => {
const opts = {
responseType: "arraybuffer",
timeout: this.timeout,
onDownloadProgress,
};
const customOrigin = customAPIOrigin();
if (customOrigin) {
const params = new URLSearchParams({ token });
return HTTPService.get(
`${customOrigin}/files/download/${file.id}?${params.toString()}`,
undefined,
undefined,
opts,
);
} else {
return HTTPService.get(
`https://files.ente.io/?fileID=${file.id}`,
undefined,
{ "X-Auth-Token": token },
opts,
);
}
};
const resp = await retryAsyncFunction(getFile);
if (resp.data === undefined) throw Error(CustomError.REQUEST_FAILED);
@@ -100,8 +113,21 @@ export class PhotosDownloadClient implements DownloadClient {
// credentials to get the pre signed URL, and (b) fetch that pre
// signed URL and stream back the response.
const params = new URLSearchParams({ token });
const getFile = () => fetch(`${fileURL(file.id)}?${params.toString()}`);
const getFile = () => {
const customOrigin = customAPIOrigin();
if (customOrigin) {
const params = new URLSearchParams({ token });
return fetch(
`${customOrigin}/files/download/${file.id}?${params.toString()}`,
);
} else {
return fetch(`https://files.ente.io/?fileID=${file.id}`, {
headers: {
"X-Auth-Token": token,
},
});
}
};
return retryAsyncFunction(getFile);
}

View File

@@ -6,8 +6,16 @@
* overridden when self hosting by setting the `NEXT_PUBLIC_ENTE_ENDPOINT`
* environment variable.
*/
export const apiOrigin = () =>
process.env.NEXT_PUBLIC_ENTE_ENDPOINT || "https://api.ente.io";
export const apiOrigin = () => customAPIOrigin() || "https://api.ente.io";
/**
* Return the overridden API origin, if one is defined by setting the
* `NEXT_PUBLIC_ENTE_ENDPOINT` environment variable to a non-empty value.
*
* Otherwise return undefined.
*/
export const customAPIOrigin = () =>
process.env.NEXT_PUBLIC_ENTE_ENDPOINT || undefined;
/** Deprecated, use {@link apiOrigin} instead. */
export const getEndpoint = () => {
@@ -18,21 +26,6 @@ export const getEndpoint = () => {
return "https://api.ente.io";
};
/**
* Return the URL that should be used for fetching a file with the given
* {@link id}.
*
* This defaults to a URL on Ente's own servers, but can be overridden when self
* hosting by setting the `NEXT_PUBLIC_ENTE_ENDPOINT` environment variable.
*/
export const fileURL = (id: number) => {
const endpoint = process.env.NEXT_PUBLIC_ENTE_ENDPOINT;
if (endpoint) {
return `${endpoint}/files/download/${id}`;
}
return `https://files.ente.io/?fileID=${id}`;
};
/**
* Return the URL that should be used for fetching the thumbnail for a file with
* the given {@link id}.