mirror of
https://github.com/ente-io/ente.git
synced 2025-08-08 15:30:40 +00:00
publ albums
This commit is contained in:
parent
21d3d55bf5
commit
13c64e0cce
@ -1,9 +1,6 @@
|
|||||||
import { CustomError } from "@ente/shared/error";
|
import { CustomError } from "@ente/shared/error";
|
||||||
import HTTPService from "@ente/shared/network/HTTPService";
|
import HTTPService from "@ente/shared/network/HTTPService";
|
||||||
import {
|
import { customAPIOrigin } from "@ente/shared/network/api";
|
||||||
getPublicCollectionFileURL,
|
|
||||||
getPublicCollectionThumbnailURL,
|
|
||||||
} from "@ente/shared/network/api";
|
|
||||||
import { retryAsyncFunction } from "@ente/shared/utils";
|
import { retryAsyncFunction } from "@ente/shared/utils";
|
||||||
import { DownloadClient } from "services/download";
|
import { DownloadClient } from "services/download";
|
||||||
import { EnteFile } from "types/file";
|
import { EnteFile } from "types/file";
|
||||||
@ -21,24 +18,45 @@ export class PublicAlbumsDownloadClient implements DownloadClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
downloadThumbnail = async (file: EnteFile) => {
|
downloadThumbnail = async (file: EnteFile) => {
|
||||||
if (!this.token) {
|
const accessToken = this.token;
|
||||||
throw Error(CustomError.TOKEN_MISSING);
|
const accessTokenJWT = this.passwordToken;
|
||||||
}
|
if (!accessToken) throw Error(CustomError.TOKEN_MISSING);
|
||||||
const resp = await HTTPService.get(
|
|
||||||
getPublicCollectionThumbnailURL(file.id),
|
|
||||||
null,
|
|
||||||
{
|
|
||||||
"X-Auth-Access-Token": this.token,
|
|
||||||
...(this.passwordToken && {
|
|
||||||
"X-Auth-Access-Token-JWT": this.passwordToken,
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
{ responseType: "arraybuffer" },
|
|
||||||
);
|
|
||||||
|
|
||||||
if (typeof resp.data === "undefined") {
|
// See: [Note: Passing credentials for self-hosted file fetches]
|
||||||
throw Error(CustomError.REQUEST_FAILED);
|
const getThumbnail = () => {
|
||||||
}
|
const opts = {
|
||||||
|
responseType: "arraybuffer",
|
||||||
|
};
|
||||||
|
|
||||||
|
const customOrigin = customAPIOrigin();
|
||||||
|
if (customOrigin) {
|
||||||
|
const params = new URLSearchParams({
|
||||||
|
accessToken,
|
||||||
|
...(accessTokenJWT && { accessTokenJWT }),
|
||||||
|
});
|
||||||
|
return HTTPService.get(
|
||||||
|
`${customOrigin}/public-collection/files/preview/${file.id}?${params.toString()}`,
|
||||||
|
undefined,
|
||||||
|
undefined,
|
||||||
|
opts,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return HTTPService.get(
|
||||||
|
`https://public-albums.ente.io/preview/?fileID=${file.id}`,
|
||||||
|
undefined,
|
||||||
|
{
|
||||||
|
"X-Auth-Access-Token": accessToken,
|
||||||
|
...(accessTokenJWT && {
|
||||||
|
"X-Auth-Access-Token-JWT": accessTokenJWT,
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
opts,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const resp = await getThumbnail();
|
||||||
|
if (resp.data === undefined) throw Error(CustomError.REQUEST_FAILED);
|
||||||
return new Uint8Array(resp.data);
|
return new Uint8Array(resp.data);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -46,46 +64,81 @@ export class PublicAlbumsDownloadClient implements DownloadClient {
|
|||||||
file: EnteFile,
|
file: EnteFile,
|
||||||
onDownloadProgress: (event: { loaded: number; total: number }) => void,
|
onDownloadProgress: (event: { loaded: number; total: number }) => void,
|
||||||
) => {
|
) => {
|
||||||
if (!this.token) {
|
const accessToken = this.token;
|
||||||
throw Error(CustomError.TOKEN_MISSING);
|
const accessTokenJWT = this.passwordToken;
|
||||||
}
|
if (!accessToken) throw Error(CustomError.TOKEN_MISSING);
|
||||||
const resp = await retryAsyncFunction(() =>
|
|
||||||
HTTPService.get(
|
|
||||||
getPublicCollectionFileURL(file.id),
|
|
||||||
null,
|
|
||||||
{
|
|
||||||
"X-Auth-Access-Token": this.token,
|
|
||||||
...(this.passwordToken && {
|
|
||||||
"X-Auth-Access-Token-JWT": this.passwordToken,
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
responseType: "arraybuffer",
|
|
||||||
timeout: this.timeout,
|
|
||||||
onDownloadProgress,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
if (typeof resp.data === "undefined") {
|
// See: [Note: Passing credentials for self-hosted file fetches]
|
||||||
throw Error(CustomError.REQUEST_FAILED);
|
const getFile = () => {
|
||||||
}
|
const opts = {
|
||||||
|
responseType: "arraybuffer",
|
||||||
|
timeout: this.timeout,
|
||||||
|
onDownloadProgress,
|
||||||
|
};
|
||||||
|
|
||||||
|
const customOrigin = customAPIOrigin();
|
||||||
|
if (customOrigin) {
|
||||||
|
const params = new URLSearchParams({
|
||||||
|
accessToken,
|
||||||
|
...(accessTokenJWT && { accessTokenJWT }),
|
||||||
|
});
|
||||||
|
return HTTPService.get(
|
||||||
|
`${customOrigin}/public-collection/files/download/${file.id}?${params.toString()}`,
|
||||||
|
undefined,
|
||||||
|
undefined,
|
||||||
|
opts,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return HTTPService.get(
|
||||||
|
`https://public-albums.ente.io/download/?fileID=${file.id}`,
|
||||||
|
undefined,
|
||||||
|
{
|
||||||
|
"X-Auth-Access-Token": accessToken,
|
||||||
|
...(accessTokenJWT && {
|
||||||
|
"X-Auth-Access-Token-JWT": accessTokenJWT,
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
opts,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const resp = await retryAsyncFunction(getFile);
|
||||||
|
if (resp.data === undefined) throw Error(CustomError.REQUEST_FAILED);
|
||||||
return new Uint8Array(resp.data);
|
return new Uint8Array(resp.data);
|
||||||
};
|
};
|
||||||
|
|
||||||
async downloadFileStream(file: EnteFile): Promise<Response> {
|
async downloadFileStream(file: EnteFile): Promise<Response> {
|
||||||
if (!this.token) {
|
const accessToken = this.token;
|
||||||
throw Error(CustomError.TOKEN_MISSING);
|
const accessTokenJWT = this.passwordToken;
|
||||||
}
|
if (!accessToken) throw Error(CustomError.TOKEN_MISSING);
|
||||||
return retryAsyncFunction(() =>
|
|
||||||
fetch(getPublicCollectionFileURL(file.id), {
|
// See: [Note: Passing credentials for self-hosted file fetches]
|
||||||
headers: {
|
const getFile = () => {
|
||||||
"X-Auth-Access-Token": this.token,
|
const customOrigin = customAPIOrigin();
|
||||||
...(this.passwordToken && {
|
if (customOrigin) {
|
||||||
"X-Auth-Access-Token-JWT": this.passwordToken,
|
const params = new URLSearchParams({
|
||||||
}),
|
accessToken,
|
||||||
},
|
...(accessTokenJWT && { accessTokenJWT }),
|
||||||
}),
|
});
|
||||||
);
|
return fetch(
|
||||||
|
`${customOrigin}/public-collection/files/download/${file.id}?${params.toString()}`,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return fetch(
|
||||||
|
`https://public-albums.ente.io/download/?fileID=${file.id}`,
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
"X-Auth-Access-Token": accessToken,
|
||||||
|
...(accessTokenJWT && {
|
||||||
|
"X-Auth-Access-Token-JWT": accessTokenJWT,
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return retryAsyncFunction(getFile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,22 +26,6 @@ export const getEndpoint = () => {
|
|||||||
return "https://api.ente.io";
|
return "https://api.ente.io";
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getPublicCollectionFileURL = (id: number) => {
|
|
||||||
const endpoint = process.env.NEXT_PUBLIC_ENTE_ENDPOINT;
|
|
||||||
if (endpoint) {
|
|
||||||
return `${endpoint}/public-collection/files/download/${id}`;
|
|
||||||
}
|
|
||||||
return `https://public-albums.ente.io/download/?fileID=${id}`;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getPublicCollectionThumbnailURL = (id: number) => {
|
|
||||||
const endpoint = process.env.NEXT_PUBLIC_ENTE_ENDPOINT;
|
|
||||||
if (endpoint) {
|
|
||||||
return `${endpoint}/public-collection/files/preview/${id}`;
|
|
||||||
}
|
|
||||||
return `https://public-albums.ente.io/preview/?fileID=${id}`;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getUploadEndpoint = () => {
|
export const getUploadEndpoint = () => {
|
||||||
const endpoint = process.env.NEXT_PUBLIC_ENTE_ENDPOINT;
|
const endpoint = process.env.NEXT_PUBLIC_ENTE_ENDPOINT;
|
||||||
if (endpoint) {
|
if (endpoint) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user