Replace encodelivephoto

This commit is contained in:
Manav Rathi 2024-04-17 11:48:15 +05:30
parent 3172104578
commit 637d830f19
No known key found for this signature in database
3 changed files with 28 additions and 17 deletions

View file

@ -5,6 +5,9 @@ import {
getFileNameWithoutExtension,
} from "utils/file";
/**
* An in-memory representation of a live photo
*/
class LivePhoto {
image: Uint8Array;
video: Uint8Array;
@ -31,6 +34,16 @@ export const decodeLivePhoto = async (file: EnteFile, zipBlob: Blob) => {
return livePhoto;
};
/**
* Return a binary serialized representation of a live photo.
*
* This function takes the (in-memory) image and video data from the
* {@link livePhoto} object, writes them to a zip file (using the respective
* filenames), and returns the {@link Uint8Array} that represent the bytes of
* this zip file.
*
* @param livePhoto The in-mem photo to serialized.
*/
export const encodeLivePhoto = async (livePhoto: LivePhoto) => {
const zip = new JSZip();
zip.file(

View file

@ -4,7 +4,7 @@ import { CustomError } from "@ente/shared/error";
import { Remote } from "comlink";
import { FILE_TYPE } from "constants/file";
import { LIVE_PHOTO_ASSET_SIZE_LIMIT } from "constants/upload";
import { encodeLivePhoto } from "services/livePhotoService";
import { encodeLivePhoto } from "@/media/live-photo";
import { getFileType } from "services/typeDetectionService";
import {
ElectronFile,

View file

@ -1,3 +1,4 @@
import { nameAndExtension } from "@/next/file";
import JSZip from "jszip";
class LivePhoto {
@ -20,33 +21,30 @@ export function getFileExtensionWithDot(filename: string) {
}
export const decodeLivePhoto = async (fileName: string, zipBlob: Blob) => {
const originalName = getFileNameWithoutExtension(fileName);
const [name] = nameAndExtension(fileName);
const zip = await JSZip.loadAsync(zipBlob, { createFolders: true });
const livePhoto = new LivePhoto();
for (const zipFilename in zip.files) {
if (zipFilename.startsWith("image")) {
for (const zipFileName in zip.files) {
if (zipFileName.startsWith("image")) {
livePhoto.imageNameTitle =
originalName + getFileExtensionWithDot(zipFilename);
livePhoto.image = await zip.files[zipFilename].async("uint8array");
} else if (zipFilename.startsWith("video")) {
name + getFileExtensionWithDot(zipFileName);
livePhoto.image = await zip.files[zipFileName].async("uint8array");
} else if (zipFileName.startsWith("video")) {
livePhoto.videoNameTitle =
originalName + getFileExtensionWithDot(zipFilename);
livePhoto.video = await zip.files[zipFilename].async("uint8array");
name + getFileExtensionWithDot(zipFileName);
livePhoto.video = await zip.files[zipFileName].async("uint8array");
}
}
return livePhoto;
};
export const encodeLivePhoto = async (livePhoto: LivePhoto) => {
const [, imageExt] = nameAndExtension(livePhoto.imageNameTitle);
const [, videoExt] = nameAndExtension(livePhoto.videoNameTitle);
const zip = new JSZip();
zip.file(
"image" + getFileExtensionWithDot(livePhoto.imageNameTitle),
livePhoto.image,
);
zip.file(
"video" + getFileExtensionWithDot(livePhoto.videoNameTitle),
livePhoto.video,
);
zip.file(["image", imageExt].filter((x) => !!x).join("."), livePhoto.image);
zip.file(["video", videoExt].filter((x) => !!x).join("."), livePhoto.video);
return await zip.generateAsync({ type: "uint8array" });
};