basic refactoring
This commit is contained in:
parent
b94795e0c4
commit
4f068e283d
|
@ -1,12 +1,16 @@
|
||||||
import { getToken } from 'utils/common/key';
|
import { getToken } from 'utils/common/key';
|
||||||
import { getFileUrl, getThumbnailUrl } from 'utils/common/apiUtil';
|
import { getFileUrl, getThumbnailUrl } from 'utils/common/apiUtil';
|
||||||
import CryptoWorker from 'utils/crypto';
|
import CryptoWorker from 'utils/crypto';
|
||||||
import { fileIsHEIC, convertHEIC2JPEG } from 'utils/file';
|
import {
|
||||||
|
fileIsHEIC,
|
||||||
|
convertHEIC2JPEG,
|
||||||
|
fileNameWithoutExtension,
|
||||||
|
} from 'utils/file';
|
||||||
import HTTPService from './HTTPService';
|
import HTTPService from './HTTPService';
|
||||||
import { File } from './fileService';
|
import { File } from './fileService';
|
||||||
import { logError } from 'utils/sentry';
|
import { logError } from 'utils/sentry';
|
||||||
import { FILE_TYPE } from 'pages/gallery';
|
import { FILE_TYPE } from 'pages/gallery';
|
||||||
import { downloadAndDecodeMotionPhoto } from './motionPhotoService';
|
import { decodeMotionPhoto } from './motionPhotoService';
|
||||||
|
|
||||||
class DownloadManager {
|
class DownloadManager {
|
||||||
private fileDownloads = new Map<string, string>();
|
private fileDownloads = new Map<string, string>();
|
||||||
|
@ -60,26 +64,27 @@ class DownloadManager {
|
||||||
getFile = async (file: File, forPreview = false) => {
|
getFile = async (file: File, forPreview = false) => {
|
||||||
try {
|
try {
|
||||||
if (!this.fileDownloads.get(`${file.id}_${forPreview}`)) {
|
if (!this.fileDownloads.get(`${file.id}_${forPreview}`)) {
|
||||||
let fileBlob: Blob;
|
|
||||||
// unzip motion photo and return fileBlob of the image for preview
|
// unzip motion photo and return fileBlob of the image for preview
|
||||||
if (
|
const fileStream = await this.downloadFile(file);
|
||||||
forPreview &&
|
let fileBlob = await new Response(fileStream).blob();
|
||||||
file.metadata.fileType === FILE_TYPE.LIVE_PHOTO
|
|
||||||
) {
|
|
||||||
const motionPhoto = await downloadAndDecodeMotionPhoto(
|
|
||||||
file,
|
|
||||||
);
|
|
||||||
fileBlob = await new Response(
|
|
||||||
await motionPhoto.imageBlob,
|
|
||||||
).blob();
|
|
||||||
} else {
|
|
||||||
const fileStream = await this.downloadFile(file);
|
|
||||||
fileBlob = await new Response(fileStream).blob();
|
|
||||||
}
|
|
||||||
if (forPreview) {
|
if (forPreview) {
|
||||||
if (fileIsHEIC(file.metadata.title)) {
|
if (fileIsHEIC(file.metadata.title)) {
|
||||||
fileBlob = await convertHEIC2JPEG(fileBlob);
|
fileBlob = await convertHEIC2JPEG(fileBlob);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (file.metadata.fileType === FILE_TYPE.LIVE_PHOTO) {
|
||||||
|
const originalName = fileNameWithoutExtension(
|
||||||
|
file.metadata.title,
|
||||||
|
);
|
||||||
|
const motionPhoto = await decodeMotionPhoto(
|
||||||
|
fileBlob,
|
||||||
|
originalName,
|
||||||
|
);
|
||||||
|
fileBlob = await new Response(
|
||||||
|
motionPhoto.imageBlob,
|
||||||
|
).blob();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
this.fileDownloads.set(
|
this.fileDownloads.set(
|
||||||
`${file.id}_${forPreview}`,
|
`${file.id}_${forPreview}`,
|
||||||
|
|
|
@ -1,31 +1,34 @@
|
||||||
import downloadManager from "./downloadManager"
|
|
||||||
import { File } from './fileService';
|
|
||||||
import JSZip from 'jszip';
|
import JSZip from 'jszip';
|
||||||
import { fileExtensionWithDot, fileNameWithoutExtension } from "utils/file";
|
import { fileExtensionWithDot } from 'utils/file';
|
||||||
|
|
||||||
class MotionPhoto {
|
class MotionPhoto {
|
||||||
imageBlob: Promise<Uint8Array>
|
imageBlob: Uint8Array;
|
||||||
videoBlob: Promise<Uint8Array>
|
videoBlob: Uint8Array;
|
||||||
imageNameTitle: String
|
imageNameTitle: String;
|
||||||
videoNameTitle: String
|
videoNameTitle: String;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const downloadAndDecodeMotionPhoto = async (file: File) => {
|
export const decodeMotionPhoto = async (
|
||||||
const fileStream = await downloadManager.downloadFile(file);
|
zipBlob: Blob,
|
||||||
let zipBlob = await new Response(fileStream).blob();
|
originalName: string,
|
||||||
return JSZip.loadAsync(zipBlob, { createFolders: true })
|
) => {
|
||||||
.then(function (zip) {
|
const zip = await JSZip.loadAsync(zipBlob, { createFolders: true });
|
||||||
let instance = new MotionPhoto();
|
|
||||||
let orignalName = fileNameWithoutExtension(file.metadata.title)
|
const motionPhoto = new MotionPhoto();
|
||||||
Object.keys(zip.files).forEach(function (zipFilename) {
|
for (const zipFilename in zip.files) {
|
||||||
if (zipFilename.startsWith("image")) {
|
if (zipFilename.startsWith('image')) {
|
||||||
instance.imageNameTitle = orignalName + fileExtensionWithDot(zipFilename);
|
motionPhoto.imageNameTitle =
|
||||||
instance.imageBlob = zip.files[zipFilename].async('uint8array');
|
originalName + fileExtensionWithDot(zipFilename);
|
||||||
} else if (zipFilename.startsWith("video")) {
|
motionPhoto.imageBlob = await zip.files[zipFilename].async(
|
||||||
instance.videoNameTitle = orignalName + fileExtensionWithDot(zipFilename);
|
'uint8array',
|
||||||
instance.videoBlob = zip.files[zipFilename].async('uint8array');
|
);
|
||||||
}
|
} else if (zipFilename.startsWith('video')) {
|
||||||
})
|
motionPhoto.videoNameTitle =
|
||||||
return instance;
|
originalName + fileExtensionWithDot(zipFilename);
|
||||||
});
|
motionPhoto.videoBlob = await zip.files[zipFilename].async(
|
||||||
}
|
'uint8array',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return motionPhoto;
|
||||||
|
};
|
||||||
|
|
Loading…
Reference in a new issue