ente/src/services/motionPhotoService.ts

35 lines
1 KiB
TypeScript
Raw Normal View History

import JSZip from 'jszip';
2021-08-11 15:59:59 +00:00
import { fileExtensionWithDot } from 'utils/file';
class MotionPhoto {
2021-08-12 04:50:02 +00:00
image: Uint8Array;
video: Uint8Array;
2021-08-11 15:59:59 +00:00
imageNameTitle: String;
videoNameTitle: String;
}
2021-08-11 15:59:59 +00:00
export const decodeMotionPhoto = async (
zipBlob: Blob,
2021-08-13 02:38:38 +00:00
originalName: string
2021-08-11 15:59:59 +00:00
) => {
const zip = await JSZip.loadAsync(zipBlob, { createFolders: true });
const motionPhoto = new MotionPhoto();
for (const zipFilename in zip.files) {
if (zipFilename.startsWith('image')) {
motionPhoto.imageNameTitle =
originalName + fileExtensionWithDot(zipFilename);
2021-08-12 04:50:02 +00:00
motionPhoto.image = await zip.files[zipFilename].async(
2021-08-13 02:38:38 +00:00
'uint8array'
2021-08-11 15:59:59 +00:00
);
} else if (zipFilename.startsWith('video')) {
motionPhoto.videoNameTitle =
originalName + fileExtensionWithDot(zipFilename);
2021-08-12 04:50:02 +00:00
motionPhoto.video = await zip.files[zipFilename].async(
2021-08-13 02:38:38 +00:00
'uint8array'
2021-08-11 15:59:59 +00:00
);
}
}
return motionPhoto;
};