Skip to content

Commit

Permalink
feat: add export file
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanyxh committed May 21, 2024
1 parent d079041 commit a0e6f03
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/assets/svgs/bxs--file-export.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 25 additions & 1 deletion src/filehandle/components/FileContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import AddFileModal from './AddFileModal';
import { FileSystemContext } from './FilePanel';
import styles from './styles/FileContent.module.less';
import type { FileInfo } from '../utils/fileManager';
import { FileType } from '../utils/fileManager';
import { exportDirectory, exportFile, FileType } from '../utils/fileManager';
import { fileOperationWarning } from '../utils/operationWarning';

function MountWebdavModal(props: { open: boolean; close(): void }) {
Expand Down Expand Up @@ -270,6 +270,22 @@ const FileContent: React.FC<IFileContentProps> = (props) => {
});
};

const handleExportFile = async () => {
const curr = selection[0];

if (!curr) return void 0;

try {
await (curr.handle.kind === 'directory'
? exportDirectory(curr.handle)
: exportFile(curr.handle));

success(`已为你导出文件 ${curr.name}。`);
} catch (err) {
error((err as Error).message);
}
};

const handleOk = async (name: string, type: FileType) => {
try {
await create(name, type);
Expand Down Expand Up @@ -334,6 +350,14 @@ const FileContent: React.FC<IFileContentProps> = (props) => {
icon: <Icon icon="ri--import-line" color="var(--color-primary)" />,
onClick: importDirectory
},
{
name: '导出文件',
icon: <Icon icon="bxs--file-export" color="var(--color-primary)" />,
style: {
display: selection.length ? void 0 : 'none'
},
onClick: handleExportFile
},
{
name: '删除',
icon: <Icon icon="material-symbols--delete" color="var(--color-primary)" />,
Expand Down
38 changes: 31 additions & 7 deletions src/filehandle/utils/fileManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ export async function getHandleType(handle: DH | FH) {
}

export async function importFile(directory: DH, options?: FilePickerOptions) {
if (!window.showOpenFilePicker) return false;

const files = await window.showOpenFilePicker(options);

await Promise.all(
Expand All @@ -81,15 +79,32 @@ export async function importFile(directory: DH, options?: FilePickerOptions) {
}

export async function importDirectory(directory: DH, options?: DirectoryPickerOptions) {
if (!window.showDirectoryPicker) return false;

const folder = await window.showDirectoryPicker(options);

await move(folder, await createDirectory(directory, folder.name));

return true;
}

export async function exportFile(file: FH) {
const handle = await window.showSaveFilePicker({
suggestedName: file.name,
types: [{ description: 'export file', accept: {} }]
});

await writeFile(handle, await file.getFile());

return true;
}

export async function exportDirectory(directory: DH) {
const handle = await window.showDirectoryPicker({ mode: 'readwrite' });

await moveDirectoryWithSelf(directory, handle);

return true;
}

export async function writeFile(file: FH, data: FileDataType) {
const writable = await file.createWritable();
await writable.write(data);
Expand Down Expand Up @@ -123,15 +138,24 @@ export async function moveFile(origin: DH, target: DH, name: string, copy = true
return handle;
}

export async function moveDirectoryWithSelf(origin: DH, target: DH) {
const _target = await target.getDirectoryHandle(origin.name, { create: true });

await move(origin, _target);

return true;
}

export async function moveDirectory(origin: DH, target: DH, name: string, copy = true) {
const _origin = await origin.getDirectoryHandle(name);
const _target = await createDirectory(target, name);

for await (const [key, handle] of _origin.entries()) {
const entries = _origin.entries();
for await (const [key, handle] of entries) {
if ((await getHandleType(handle)) === FileType.FILE) {
return await moveFile(_origin, _target, key, copy);
await moveFile(_origin, _target, key, copy);
} else {
return await moveDirectory(_origin, _target, key);
await moveDirectory(_origin, _target, key);
}
}

Expand Down
2 changes: 1 addition & 1 deletion types/filesystem.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ declare interface SaveFilePickerOptions {
}

declare interface Window {
showDirectoryPicker: (options?: DirectoryPickerOptions) => Promise<FileSystemDirectoryHandle>;
showDirectoryPicker(options?: DirectoryPickerOptions): Promise<FileSystemDirectoryHandle>;
showOpenFilePicker(options?: FilePickerOptions): Promise<FileSystemFileHandle[]>;
showSaveFilePicker(options?: SaveFilePickerOptions): Promise<FileSystemFileHandle>;
}

0 comments on commit a0e6f03

Please sign in to comment.