File upload/download without settings

This commit is contained in:
alexweininger 2021-06-04 00:41:12 -07:00
parent 57cb4626e4
commit 6e9d4e7f7b
7 changed files with 1076 additions and 38 deletions

1032
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -345,6 +345,18 @@
"command": "vscode-appwrite.viewMore", "command": "vscode-appwrite.viewMore",
"title": "View more", "title": "View more",
"category": "Appwrite" "category": "Appwrite"
},
{
"command": "vscode-appwrite.uploadFile",
"title": "Upload file to storage",
"icon": "$(cloud-upload)",
"category": "Appwrite"
},
{
"command": "vscode-appwrite.downloadFile",
"title": "Download file",
"icon": "$(cloud-download)",
"category": "Appwrite"
} }
], ],
"views": { "views": {
@ -456,6 +468,11 @@
"command": "vscode-appwrite.createFunction", "command": "vscode-appwrite.createFunction",
"when": "view == Functions", "when": "view == Functions",
"group": "navigation" "group": "navigation"
},
{
"command": "vscode-appwrite.uploadFile",
"when": "view == Storage",
"group": "navigation"
} }
], ],
"view/item/context": [ "view/item/context": [
@ -631,6 +648,11 @@
"command": "vscode-appwrite.CreateTag", "command": "vscode-appwrite.CreateTag",
"when": "viewItem =~ /^tags$/", "when": "viewItem =~ /^tags$/",
"group": "inline" "group": "inline"
},
{
"command": "vscode-appwrite.downloadFile",
"when": "viewItem =~ /^file$/",
"group": "inline"
} }
], ],
"explorer/context": [ "explorer/context": [
@ -775,6 +797,7 @@
"cron-validate": "^1.4.3", "cron-validate": "^1.4.3",
"cronstrue": "^1.113.0", "cronstrue": "^1.113.0",
"dayjs": "^1.10.4", "dayjs": "^1.10.4",
"downloads-folder": "^3.0.1",
"fs-extra": "^9.1.0", "fs-extra": "^9.1.0",
"node-appwrite": "^2.2.3", "node-appwrite": "^2.2.3",
"tar": "^6.1.0" "tar": "^6.1.0"

3
src/appwrite.d.ts vendored
View file

@ -362,6 +362,9 @@ export type StorageClient = {
createFile: (file: any, read?: string[], write?: string[]) => Promise<any>; createFile: (file: any, read?: string[], write?: string[]) => Promise<any>;
listFiles: () => Promise<any>; listFiles: () => Promise<any>;
getFile: (fileId: string) => Promise<any>; getFile: (fileId: string) => Promise<any>;
getFileDownload: (fileId: string) => Promise<any>;
getFileView: (fileId: string) => Promise<any>;
deleteFile: (fileId: string) => Promise<any>;
}; };
type Vars = Record<string, any>; type Vars = Record<string, any>;

View file

@ -14,6 +14,14 @@ export class Storage {
return await AppwriteCall(this.storage.listFiles()); return await AppwriteCall(this.storage.listFiles());
} }
public async getFileDownload(fileId: string): Promise<any> {
return await AppwriteCall(this.storage.getFileDownload(fileId));
}
public async getFileView(fileId: string): Promise<any> {
return await AppwriteCall(this.storage.getFileView(fileId));
}
public async createFile(file: ReadStream): Promise<void> { public async createFile(file: ReadStream): Promise<void> {
return await AppwriteCall(this.storage.createFile(file)); return await AppwriteCall(this.storage.createFile(file));
} }

View file

@ -44,6 +44,8 @@ import { openFunctionTagsInBrowser } from './functions/openFunctionTagsInBrowser
import { viewMore } from './common/viewMore'; import { viewMore } from './common/viewMore';
import { openCollectionInBrowser } from './database/openCollectionInBrowser'; import { openCollectionInBrowser } from './database/openCollectionInBrowser';
import { uploadFile } from './storage/uploadFile';
import { downloadFile } from './storage/downloadFile';
class CommandRegistrar { class CommandRegistrar {
constructor(private readonly context: ExtensionContext) {} constructor(private readonly context: ExtensionContext) {}
@ -115,6 +117,9 @@ export function registerCommands(context: ExtensionContext): void {
/** Storage **/ /** Storage **/
registerCommand("refreshStorage", undefined, "storage"); registerCommand("refreshStorage", undefined, "storage");
registerCommand("uploadFile", uploadFile, "storage");
registerCommand("downloadFile", downloadFile);
registerCommand("openStorageDocumentation", () => openDocumentation("storage")); registerCommand("openStorageDocumentation", () => openDocumentation("storage"));
/** Projects **/ /** Projects **/

View file

@ -0,0 +1,29 @@
import { ProgressLocation, Uri, window, workspace } from "vscode";
import { storageClient } from "../../client";
import * as fs from "fs";
import { FileTreeItem } from "../../tree/storage/FileTreeItem";
import downloadsFolder = require("downloads-folder");
import { File } from "../../appwrite";
import { ProgressMessage } from "../../utils/types";
import { CancellationToken } from "vscode";
export async function downloadFile(item: FileTreeItem | File): Promise<void> {
const file = item instanceof FileTreeItem ? item.file : item;
const data = await window.withProgress(
{ location: ProgressLocation.Notification, title: `Downloading ${file.name}`, cancellable: false },
async (progress: ProgressMessage, _token: CancellationToken): Promise<Buffer> => {
const data = await storageClient?.getFileDownload(file.$id);
progress.report({ message: "Download finished" });
return data;
}
);
const defaultUriBase = workspace.workspaceFolders?.[0].uri ?? Uri.parse(downloadsFolder());
const defaultUri = Uri.joinPath(defaultUriBase, file.name);
const destination = await window.showSaveDialog({ saveLabel: "Save", title: file.name, defaultUri });
if (data && destination) {
await fs.promises.writeFile(destination.fsPath, data);
}
}

View file

@ -0,0 +1,14 @@
import { window } from "vscode";
import { storageClient } from "../../client";
import * as fs from "fs";
export async function uploadFile(): Promise<void> {
try {
const file = await window.showOpenDialog({ canSelectFiles: true, canSelectFolders: false, canSelectMany: false });
if (file) {
await storageClient?.createFile(fs.createReadStream(file[0].fsPath));
}
} catch (e) {
window.showErrorMessage("Failed to upload file.");
}
}