fix public collection files download
This commit is contained in:
parent
333271bfc0
commit
9b47830076
|
@ -1,4 +1,4 @@
|
|||
import React, { useEffect, useRef, useState } from 'react';
|
||||
import React, { useContext, useEffect, useRef, useState } from 'react';
|
||||
import Photoswipe from 'photoswipe';
|
||||
import PhotoswipeUIDefault from 'photoswipe/dist/photoswipe-ui-default';
|
||||
import classnames from 'classnames';
|
||||
|
@ -44,6 +44,7 @@ import EnteSpinner from 'components/EnteSpinner';
|
|||
import EnteDateTimePicker from 'components/EnteDateTimePicker';
|
||||
import { MAX_EDITED_FILE_NAME_LENGTH } from 'constants/file';
|
||||
import { sleep } from 'utils/common';
|
||||
import { PublicCollectionGalleryContext } from 'utils/publicCollectionGallery';
|
||||
|
||||
const SmallLoadingSpinner = () => (
|
||||
<EnteSpinner
|
||||
|
@ -483,6 +484,9 @@ function PhotoSwipe(props: Iprops) {
|
|||
const [metadata, setMetaData] = useState<EnteFile['metadata']>(null);
|
||||
const [exif, setExif] = useState<any>(null);
|
||||
const needUpdate = useRef(false);
|
||||
const publicCollectionGalleryContext = useContext(
|
||||
PublicCollectionGalleryContext
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (!pswpElement) return;
|
||||
|
@ -668,7 +672,12 @@ function PhotoSwipe(props: Iprops) {
|
|||
const downloadFileHelper = async (file) => {
|
||||
const { loadingBar } = props;
|
||||
loadingBar.current?.continuousStart();
|
||||
await downloadFile(file);
|
||||
await downloadFile(
|
||||
file,
|
||||
publicCollectionGalleryContext.accessedThroughSharedURL,
|
||||
publicCollectionGalleryContext.token
|
||||
);
|
||||
|
||||
loadingBar.current?.complete();
|
||||
};
|
||||
const scheduleUpdate = () => (needUpdate.current = true);
|
||||
|
|
|
@ -100,7 +100,11 @@ class PublicCollectionDownloadManager {
|
|||
}
|
||||
};
|
||||
|
||||
private async downloadFile(token: string, file: EnteFile) {
|
||||
public async getCachedOriginalFile(file: EnteFile) {
|
||||
return await this.fileObjectURLPromise.get(file.id.toString());
|
||||
}
|
||||
|
||||
async downloadFile(token: string, file: EnteFile) {
|
||||
const worker = await new CryptoWorker();
|
||||
if (!token) {
|
||||
return null;
|
||||
|
|
|
@ -22,6 +22,7 @@ import {
|
|||
FILE_TYPE,
|
||||
VISIBILITY_STATE,
|
||||
} from 'constants/file';
|
||||
import PublicCollectionDownloadManager from 'services/publicCollectionDownloadManager';
|
||||
|
||||
export function downloadAsFile(filename: string, content: string) {
|
||||
const file = new Blob([content], {
|
||||
|
@ -39,19 +40,46 @@ export function downloadAsFile(filename: string, content: string) {
|
|||
a.remove();
|
||||
}
|
||||
|
||||
export async function downloadFile(file: EnteFile) {
|
||||
export async function downloadFile(
|
||||
file: EnteFile,
|
||||
accessedThroughSharedURL: boolean,
|
||||
token?: string
|
||||
) {
|
||||
let fileURL: string;
|
||||
let tempURL: string;
|
||||
const a = document.createElement('a');
|
||||
a.style.display = 'none';
|
||||
let fileURL = await DownloadManager.getCachedOriginalFile(file);
|
||||
let tempURL;
|
||||
if (!fileURL) {
|
||||
tempURL = URL.createObjectURL(
|
||||
await new Response(await DownloadManager.downloadFile(file)).blob()
|
||||
if (accessedThroughSharedURL) {
|
||||
fileURL = await PublicCollectionDownloadManager.getCachedOriginalFile(
|
||||
file
|
||||
);
|
||||
fileURL = tempURL;
|
||||
tempURL;
|
||||
if (!fileURL) {
|
||||
tempURL = URL.createObjectURL(
|
||||
await new Response(
|
||||
await PublicCollectionDownloadManager.downloadFile(
|
||||
token,
|
||||
file
|
||||
)
|
||||
).blob()
|
||||
);
|
||||
console.log({ tempURL });
|
||||
fileURL = tempURL;
|
||||
}
|
||||
} else {
|
||||
fileURL = await DownloadManager.getCachedOriginalFile(file);
|
||||
if (!fileURL) {
|
||||
tempURL = URL.createObjectURL(
|
||||
await new Response(
|
||||
await DownloadManager.downloadFile(file)
|
||||
).blob()
|
||||
);
|
||||
fileURL = tempURL;
|
||||
}
|
||||
}
|
||||
|
||||
const fileType = getFileExtension(file.metadata.title);
|
||||
let tempEditedFileURL;
|
||||
let tempEditedFileURL: string;
|
||||
if (
|
||||
file.pubMagicMetadata?.data.editedTime &&
|
||||
(fileType === TYPE_JPEG || fileType === TYPE_JPG)
|
||||
|
@ -501,7 +529,7 @@ export function getNonTrashedUniqueUserFiles(files: EnteFile[]) {
|
|||
export async function downloadFiles(files: EnteFile[]) {
|
||||
for (const file of files) {
|
||||
try {
|
||||
await downloadFile(file);
|
||||
await downloadFile(file, false);
|
||||
} catch (e) {
|
||||
logError(e, 'download fail for file');
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue