And the next

This commit is contained in:
Manav Rathi 2024-04-26 09:37:52 +05:30
parent f47732ff5e
commit a7f5061eb6
No known key found for this signature in database
2 changed files with 53 additions and 44 deletions

View file

@ -31,7 +31,6 @@ import { SetFiles } from "types/gallery";
import { import {
FileWithCollection, FileWithCollection,
PublicUploadProps, PublicUploadProps,
type FileWithCollection2 as ClusteredFile,
type LivePhotoAssets2, type LivePhotoAssets2,
} from "types/upload"; } from "types/upload";
import { import {
@ -485,16 +484,16 @@ class UploadManager {
while (this.filesToBeUploaded.length > 0) { while (this.filesToBeUploaded.length > 0) {
this.abortIfCancelled(); this.abortIfCancelled();
let fileWithCollection = this.filesToBeUploaded.pop(); const clusteredFile = this.filesToBeUploaded.pop();
const { collectionID } = fileWithCollection; const { localID, collectionID } = clusteredFile;
const collection = this.collections.get(collectionID); const collection = this.collections.get(collectionID);
fileWithCollection = { ...fileWithCollection, collection }; const uploadableFile = { ...clusteredFile, collection };
uiService.setFileProgress(fileWithCollection.localID, 0); uiService.setFileProgress(localID, 0);
await wait(0); await wait(0);
const { fileUploadResult, uploadedFile } = await uploader( const { uploadResult, uploadedFile } = await uploader(
fileWithCollection, uploadableFile,
this.uploaderName, this.uploaderName,
this.existingFiles, this.existingFiles,
this.parsedMetadataJSONMap, this.parsedMetadataJSONMap,
@ -516,46 +515,43 @@ class UploadManager {
); );
const finalUploadResult = await this.postUploadTask( const finalUploadResult = await this.postUploadTask(
fileUploadResult, uploadableFile,
uploadResult,
uploadedFile, uploadedFile,
fileWithCollection,
); );
this.uiService.moveFileToResultList( this.uiService.moveFileToResultList(localID, finalUploadResult);
fileWithCollection.localID,
finalUploadResult,
);
this.uiService.increaseFileUploaded(); this.uiService.increaseFileUploaded();
UploadService.reducePendingUploadCount(); UploadService.reducePendingUploadCount();
} }
} }
private async postUploadTask( private async postUploadTask(
fileUploadResult: UPLOAD_RESULT, uploadableFile: UploadableFile,
uploadedFile: EncryptedEnteFile | EnteFile | null, uploadResult: UPLOAD_RESULT,
fileWithCollection: ClusteredFile, uploadedFile: EncryptedEnteFile | EnteFile | undefined,
) { ) {
log.info(`Upload completed with result: ${uploadResult}`);
try { try {
let decryptedFile: EnteFile; let decryptedFile: EnteFile;
log.info(`Upload completed with result: ${fileUploadResult}`); await this.removeFromPendingUploads(uploadableFile);
await this.removeFromPendingUploads(fileWithCollection); switch (uploadResult) {
switch (fileUploadResult) {
case UPLOAD_RESULT.FAILED: case UPLOAD_RESULT.FAILED:
case UPLOAD_RESULT.BLOCKED: case UPLOAD_RESULT.BLOCKED:
this.failedFiles.push(fileWithCollection); this.failedFiles.push(uploadableFile);
break; break;
case UPLOAD_RESULT.ALREADY_UPLOADED: case UPLOAD_RESULT.ALREADY_UPLOADED:
decryptedFile = uploadedFile as EnteFile; decryptedFile = uploadedFile as EnteFile;
break; break;
case UPLOAD_RESULT.ADDED_SYMLINK: case UPLOAD_RESULT.ADDED_SYMLINK:
decryptedFile = uploadedFile as EnteFile; decryptedFile = uploadedFile as EnteFile;
fileUploadResult = UPLOAD_RESULT.UPLOADED; uploadResult = UPLOAD_RESULT.UPLOADED;
break; break;
case UPLOAD_RESULT.UPLOADED: case UPLOAD_RESULT.UPLOADED:
case UPLOAD_RESULT.UPLOADED_WITH_STATIC_THUMBNAIL: case UPLOAD_RESULT.UPLOADED_WITH_STATIC_THUMBNAIL:
decryptedFile = await decryptFile( decryptedFile = await decryptFile(
uploadedFile as EncryptedEnteFile, uploadedFile as EncryptedEnteFile,
fileWithCollection.collection.key, uploadableFile.collection.key,
); );
break; break;
case UPLOAD_RESULT.UNSUPPORTED: case UPLOAD_RESULT.UNSUPPORTED:
@ -563,23 +559,21 @@ class UploadManager {
// no-op // no-op
break; break;
default: default:
throw new Error( throw new Error(`Invalid Upload Result ${uploadResult}`);
`Invalid Upload Result ${fileUploadResult}`,
);
} }
if ( if (
[ [
UPLOAD_RESULT.ADDED_SYMLINK, UPLOAD_RESULT.ADDED_SYMLINK,
UPLOAD_RESULT.UPLOADED, UPLOAD_RESULT.UPLOADED,
UPLOAD_RESULT.UPLOADED_WITH_STATIC_THUMBNAIL, UPLOAD_RESULT.UPLOADED_WITH_STATIC_THUMBNAIL,
].includes(fileUploadResult) ].includes(uploadResult)
) { ) {
try { try {
eventBus.emit(Events.FILE_UPLOADED, { eventBus.emit(Events.FILE_UPLOADED, {
enteFile: decryptedFile, enteFile: decryptedFile,
localFile: localFile:
fileWithCollection.file ?? uploadableFile.fileOrPath ??
fileWithCollection.livePhotoAssets.image, uploadableFile.livePhotoAssets.image,
}); });
} catch (e) { } catch (e) {
log.warn("Ignoring error in fileUploaded handlers", e); log.warn("Ignoring error in fileUploaded handlers", e);
@ -587,11 +581,11 @@ class UploadManager {
this.updateExistingFiles(decryptedFile); this.updateExistingFiles(decryptedFile);
} }
await this.watchFolderCallback( await this.watchFolderCallback(
fileUploadResult, uploadResult,
fileWithCollection, uploadableFile,
uploadedFile as EncryptedEnteFile, uploadedFile as EncryptedEnteFile,
); );
return fileUploadResult; return uploadResult;
} catch (e) { } catch (e) {
log.error("failed to do post file upload action", e); log.error("failed to do post file upload action", e);
return UPLOAD_RESULT.FAILED; return UPLOAD_RESULT.FAILED;
@ -729,6 +723,11 @@ const makeFileWithCollectionIDAndName = (
}; };
}; };
/**
* A file with both parts of a live photo clubbed together.
*
* See: [Note: Intermediate file types during upload].
*/
type ClusteredFile = { type ClusteredFile = {
localID: number; localID: number;
collectionID: number; collectionID: number;
@ -738,6 +737,16 @@ type ClusteredFile = {
livePhotoAssets?: LivePhotoAssets2; livePhotoAssets?: LivePhotoAssets2;
}; };
/**
* The file that we hand off to the uploader. Essentially {@link ClusteredFile}
* with the {@link collection} attached to it.
*
* See: [Note: Intermediate file types during upload].
*/
export type UploadableFile = ClusteredFile & {
collection: Collection;
};
const splitMetadataAndMediaFiles = ( const splitMetadataAndMediaFiles = (
files: FileWithCollectionIDAndName[], files: FileWithCollectionIDAndName[],
): [ ): [

View file

@ -25,6 +25,7 @@ import { parseImageMetadata } from "services/exif";
import * as ffmpeg from "services/ffmpeg"; import * as ffmpeg from "services/ffmpeg";
import { import {
EnteFile, EnteFile,
type EncryptedEnteFile,
type FilePublicMagicMetadata, type FilePublicMagicMetadata,
type FilePublicMagicMetadataProps, type FilePublicMagicMetadataProps,
} from "types/file"; } from "types/file";
@ -40,7 +41,6 @@ import {
UploadAsset, UploadAsset,
UploadFile, UploadFile,
UploadURL, UploadURL,
type FileWithCollection2,
type LivePhotoAssets2, type LivePhotoAssets2,
type UploadAsset2, type UploadAsset2,
} from "types/upload"; } from "types/upload";
@ -63,6 +63,7 @@ import {
generateThumbnailWeb, generateThumbnailWeb,
} from "./thumbnail"; } from "./thumbnail";
import UploadHttpClient from "./uploadHttpClient"; import UploadHttpClient from "./uploadHttpClient";
import type { UploadableFile } from "./uploadManager";
/** Upload files to cloud storage */ /** Upload files to cloud storage */
class UploadService { class UploadService {
@ -160,12 +161,12 @@ type MakeProgressTracker = (
) => unknown; ) => unknown;
interface UploadResponse { interface UploadResponse {
fileUploadResult: UPLOAD_RESULT; uploadResult: UPLOAD_RESULT;
uploadedFile?: EnteFile; uploadedFile?: EncryptedEnteFile | EnteFile;
} }
export const uploader = async ( export const uploader = async (
fileWithCollection: FileWithCollection2, fileWithCollection: UploadableFile,
uploaderName: string, uploaderName: string,
existingFiles: EnteFile[], existingFiles: EnteFile[],
parsedMetadataJSONMap: Map<string, ParsedMetadataJSON>, parsedMetadataJSONMap: Map<string, ParsedMetadataJSON>,
@ -200,7 +201,7 @@ export const uploader = async (
const maxFileSize = 4 * 1024 * 1024 * 1024; /* 4 GB */ const maxFileSize = 4 * 1024 * 1024 * 1024; /* 4 GB */
if (fileSize >= maxFileSize) if (fileSize >= maxFileSize)
return { fileUploadResult: UPLOAD_RESULT.TOO_LARGE }; return { uploadResult: UPLOAD_RESULT.TOO_LARGE };
abortIfCancelled(); abortIfCancelled();
@ -225,7 +226,7 @@ export const uploader = async (
); );
if (matchInSameCollection) { if (matchInSameCollection) {
return { return {
fileUploadResult: UPLOAD_RESULT.ALREADY_UPLOADED, uploadResult: UPLOAD_RESULT.ALREADY_UPLOADED,
uploadedFile: matchInSameCollection, uploadedFile: matchInSameCollection,
}; };
} else { } else {
@ -234,7 +235,7 @@ export const uploader = async (
symlink.collectionID = collection.id; symlink.collectionID = collection.id;
await addToCollection(collection, [symlink]); await addToCollection(collection, [symlink]);
return { return {
fileUploadResult: UPLOAD_RESULT.ADDED_SYMLINK, uploadResult: UPLOAD_RESULT.ADDED_SYMLINK,
uploadedFile: symlink, uploadedFile: symlink,
}; };
} }
@ -287,7 +288,7 @@ export const uploader = async (
}); });
return { return {
fileUploadResult: metadata.hasStaticThumbnail uploadResult: metadata.hasStaticThumbnail
? UPLOAD_RESULT.UPLOADED_WITH_STATIC_THUMBNAIL ? UPLOAD_RESULT.UPLOADED_WITH_STATIC_THUMBNAIL
: UPLOAD_RESULT.UPLOADED, : UPLOAD_RESULT.UPLOADED,
uploadedFile: uploadedFile, uploadedFile: uploadedFile,
@ -304,16 +305,15 @@ export const uploader = async (
const error = handleUploadError(e); const error = handleUploadError(e);
switch (error.message) { switch (error.message) {
case CustomError.ETAG_MISSING: case CustomError.ETAG_MISSING:
return { fileUploadResult: UPLOAD_RESULT.BLOCKED }; return { uploadResult: UPLOAD_RESULT.BLOCKED };
case CustomError.UNSUPPORTED_FILE_FORMAT: case CustomError.UNSUPPORTED_FILE_FORMAT:
return { fileUploadResult: UPLOAD_RESULT.UNSUPPORTED }; return { uploadResult: UPLOAD_RESULT.UNSUPPORTED };
case CustomError.FILE_TOO_LARGE: case CustomError.FILE_TOO_LARGE:
return { return {
fileUploadResult: uploadResult: UPLOAD_RESULT.LARGER_THAN_AVAILABLE_STORAGE,
UPLOAD_RESULT.LARGER_THAN_AVAILABLE_STORAGE,
}; };
default: default:
return { fileUploadResult: UPLOAD_RESULT.FAILED }; return { uploadResult: UPLOAD_RESULT.FAILED };
} }
} }
}; };