Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload folders via D&D in the browser workbench #97347

Merged
merged 2 commits into from
May 13, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 38 additions & 12 deletions src/vs/workbench/contrib/files/browser/views/explorerViewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -939,27 +939,53 @@ export class FileDragAndDrop implements ITreeDragAndDrop<ExplorerItem> {
}

private async handleWebExternalDrop(data: DesktopDragAndDropData, target: ExplorerItem, originalEvent: DragEvent): Promise<void> {
data.files.forEach(file => {
const items = (originalEvent as any).dataTransfer.items;
for (let item of items) {
const entry = item.webkitGetAsEntry();
await this.uploadFileEntry(entry, target.resource, target);

if (items.length === 1) {
await this.editorService.openEditor({ resource: joinPath(target.resource, entry.name), options: { pinned: true } });
}
}
}

private async uploadFileEntry(entry: any, parentResource: URI, target: ExplorerItem | undefined): Promise<void> {
const resource = joinPath(parentResource, entry.name);

if (entry.isFile) {
// Handle file upload
if (target && target.getChild(entry.name)) {
const { confirmed } = await this.dialogService.confirm(getFileOverwriteConfirm(resource.path));
if (!confirmed) {
return;
}
}

const file = await new Promise<File>((resolve, reject) => entry.file(resolve, reject));
const reader = new FileReader();
reader.readAsArrayBuffer(file);
reader.onload = async (event) => {
const name = file.name;
if (typeof name === 'string' && event.target?.result instanceof ArrayBuffer) {
if (target.getChild(name)) {
const { confirmed } = await this.dialogService.confirm(getFileOverwriteConfirm(name));
if (!confirmed) {
return;
}
}

const resource = joinPath(target.resource, name);
await this.fileService.writeFile(resource, VSBuffer.wrap(new Uint8Array(event.target.result)));
if (data.files.length === 1) {
await this.editorService.openEditor({ resource, options: { pinned: true } });
}
}
};
});
} else if (entry.isDirectory) {
// Handle folder upload
await this.fileService.createFolder(resource);

// Recursive upload files in this directory
const folderTarget = target && target.getChild(entry.name) || undefined;
const dirReader = entry.createReader();
const childEntries = await new Promise<any[]>((resolve, reject) => {
dirReader.readEntries(resolve, reject);
});
for (let childEntry of childEntries) {
await this.uploadFileEntry(childEntry, resource, folderTarget);
}
}
}

private async handleExternalDrop(data: DesktopDragAndDropData, target: ExplorerItem, originalEvent: DragEvent): Promise<void> {
Expand Down