fix public collection files download

This commit is contained in:
Abhinav 2022-01-26 11:50:02 +05:30
parent 333271bfc0
commit 9b47830076
3 changed files with 53 additions and 12 deletions

View file

@ -1,4 +1,4 @@
import React, { useEffect, useRef, useState } from 'react'; import React, { useContext, useEffect, useRef, useState } from 'react';
import Photoswipe from 'photoswipe'; import Photoswipe from 'photoswipe';
import PhotoswipeUIDefault from 'photoswipe/dist/photoswipe-ui-default'; import PhotoswipeUIDefault from 'photoswipe/dist/photoswipe-ui-default';
import classnames from 'classnames'; import classnames from 'classnames';
@ -44,6 +44,7 @@ import EnteSpinner from 'components/EnteSpinner';
import EnteDateTimePicker from 'components/EnteDateTimePicker'; import EnteDateTimePicker from 'components/EnteDateTimePicker';
import { MAX_EDITED_FILE_NAME_LENGTH } from 'constants/file'; import { MAX_EDITED_FILE_NAME_LENGTH } from 'constants/file';
import { sleep } from 'utils/common'; import { sleep } from 'utils/common';
import { PublicCollectionGalleryContext } from 'utils/publicCollectionGallery';
const SmallLoadingSpinner = () => ( const SmallLoadingSpinner = () => (
<EnteSpinner <EnteSpinner
@ -483,6 +484,9 @@ function PhotoSwipe(props: Iprops) {
const [metadata, setMetaData] = useState<EnteFile['metadata']>(null); const [metadata, setMetaData] = useState<EnteFile['metadata']>(null);
const [exif, setExif] = useState<any>(null); const [exif, setExif] = useState<any>(null);
const needUpdate = useRef(false); const needUpdate = useRef(false);
const publicCollectionGalleryContext = useContext(
PublicCollectionGalleryContext
);
useEffect(() => { useEffect(() => {
if (!pswpElement) return; if (!pswpElement) return;
@ -668,7 +672,12 @@ function PhotoSwipe(props: Iprops) {
const downloadFileHelper = async (file) => { const downloadFileHelper = async (file) => {
const { loadingBar } = props; const { loadingBar } = props;
loadingBar.current?.continuousStart(); loadingBar.current?.continuousStart();
await downloadFile(file); await downloadFile(
file,
publicCollectionGalleryContext.accessedThroughSharedURL,
publicCollectionGalleryContext.token
);
loadingBar.current?.complete(); loadingBar.current?.complete();
}; };
const scheduleUpdate = () => (needUpdate.current = true); const scheduleUpdate = () => (needUpdate.current = true);

View file

@ -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(); const worker = await new CryptoWorker();
if (!token) { if (!token) {
return null; return null;

View file

@ -22,6 +22,7 @@ import {
FILE_TYPE, FILE_TYPE,
VISIBILITY_STATE, VISIBILITY_STATE,
} from 'constants/file'; } from 'constants/file';
import PublicCollectionDownloadManager from 'services/publicCollectionDownloadManager';
export function downloadAsFile(filename: string, content: string) { export function downloadAsFile(filename: string, content: string) {
const file = new Blob([content], { const file = new Blob([content], {
@ -39,19 +40,46 @@ export function downloadAsFile(filename: string, content: string) {
a.remove(); 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'); const a = document.createElement('a');
a.style.display = 'none'; a.style.display = 'none';
let fileURL = await DownloadManager.getCachedOriginalFile(file); if (accessedThroughSharedURL) {
let tempURL; fileURL = await PublicCollectionDownloadManager.getCachedOriginalFile(
if (!fileURL) { file
tempURL = URL.createObjectURL(
await new Response(await DownloadManager.downloadFile(file)).blob()
); );
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); const fileType = getFileExtension(file.metadata.title);
let tempEditedFileURL; let tempEditedFileURL: string;
if ( if (
file.pubMagicMetadata?.data.editedTime && file.pubMagicMetadata?.data.editedTime &&
(fileType === TYPE_JPEG || fileType === TYPE_JPG) (fileType === TYPE_JPEG || fileType === TYPE_JPG)
@ -501,7 +529,7 @@ export function getNonTrashedUniqueUserFiles(files: EnteFile[]) {
export async function downloadFiles(files: EnteFile[]) { export async function downloadFiles(files: EnteFile[]) {
for (const file of files) { for (const file of files) {
try { try {
await downloadFile(file); await downloadFile(file, false);
} catch (e) { } catch (e) {
logError(e, 'download fail for file'); logError(e, 'download fail for file');
} }