diff --git a/public/index.html b/public/index.html
index 70eda5b47..fc412032b 100644
--- a/public/index.html
+++ b/public/index.html
@@ -508,6 +508,26 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/public/lang/de.json b/public/lang/de.json
index 98d60a907..517e8f5ae 100644
--- a/public/lang/de.json
+++ b/public/lang/de.json
@@ -28,6 +28,10 @@
"close": "Schließen",
"accept": "Akzeptieren",
"decline": "Ablehnen",
+ "send-folders-title": "Ordner senden",
+ "send-folders-title-plural": "Ordner senden",
+ "send-folders-as-zip": "Als ZIP-Archiv senden",
+ "send-folders-individually": "Dateien einzeln senden",
"title-image": "Bild",
"title-file": "Datei",
"title-image-plural": "Bilder",
@@ -127,6 +131,8 @@
"unfinished-transfers-warning": "Es wurden noch nicht alle Übertragungen fertiggestellt. Möchtest du PairDrop wirklich schließen?",
"display-name-changed-permanently": "Anzeigename wurde dauerhaft geändert",
"download-successful": "{{descriptor}} heruntergeladen",
+ "folder-empty": "Der abgelegte Ordner enthält keine Dateien",
+ "creating-zip": "ZIP-Archiv wird erstellt…",
"pairing-cleared": "Alle Geräte entkoppelt",
"click-to-show": "Klicken zum Anzeigen",
"online-requirement": "Du musst online sein um Geräte zu koppeln.",
diff --git a/public/lang/en.json b/public/lang/en.json
index 54a05c613..be38ddaa3 100644
--- a/public/lang/en.json
+++ b/public/lang/en.json
@@ -72,6 +72,10 @@
"would-like-to-share": "would like to share",
"accept": "Accept",
"decline": "Decline",
+ "send-folders-title": "Send Folder",
+ "send-folders-title-plural": "Send Folders",
+ "send-folders-as-zip": "Send as ZIP archive",
+ "send-folders-individually": "Send files individually",
"has-sent": "has sent:",
"share": "Share",
"download": "Download",
@@ -126,6 +130,8 @@
"display-name-changed-temporarily": "Display name is changed for this session only",
"display-name-random-again": "Display name is randomly generated again",
"download-successful": "{{descriptor}} downloaded",
+ "folder-empty": "The dropped folder contains no files",
+ "creating-zip": "Creating ZIP archive…",
"pairing-tabs-error": "Pairing two web browser tabs is impossible",
"pairing-success": "Devices paired",
"pairing-not-persistent": "Paired devices are not persistent",
diff --git a/public/scripts/main.js b/public/scripts/main.js
index 4883642f8..d2a7f688a 100644
--- a/public/scripts/main.js
+++ b/public/scripts/main.js
@@ -171,6 +171,7 @@ class PairDrop {
this.receiveFileDialog = new ReceiveFileDialog();
this.receiveRequestDialog = new ReceiveRequestDialog();
this.sendTextDialog = new SendTextDialog();
+ this.sendFoldersDialog = new SendFoldersDialog();
this.receiveTextDialog = new ReceiveTextDialog();
this.pairDeviceDialog = new PairDeviceDialog();
this.clearDevicesDialog = new EditPairedDevicesDialog();
diff --git a/public/scripts/ui.js b/public/scripts/ui.js
index 42da3bc32..e0051fa90 100644
--- a/public/scripts/ui.js
+++ b/public/scripts/ui.js
@@ -179,22 +179,30 @@ class PeersUI {
$peer.ui.setProgress(progress.progress, progress.status)
}
- _onDrop(e) {
+ async _onDrop(e) {
if (this.shareMode.active || Dialog.anyDialogShown()) return;
e.preventDefault();
this._onDragEnd();
- if ($$('x-peer') && $$('x-peer').contains(e.target)) return; // dropped on peer
+ if (e.target.closest && e.target.closest('x-peer')) return; // dropped on peer
- let files = e.dataTransfer.files;
- let text = e.dataTransfer.getData("text");
+ const text = e.dataTransfer.getData("text");
+ const { files, folderNames } = e.dataTransfer.items
+ ? await getFilesFromDataTransferItems(e.dataTransfer.items)
+ : { files: [...e.dataTransfer.files], folderNames: [] };
- // convert FileList to Array
- files = [...files];
+ // reevaluate as the situation may have changed while reading the dropped items
+ if (this.shareMode.active || Dialog.anyDialogShown()) return;
- if (files.length > 0) {
+ if (folderNames.length > 0) {
+ Events.fire('folders-dropped', {
+ files: files,
+ folderNames: folderNames
+ });
+ }
+ else if (files.length > 0) {
Events.fire('activate-share-mode', {
files: files
});
@@ -644,7 +652,7 @@ class PeerUI {
$progress.style.setProperty('--progress', degrees);
}
- _onDrop(e) {
+ async _onDrop(e) {
if (this._shareMode.active || Dialog.anyDialogShown()) return;
e.preventDefault();
@@ -652,10 +660,22 @@ class PeerUI {
this._onDragEnd();
const peerId = this._peer.id;
- const files = e.dataTransfer.files;
const text = e.dataTransfer.getData("text");
+ const { files, folderNames } = e.dataTransfer.items
+ ? await getFilesFromDataTransferItems(e.dataTransfer.items)
+ : { files: [...e.dataTransfer.files], folderNames: [] };
- if (files.length > 0) {
+ // reevaluate as the situation may have changed while reading the dropped items
+ if (this._shareMode.active || Dialog.anyDialogShown()) return;
+
+ if (folderNames.length > 0) {
+ Events.fire('folders-dropped', {
+ files: files,
+ folderNames: folderNames,
+ to: peerId
+ });
+ }
+ else if (files.length > 0) {
Events.fire('files-selected', {
files: files,
to: peerId
@@ -2448,6 +2468,167 @@ class AboutUI {
}
}
+class SendFoldersDialog extends Dialog {
+
+ constructor() {
+ super('send-folders-dialog');
+
+ this.$dialogTitle = this.$el.querySelector('.dialog-title');
+ this.$folderNames = this.$el.querySelector('.folder-names');
+ this.$sendAsZipBtn = this.$el.querySelector('#send-as-zip');
+ this.$sendIndividuallyBtn = this.$el.querySelector('#send-individually');
+
+ this.$sendAsZipBtn.addEventListener('click', _ => this._sendAsZip());
+ this.$sendIndividuallyBtn.addEventListener('click', _ => this._sendIndividually());
+
+ Events.on('folders-dropped', e => this._onFoldersDropped(e.detail));
+ Events.on('keydown', e => this._onKeyDown(e));
+ }
+
+ _onFoldersDropped(detail) {
+ if (!detail.files.length) {
+ Events.fire('notify-user', Localization.getTranslation("notifications.folder-empty"));
+ return;
+ }
+
+ if (this._clearTimeout) clearTimeout(this._clearTimeout);
+
+ this._files = detail.files;
+ this._folderNames = detail.folderNames;
+ this._peerId = detail.to;
+
+ // hide this dialog if the corresponding peer disconnects
+ this.correspondingPeerId = detail.to;
+
+ this.$dialogTitle.innerText = this._folderNames.length > 1
+ ? Localization.getTranslation("dialogs.send-folders-title-plural")
+ : Localization.getTranslation("dialogs.send-folders-title");
+ this.$folderNames.innerText = this._folderNames.join(", ");
+
+ this.show();
+ }
+
+ _onKeyDown(e) {
+ if (!this.isShown() || this._busy) return;
+
+ if (e.code === "Escape") {
+ this.hide();
+ }
+ }
+
+ hide() {
+ super.hide();
+ // release the file references after the hide transition has finished
+ this._clearTimeout = setTimeout(() => {
+ this._files = null;
+ this._folderNames = null;
+ this._peerId = null;
+ }, 300);
+ }
+
+ _setBusy(busy) {
+ this._busy = busy;
+ if (busy) {
+ this.$sendAsZipBtn.setAttribute('disabled', true);
+ this.$sendIndividuallyBtn.setAttribute('disabled', true);
+ }
+ else {
+ this.$sendAsZipBtn.removeAttribute('disabled');
+ this.$sendIndividuallyBtn.removeAttribute('disabled');
+ }
+ }
+
+ _sendFiles(files, peerId) {
+ if (peerId) {
+ if (!$(peerId)) {
+ // peer left while the files were prepared
+ Events.fire('notify-user', Localization.getTranslation("notifications.selected-peer-left"));
+ return;
+ }
+ Events.fire('files-selected', {
+ files: files,
+ to: peerId
+ });
+ }
+ else {
+ Events.fire('activate-share-mode', {
+ files: files
+ });
+ }
+ }
+
+ async _sendAsZip() {
+ const files = this._files;
+ const folderNames = this._folderNames;
+ const peerId = this._peerId;
+
+ if (peerId) {
+ // progress is shown on the peer icon -> hide the dialog immediately
+ this.hide();
+ }
+ else {
+ // keep the dialog open while zipping to prevent other share interactions in the meantime
+ this._setBusy(true);
+ Events.fire('notify-user', {
+ message: Localization.getTranslation("notifications.creating-zip"),
+ persistent: true
+ });
+ }
+
+ // files that were not dropped inside a folder are sent unchanged
+ let filesToSend = [];
+ let totalSize = 0;
+ for (let i = 0; i < files.length; i++) {
+ if (!files[i].relativePath) {
+ filesToSend.push(files[i]);
+ }
+ else {
+ totalSize += files[i].size;
+ }
+ }
+
+ try {
+ let bytesCompleted = 0;
+ for (let i = 0; i < folderNames.length; i++) {
+ const folderFiles = files.filter(file => file.relativePath && file.relativePath.startsWith(`${folderNames[i]}/`));
+ if (!folderFiles.length) continue; // do not send a zip file for an empty folder
+
+ const zipFile = await zipper.createZipFileFromFiles(folderFiles, `${folderNames[i]}.zip`, bytes => {
+ if (!peerId || !totalSize) return;
+ Events.fire('set-progress', {
+ peerId: peerId,
+ progress: (bytesCompleted + bytes) / totalSize,
+ status: 'process'
+ });
+ });
+ for (let j = 0; j < folderFiles.length; j++) {
+ bytesCompleted += folderFiles[j].size;
+ }
+ filesToSend.push(zipFile);
+ }
+ } catch (e) {
+ console.error(e);
+ // zipping failed -> fall back to sending the files individually
+ filesToSend = files;
+ }
+
+ if (!peerId) {
+ this._setBusy(false);
+ this.hide();
+ }
+
+ this._sendFiles(filesToSend, peerId);
+ }
+
+ _sendIndividually() {
+ const files = this._files;
+ const peerId = this._peerId;
+
+ this.hide();
+ this._sendFiles(files, peerId);
+ }
+}
+
class Toast extends Dialog {
constructor() {
super('toast');
diff --git a/public/scripts/util.js b/public/scripts/util.js
index 24e2626df..40021cefb 100644
--- a/public/scripts/util.js
+++ b/public/scripts/util.js
@@ -72,6 +72,21 @@ const zipper = (() => {
addFile(file, options) {
return zipWriter.add(file.name, new zip.BlobReader(file), options);
},
+ async createZipFileFromFiles(files, filename, onbytes) {
+ // Uses a dedicated ZipWriter to not interfere with concurrent usage of the shared zipWriter above
+ // If set, the relativePath of a file read from a dropped directory is used to preserve the directory structure
+ const dedicatedZipWriter = new zip.ZipWriter(new zip.BlobWriter("application/zip"), { bufferedWrite: true, level: 0 });
+ let bytesCompleted = 0;
+ for (let i = 0; i < files.length; i++) {
+ await dedicatedZipWriter.add(files[i].relativePath || files[i].name, new zip.BlobReader(files[i]), {
+ onprogress: (progress) => {
+ if (onbytes) onbytes(bytesCompleted + progress);
+ }
+ });
+ bytesCompleted += files[i].size;
+ }
+ return new File([await dedicatedZipWriter.close()], filename, {type: "application/zip"});
+ },
async getBlobURL() {
if (zipWriter) {
const blobURL = URL.createObjectURL(await zipWriter.close());
@@ -102,6 +117,88 @@ const zipper = (() => {
})();
+async function getFilesFromDataTransferItems(dataTransferItems) {
+ // Reads all files from dropped items including the contents of dropped directories
+ // by using the File and Directory Entries API
+ // Files read from a directory are marked with their path relative to the drop
+ // via the custom property `relativePath`
+
+ // Entries must be gathered synchronously as the DataTransferItemList
+ // is emptied as soon as the drop event handler yields
+ let entries = [];
+ let files = [];
+ for (let i = 0; i < dataTransferItems.length; i++) {
+ const item = dataTransferItems[i];
+ if (item.kind !== 'file') continue;
+
+ const entry = item.webkitGetAsEntry
+ ? item.webkitGetAsEntry()
+ : null;
+
+ if (entry) {
+ entries.push(entry);
+ }
+ else {
+ const file = item.getAsFile();
+ if (file) files.push(file);
+ }
+ }
+
+ let folderNames = [];
+ for (let i = 0; i < entries.length; i++) {
+ const entry = entries[i];
+ if (entry.isDirectory) {
+ folderNames.push(entry.name);
+ await readDirectoryEntryRecursively(entry, `${entry.name}/`, files);
+ }
+ else {
+ try {
+ files.push(await getFileFromFileEntry(entry));
+ } catch (e) {
+ // skip unreadable entries instead of aborting the whole drop
+ console.error(`Could not read dropped file "${entry.name}":`, e);
+ }
+ }
+ }
+
+ return { files: files, folderNames: folderNames };
+}
+
+async function readDirectoryEntryRecursively(directoryEntry, path, files) {
+ const directoryReader = directoryEntry.createReader();
+ let entries;
+ do {
+ try {
+ // readEntries returns at most 100 entries per call and must be called until it returns an empty array
+ entries = await new Promise((resolve, reject) => directoryReader.readEntries(resolve, reject));
+ } catch (e) {
+ // skip unreadable directories instead of aborting the whole drop
+ console.error(`Could not read dropped directory "${path}":`, e);
+ return;
+ }
+ for (let i = 0; i < entries.length; i++) {
+ const entry = entries[i];
+ if (entry.isDirectory) {
+ await readDirectoryEntryRecursively(entry, `${path}${entry.name}/`, files);
+ }
+ else {
+ try {
+ const file = await getFileFromFileEntry(entry);
+ file.relativePath = path + file.name;
+ files.push(file);
+ } catch (e) {
+ // skip unreadable entries instead of aborting the whole drop
+ console.error(`Could not read dropped file "${path}${entry.name}":`, e);
+ }
+ }
+ }
+ } while (entries.length);
+}
+
+function getFileFromFileEntry(fileEntry) {
+ return new Promise((resolve, reject) => fileEntry.file(resolve, reject));
+}
+
const mime = (() => {
const suffixToMimeMap = {