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, getFileNameWithoutExtension,
} from "utils/file"; } from "utils/file";
/**
* An in-memory representation of a live photo
*/
class LivePhoto { class LivePhoto {
image: Uint8Array; image: Uint8Array;
video: Uint8Array; video: Uint8Array;
@ -31,6 +34,16 @@ export const decodeLivePhoto = async (file: EnteFile, zipBlob: Blob) => {
return livePhoto; 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) => { export const encodeLivePhoto = async (livePhoto: LivePhoto) => {
const zip = new JSZip(); const zip = new JSZip();
zip.file( zip.file(

View file

@ -4,7 +4,7 @@ import { CustomError } from "@ente/shared/error";
import { Remote } from "comlink"; import { Remote } from "comlink";
import { FILE_TYPE } from "constants/file"; import { FILE_TYPE } from "constants/file";
import { LIVE_PHOTO_ASSET_SIZE_LIMIT } from "constants/upload"; 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 { getFileType } from "services/typeDetectionService";
import { import {
ElectronFile, ElectronFile,

View file

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