Skip to content

armgit / ente

Public Imported from GitHub
0

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

take 1

Manav Rathibc762b972fac
diff --git a/web/packages/gallery/services/upload/remote.ts b/web/packages/gallery/services/upload/remote.ts
index 46007067d9..063511d224 100644
--- a/web/packages/gallery/services/upload/remote.ts
+++ b/web/packages/gallery/services/upload/remote.ts
@@ -16,6 +16,7 @@ import { apiURL, uploaderOrigin } from "ente-base/origins";
import { type EnteFile } from "ente-media/file";
import { CustomError, handleUploadError } from "ente-shared/error";
import HTTPService from "ente-shared/network/HTTPService";
+import { nullToUndefined } from "ente-utils/transform";
import { z } from "zod";
import type { MultipartUploadURLs, UploadFile } from "./upload-service";
@@ -243,6 +244,39 @@ export class PhotosUploadHTTPClient {
}
}
+/**
+ * Upload a part of a multipart upload using a pre-signed URL.
+ *
+ * See: [Note: Multipart uploads].
+ *
+ * @param partUploadURL A pre-signed URL that can be used to upload data to the
+ * remote S3-compatible storage.
+ *
+ * @param partData The part bytes to upload.
+ *
+ * @param onProgress A function to call to indicate upload progress.
+ *
+ * @returns the value of the "ETag" header in the remote response, or
+ * `undefined` if the ETag was not present in the response (this is not expected
+ * from remote in case of a successful response, but it can happen in case the
+ * user has some misconfigured browser extension which is blocking the ETag
+ * header from being parsed).
+ */
+export const putFilePart = async (
+ partUploadURL: string,
+ partData: Uint8Array,
+ onProgress: (bytesUploaded: number) => void,
+) => {
+ const res = await retryEnsuringHTTPOk(() =>
+ fetch(partUploadURL, {
+ method: "PUT",
+ headers: publicRequestHeaders(),
+ body: partData,
+ }),
+ );
+ return nullToUndefined(res.headers.get("etag"));
+};
+
/**
* Information about an individual part of a multipart upload that has been
* uploaded to the remote (S3 or proxy).
@@ -371,8 +405,9 @@ const createMultipartUploadRequestBody = (
*
* 2. Break the file to be uploaded into parts, and upload each part using a PUT
* request to one of the presigned URLs we got in step 1. There are two
- * variants of this - one where we directly upload to the remote (S3), and
- * one where we go via a worker.
+ * variants of this - one where we directly upload to the remote (S3)
+ * ({@link putFilePart}), and one where we go via a worker
+ * ({@link putFilePartViaWorker}).
*
* 3. Once all the parts have been uploaded, send a consolidated report of all
* the uploaded parts (the step 2's) to remote via another presigned
diff --git a/web/packages/gallery/services/upload/upload-service.ts b/web/packages/gallery/services/upload/upload-service.ts
index fb7beff2a3..8be36b6299 100644
--- a/web/packages/gallery/services/upload/upload-service.ts
+++ b/web/packages/gallery/services/upload/upload-service.ts
@@ -56,6 +56,7 @@ import {
completeMultipartUploadViaWorker,
PhotosUploadHTTPClient,
PublicAlbumsUploadHTTPClient,
+ putFilePart,
type MultipartCompletedPart,
type ObjectUploadURL,
} from "./remote";
@@ -1564,12 +1565,12 @@ async function uploadStreamUsingMultipart(
let fileSize = 0;
for (const [
index,
- fileUploadURL,
+ partUploadURL,
] of multipartUploadURLs.partURLs.entries()) {
abortIfCancelled();
- const uploadPart = await nextMultipartUploadPart(streamReader);
- fileSize += uploadPart.length;
+ const partData = await nextMultipartUploadPart(streamReader);
+ fileSize += partData.length;
const progressTracker = makeProgressTracker(
fileLocalID,
percentPerPart,
@@ -1578,16 +1579,23 @@ async function uploadStreamUsingMultipart(
let eTag = null;
if (!isCFUploadProxyDisabled) {
eTag = await photosHTTPClient.putFilePartV2(
- fileUploadURL,
- uploadPart,
+ partUploadURL,
+ partData,
progressTracker,
);
} else {
- eTag = await photosHTTPClient.putFilePart(
- fileUploadURL,
- uploadPart,
- progressTracker,
- );
+ if (process.env.NEXT_PUBLIC_ENTE_WIP_MP) {
+ eTag = await putFilePart(partUploadURL, partData, (...args) => {
+ console.log("test progress", args);
+ });
+ if (!eTag) throw new Error(CustomError.ETAG_MISSING);
+ } else {
+ eTag = await photosHTTPClient.putFilePart(
+ partUploadURL,
+ partData,
+ progressTracker,
+ );
+ }
}
completedParts.push({ partNumber: index + 1, eTag });
}