|
@@ -1,4 +1,4 @@
|
|
|
-import { nameAndExtension } from "@/next/file";
|
|
|
+import { fileNameFromComponents, nameAndExtension } from "@/next/file";
|
|
|
import JSZip from "jszip";
|
|
|
|
|
|
class LivePhoto {
|
|
@@ -8,18 +8,6 @@ class LivePhoto {
|
|
|
videoNameTitle: string;
|
|
|
}
|
|
|
|
|
|
-export function getFileNameWithoutExtension(filename: string) {
|
|
|
- const lastDotPosition = filename.lastIndexOf(".");
|
|
|
- if (lastDotPosition === -1) return filename;
|
|
|
- else return filename.slice(0, lastDotPosition);
|
|
|
-}
|
|
|
-
|
|
|
-export function getFileExtensionWithDot(filename: string) {
|
|
|
- const lastDotPosition = filename.lastIndexOf(".");
|
|
|
- if (lastDotPosition === -1) return "";
|
|
|
- else return filename.slice(lastDotPosition);
|
|
|
-}
|
|
|
-
|
|
|
/**
|
|
|
* Convert a binary serialized representation of a live photo to an in-memory
|
|
|
* {@link LivePhoto}.
|
|
@@ -42,12 +30,12 @@ export const decodeLivePhoto = async (fileName: string, zipBlob: Blob) => {
|
|
|
const livePhoto = new LivePhoto();
|
|
|
for (const zipFileName in zip.files) {
|
|
|
if (zipFileName.startsWith("image")) {
|
|
|
- livePhoto.imageNameTitle =
|
|
|
- name + getFileExtensionWithDot(zipFileName);
|
|
|
+ const [, imageExt] = nameAndExtension(zipFileName);
|
|
|
+ livePhoto.imageNameTitle = fileNameFromComponents([name, imageExt]);
|
|
|
livePhoto.image = await zip.files[zipFileName].async("uint8array");
|
|
|
} else if (zipFileName.startsWith("video")) {
|
|
|
- livePhoto.videoNameTitle =
|
|
|
- name + getFileExtensionWithDot(zipFileName);
|
|
|
+ const [, videoExt] = nameAndExtension(zipFileName);
|
|
|
+ livePhoto.videoNameTitle = fileNameFromComponents([name, videoExt]);
|
|
|
livePhoto.video = await zip.files[zipFileName].async("uint8array");
|
|
|
}
|
|
|
}
|
|
@@ -69,7 +57,7 @@ export const encodeLivePhoto = async (livePhoto: LivePhoto) => {
|
|
|
const [, videoExt] = nameAndExtension(livePhoto.videoNameTitle);
|
|
|
|
|
|
const zip = new JSZip();
|
|
|
- zip.file(["image", imageExt].filter((x) => !!x).join("."), livePhoto.image);
|
|
|
- zip.file(["video", videoExt].filter((x) => !!x).join("."), livePhoto.video);
|
|
|
+ zip.file(fileNameFromComponents(["image", imageExt]), livePhoto.image);
|
|
|
+ zip.file(fileNameFromComponents(["video", videoExt]), livePhoto.video);
|
|
|
return await zip.generateAsync({ type: "uint8array" });
|
|
|
};
|