|
| 1 | +/** |
| 2 | + * @copyright Copyright (c) 2023 Ferdinand Thiessen <opensource@fthiessen.de> |
| 3 | + * |
| 4 | + * @author Ferdinand Thiessen <opensource@fthiessen.de> |
| 5 | + * |
| 6 | + * @license AGPL-3.0-or-later |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU Affero General Public License as |
| 10 | + * published by the Free Software Foundation, either version 3 of the |
| 11 | + * License, or (at your option) any later version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * GNU Affero General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Affero General Public License |
| 19 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | + * |
| 21 | + */ |
| 22 | + |
| 23 | +import type { Upload } from '@nextcloud/upload' |
| 24 | +import type { FileStat, ResponseDataDetailed } from 'webdav' |
| 25 | + |
| 26 | +import { showError } from '@nextcloud/dialogs' |
| 27 | +import { emit } from '@nextcloud/event-bus' |
| 28 | +import { davGetClient, davGetDefaultPropfind, davResultToNode, davRootPath } from '@nextcloud/files' |
| 29 | +import { translate as t } from '@nextcloud/l10n' |
| 30 | +import { getUploader } from '@nextcloud/upload' |
| 31 | +import logger from '../logger.js' |
| 32 | + |
| 33 | +export const handleDrop = async (data: DataTransfer) => { |
| 34 | + // TODO: Maybe handle `getAsFileSystemHandle()` in the future |
| 35 | + |
| 36 | + const uploads = [] as Upload[] |
| 37 | + for (const item of data.items) { |
| 38 | + if (item.kind !== 'file') { |
| 39 | + logger.debug('Skipping dropped item', { kind: item.kind, type: item.type }) |
| 40 | + continue |
| 41 | + } |
| 42 | + |
| 43 | + // MDN recommends to try both, as it might be renamed in the future |
| 44 | + const entry = (item as unknown as { getAsEntry?: () => FileSystemEntry|undefined})?.getAsEntry?.() ?? item.webkitGetAsEntry() |
| 45 | + |
| 46 | + // Handle browser issues if Filesystem API is not available. Fallback to File API |
| 47 | + if (entry === null) { |
| 48 | + logger.debug('Could not get FilesystemEntry of item, falling back to file') |
| 49 | + const file = item.getAsFile() |
| 50 | + if (file === null) { |
| 51 | + logger.warn('Could not process DataTransferItem', { type: item.type, kind: item.kind }) |
| 52 | + showError(t('files', 'One of the dropped files could not be processed')) |
| 53 | + } else { |
| 54 | + uploads.push(await handleFileUpload(file)) |
| 55 | + } |
| 56 | + } else { |
| 57 | + logger.debug('Handle recursive upload', { entry: entry.name }) |
| 58 | + // Use Filesystem API |
| 59 | + uploads.push(...await handleRecursiveUpload(entry)) |
| 60 | + } |
| 61 | + } |
| 62 | + return uploads |
| 63 | +} |
| 64 | + |
| 65 | +const handleFileUpload = async (file: File, path: string = '') => { |
| 66 | + const uploader = getUploader() |
| 67 | + |
| 68 | + try { |
| 69 | + return await uploader.upload(`${path}${file.name}`, file) |
| 70 | + } catch (e) { |
| 71 | + showError(t('files', 'Uploading "{filename}" failed', { filename: file.name })) |
| 72 | + throw e |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +const handleRecursiveUpload = async (entry: FileSystemEntry, path: string = ''): Promise<Upload[]> => { |
| 77 | + if (entry.isFile) { |
| 78 | + return [ |
| 79 | + await new Promise<Upload>((resolve, reject) => { |
| 80 | + (entry as FileSystemFileEntry).file( |
| 81 | + async (file) => resolve(await handleFileUpload(file, path)), |
| 82 | + (error) => reject(error), |
| 83 | + ) |
| 84 | + }), |
| 85 | + ] |
| 86 | + } else { |
| 87 | + const directory = entry as FileSystemDirectoryEntry |
| 88 | + logger.debug('Handle directory recursivly', { name: directory.name }) |
| 89 | + |
| 90 | + // TODO: Implement this on `@nextcloud/upload` |
| 91 | + const absolutPath = `${davRootPath}${getUploader().destination.path}${path}${directory.name}` |
| 92 | + const davClient = davGetClient() |
| 93 | + const dirExists = await davClient.exists(absolutPath) |
| 94 | + if (!dirExists) { |
| 95 | + logger.debug('Directory does not exist, creating it', { absolutPath }) |
| 96 | + await davClient.createDirectory(absolutPath, { recursive: true }) |
| 97 | + const stat = await davClient.stat(absolutPath, { details: true, data: davGetDefaultPropfind() }) as ResponseDataDetailed<FileStat> |
| 98 | + emit('files:node:created', davResultToNode(stat.data)) |
| 99 | + } |
| 100 | + |
| 101 | + const entries = await readDirectory(directory) |
| 102 | + // sorted so we upload files first before starting next level |
| 103 | + const promises = entries.sort((a) => a.isFile ? -1 : 1) |
| 104 | + .map((file) => handleRecursiveUpload(file, `${path}${directory.name}/`)) |
| 105 | + return (await Promise.all(promises)).flat() |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +/** |
| 110 | + * Read a directory using Filesystem API |
| 111 | + * @param directory the directory to read |
| 112 | + */ |
| 113 | +function readDirectory(directory: FileSystemDirectoryEntry) { |
| 114 | + const dirReader = directory.createReader() |
| 115 | + |
| 116 | + return new Promise<FileSystemEntry[]>((resolve, reject) => { |
| 117 | + const entries = [] as FileSystemEntry[] |
| 118 | + const getEntries = () => { |
| 119 | + dirReader.readEntries((results) => { |
| 120 | + if (results.length) { |
| 121 | + entries.push(...results) |
| 122 | + getEntries() |
| 123 | + } else { |
| 124 | + resolve(entries) |
| 125 | + } |
| 126 | + }, (error) => { |
| 127 | + reject(error) |
| 128 | + }) |
| 129 | + } |
| 130 | + |
| 131 | + getEntries() |
| 132 | + }) |
| 133 | +} |
0 commit comments