1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-23 20:37:42 +00:00

refactor: download (#7388)

* refactor: download

* feat: update code

* feat: rebase bug

* feat: rebase bug

---------

Co-authored-by: 杨国璇 <ygx@Hello-word.local>
Co-authored-by: 杨国璇 <ygx@192.168.1.9>
Co-authored-by: 杨国璇 <ygx@MacBookPro.lan>
This commit is contained in:
杨国璇
2025-05-29 16:20:20 +08:00
committed by GitHub
parent d7b10849da
commit 12840046cd
12 changed files with 346 additions and 476 deletions

View File

@@ -0,0 +1,83 @@
import React, { useContext, useEffect, useCallback, useState, useRef } from 'react';
import { useGoFileserver, fileServerRoot } from '../utils/constants';
import { Utils } from '../utils/utils';
import { seafileAPI } from '../utils/seafile-api';
import URLDecorator from '../utils/url-decorator';
import ModalPortal from '../components/modal-portal';
import ZipDownloadDialog from '../components/dialog/zip-download-dialog';
import toaster from '../components/toast';
import { EVENT_BUS_TYPE } from '../components/common/event-bus-type';
// This hook provides content about download
const DownloadFileContext = React.createContext(null);
export const DownloadFileProvider = ({ repoID, eventBus, children }) => {
const [isZipDialogOpen, setZipDialogOpen] = useState();
const pathRef = useRef('');
const direntListRef = useRef([]);
const handelDownload = useCallback((path, direntList = []) => {
const direntCount = direntList.length;
if (direntCount === 0) return;
if (direntCount === 1 && !direntList[0].is_dir) {
const direntPath = Utils.joinPath(path, direntList[0].name);
const url = URLDecorator.getUrl({ type: 'download_file_url', repoID: repoID, filePath: direntPath });
location.href = url;
return;
}
direntListRef.current = direntList.map(dirent => dirent.name);
if (!useGoFileserver) {
pathRef.current = path;
setZipDialogOpen(true);
return;
}
seafileAPI.zipDownload(repoID, path, direntListRef.current).then((res) => {
const zipToken = res.data['zip_token'];
location.href = `${fileServerRoot}zip/${zipToken}`;
}).catch((error) => {
const errorMsg = Utils.getErrorMsg(error);
toaster.danger(errorMsg);
});
}, [repoID]);
const cancelDownload = useCallback(() => {
setZipDialogOpen(false);
pathRef.current = '';
direntListRef.current = [];
}, []);
useEffect(() => {
const unsubscribeDownloadFile = eventBus.subscribe(EVENT_BUS_TYPE.DOWNLOAD_FILE, handelDownload);
return () => {
unsubscribeDownloadFile();
};
}, [eventBus, handelDownload]);
return (
<DownloadFileContext.Provider value={{ eventBus, handelDownload }}>
{children}
{isZipDialogOpen && (
<ModalPortal>
<ZipDownloadDialog
repoID={repoID}
path={pathRef.current}
target={direntListRef.current}
toggleDialog={cancelDownload}
/>
</ModalPortal>
)}
</DownloadFileContext.Provider>
);
};
export const useDownloadFile = () => {
const context = useContext(DownloadFileContext);
if (!context) {
throw new Error('\'DownloadFileContext\' is null');
}
return context;
};

View File

@@ -1 +1,2 @@
export { MetadataStatusProvider, useMetadataStatus } from './metadata-status';
export { DownloadFileProvider } from './download-file';