Skip to content

armgit / ente

Public Imported from GitHub
0

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

get 1

Manav Rathi0cf50513cca5
diff --git a/desktop/src/main/services/ffmpeg-worker.ts b/desktop/src/main/services/ffmpeg-worker.ts
index 6597951dd0..ca95b37c8b 100644
--- a/desktop/src/main/services/ffmpeg-worker.ts
+++ b/desktop/src/main/services/ffmpeg-worker.ts
@@ -18,7 +18,10 @@ import log from "../log-worker";
import { messagePortMainEndpoint } from "../utils/comlink";
import { nullToUndefined, wait } from "../utils/common";
import { execAsyncWorker } from "../utils/exec-worker";
-import { publicRequestHeaders } from "../utils/http";
+import {
+ authenticatedRequestHeaders,
+ publicRequestHeaders,
+} from "../utils/http";
/* Ditto in the web app's code (used by the Wasm FFmpeg invocation). */
const ffmpegPathPlaceholder = "FFMPEG";
@@ -46,6 +49,7 @@ export interface FFmpegUtilityProcess {
ffmpegGenerateHLSPlaylistAndSegments: (
inputFilePath: string,
outputPathPrefix: string,
+ fileID: number,
fetchURL: string,
authToken: string,
) => Promise<FFmpegGenerateHLSPlaylistAndSegmentsResult | undefined>;
@@ -235,6 +239,9 @@ export interface FFmpegGenerateHLSPlaylistAndSegmentsResult {
* the user's local file system. This function will write the generated HLS
* playlist and video segments under this prefix.
*
+ * @param fileID The ID of the {@link EnteFile} whose HLS playlist we are
+ * generating.
+ *
* @param fetchURL The fully resolved API URL for obtaining pre-signed S3 URLs
* for uploading the generated video segment file.
*
@@ -250,6 +257,7 @@ export interface FFmpegGenerateHLSPlaylistAndSegmentsResult {
const ffmpegGenerateHLSPlaylistAndSegments = async (
inputFilePath: string,
outputPathPrefix: string,
+ fileID: number,
fetchURL: string,
authToken: string,
): Promise<FFmpegGenerateHLSPlaylistAndSegmentsResult | undefined> => {
@@ -555,6 +563,7 @@ const ffmpegGenerateHLSPlaylistAndSegments = async (
videoObjectID = await uploadVideoSegments(
videoPath,
videoSize,
+ fileID,
fetchURL,
authToken,
);
@@ -832,6 +841,8 @@ const pseudoFFProbeVideo = async (inputFilePath: string) => {
*
* @param videoSize The size in bytes of the file at {@link videoFilePath}.
*
+ * @param fileID The ID of the {@link EnteFile} whose video segment this is.
+ *
* @param fetchURL The API URL for fetching pre-signed upload URLs.
*
* @param authToken The user's auth token for use with {@link fetchURL}.
@@ -841,6 +852,7 @@ const pseudoFFProbeVideo = async (inputFilePath: string) => {
const uploadVideoSegments = async (
videoFilePath: string,
videoSize: number,
+ fileID: number,
fetchURL: string,
authToken: string,
) => {
@@ -917,74 +929,37 @@ const FilePreviewDataUploadURLResponse = z.object({
/**
* Obtain a pre-signed URL(s) that can be used to upload the "file preview data"
- * of type "vid_preview" (the file containing the encrypted video segments which
- * the "vid_preview" HLS playlist for the file would refer to).
+ * of type "vid_preview".
+ *
+ * This will be the file containing the encrypted video segments which the
+ * "vid_preview" HLS playlist for the file would refer to.
+ *
+ * @param partCount If greater than 1, then we request for a multipart upload.
*/
-export const getFilePreviewDataUploadURL = async (file: EnteFile) => {
- // [Note: Estimating number of parts for a vid_preview upload]
- //
- // We need to determine the number of parts of a multipart upload that we
- // will use to upload a vid_preview. However, we do not have the generated
- // preview file yet - that'll happen later, in the desktop side.
- //
- // We could make this API request from the desktop app side, but that will
- // require reinventing a lot of the wheels in that code.
- //
- // So instead, we try to estimate a reasonable number of parts into which
- // the eventually generated video should be split for a multipart upload.
- // Some relevant numbers:
- //
- // - A part needs to be a minimum 5 MB.
- //
- // - During regular file uploads, we use 20 MB parts.
- //
- // - If possible, we want to try and keep the part size below 100 MB. This
- // is not a hard limit, but just a good to have to avoid hitting request
- // size limits (e.g. see [Note: Multipart uploads]).
- //
- // The estimate we use is:
- //
- // partCount = (size) => Math.ceil((0.7 * size) / 100)
- //
- // That is, if the generated preview file is 70% of the original's size, how
- // many 100 MB parts will it need.
- //
- // In practice, this will is a big overestimate of the generated preview
- // size, a more typical video preview size will be in the 30% to 60% range.
- //
- // The eventual part size will then be:
- //
- // partSize = (totalSize, count) => Math.ceil(totalSize / count)
- //
- // As an example, for a 200 MB file, we'll estimate partCount of 2, and
- // suppose the generated file is 50% of the original, then each part size
- // will be 50 MB.
- //
- // As a sanity check of the other extreme, if the generated file is 10% of
- // the original, we still end up with 10 MB parts.
- let partCount = 1; // Default is single object upload.
- const fileSize = file.info?.fileSize; /* bytes */
- if (fileSize) {
- const fileSizeMB = fileSize / 1024 / 1024;
- // For files larger than 100 MB, estimate the number of ~100 MB parts.
- if (fileSizeMB > 100) {
- partCount = Math.ceil((0.7 * fileSize) / (100 * 1024));
- }
- }
-
+export const getFilePreviewDataUploadURL = async (
+ fileID: string,
+ partCount: number,
+ fetchURL: string,
+ authToken: string,
+) => {
const params = new URLSearchParams({
- fileID: `${file.id}`,
+ fileID: fileID.toString(),
type: "vid_preview",
});
if (partCount > 1) {
params.set("isMultiPart", "true");
params.set("count", partCount.toString());
}
- const url = await apiURL("/files/data/preview-upload-url");
- const res = await fetch(`${url}?${params.toString()}`, {
- headers: await authenticatedRequestHeaders(),
- });
- ensureOk(res);
+
+ const res = await retryEnsuringHTTPOk(() =>
+ fetch(`${fetchURL}?${params.toString()}`, {
+ headers: authenticatedRequestHeaders(
+ desktopAppVersion(),
+ authToken,
+ ),
+ }),
+ );
+
return FilePreviewDataUploadURLResponse.parse(await res.json());
};
diff --git a/desktop/src/main/stream.ts b/desktop/src/main/stream.ts
index 886caf6ec1..b44cfbc046 100644
--- a/desktop/src/main/stream.ts
+++ b/desktop/src/main/stream.ts
@@ -289,9 +289,10 @@ const handleGenerateHLSWrite = async (
request: Request,
params: URLSearchParams,
) => {
+ const fileID = parseInt(params.get("fileID") ?? "", 10);
const fetchURL = params.get("fetchURL");
const authToken = params.get("authToken");
- if (!fetchURL || !authToken) throw new Error("Missing params");
+ if (!fileID || !fetchURL || !authToken) throw new Error("Missing params");
let inputItem: Parameters<typeof makeFileForStreamOrPathOrZipItem>[0];
const path = params.get("path");
@@ -325,6 +326,7 @@ const handleGenerateHLSWrite = async (
result = await worker.ffmpegGenerateHLSPlaylistAndSegments(
inputFilePath,
outputFilePathPrefix,
+ fileID,
fetchURL,
authToken,
);
diff --git a/desktop/src/main/utils/http.ts b/desktop/src/main/utils/http.ts
index 6496b99cc6..91a476fed9 100644
--- a/desktop/src/main/utils/http.ts
+++ b/desktop/src/main/utils/http.ts
@@ -12,5 +12,20 @@ export const clientPackageName = "io.ente.photos.desktop";
*/
export const publicRequestHeaders = (desktopAppVersion: string) => ({
"X-Client-Package": clientPackageName,
- "X-Client-Version": desktopAppVersion
+ "X-Client-Version": desktopAppVersion,
+});
+
+/**
+ * Reimplementation of {@link authenticatedRequestHeaders} from the web source.
+ *
+ * This builds on top of {@link publicRequestHeaders} and takes the same
+ * parameters, and additionally also requires the {@link authToken} that will be
+ * passed as the "X-Auth-Token" header.
+ */
+export const authenticatedRequestHeaders = (
+ desktopAppVersion: string,
+ authToken: string,
+) => ({
+ ...publicRequestHeaders(desktopAppVersion),
+ "X-Auth-Token": authToken,
});
diff --git a/web/packages/gallery/services/video.ts b/web/packages/gallery/services/video.ts
index c66f49e5fe..07b620842d 100644
--- a/web/packages/gallery/services/video.ts
+++ b/web/packages/gallery/services/video.ts
@@ -1029,6 +1029,7 @@ const processQueueItem = async ({
res = await initiateGenerateHLS(
electron,
sourceVideo,
+ file.id,
fetchURL,
authToken,
);
diff --git a/web/packages/gallery/utils/native-stream.ts b/web/packages/gallery/utils/native-stream.ts
index 7fc1c63ee7..7863f0d33c 100644
--- a/web/packages/gallery/utils/native-stream.ts
+++ b/web/packages/gallery/utils/native-stream.ts
@@ -193,6 +193,9 @@ export type GenerateHLSResult = z.infer<typeof GenerateHLSResult>;
*
* - Otherwise it should be a {@link ReadableStream} of the video contents.
*
+ * @param fileID The ID of the {@link EnteFile} for which we're generating the
+ * HLS.
+ *
* @param fetchURL The fully resolved API URL for obtaining the pre-signed URLs
* to which the video segment file should be uploaded.
*
@@ -211,11 +214,13 @@ export type GenerateHLSResult = z.infer<typeof GenerateHLSResult>;
export const initiateGenerateHLS = async (
_: Electron,
video: FileSystemUploadItem | ReadableStream,
+ fileID: number,
fetchURL: string,
authToken: string,
): Promise<GenerateHLSResult | undefined> => {
const params = new URLSearchParams({
op: "generate-hls",
+ fileID: fileID.toString(),
fetchURL,
authToken,
});