From 2937b8cf94099e7965d22c9b93b8500cb1fb809a Mon Sep 17 00:00:00 2001 From: Aniket-Engg Date: Wed, 8 Feb 2023 14:16:22 +0530 Subject: [PATCH] download file to local device --- apps/remix-ide/src/app/files/fileManager.ts | 12 ++++++++++++ libs/remix-ui/workspace/src/lib/actions/index.ts | 9 +++++++++ .../workspace/src/lib/components/file-explorer.tsx | 9 ++++++--- libs/remix-ui/workspace/src/lib/contexts/index.ts | 1 + .../src/lib/providers/FileSystemProvider.tsx | 7 ++++++- .../workspace/src/lib/remix-ui-workspace.tsx | 2 ++ libs/remix-ui/workspace/src/lib/types/index.ts | 1 + 7 files changed, 37 insertions(+), 4 deletions(-) diff --git a/apps/remix-ide/src/app/files/fileManager.ts b/apps/remix-ide/src/app/files/fileManager.ts index cd9a030476..19cb956d34 100644 --- a/apps/remix-ide/src/app/files/fileManager.ts +++ b/apps/remix-ide/src/app/files/fileManager.ts @@ -1,4 +1,5 @@ 'use strict' +import { saveAs } from 'file-saver' import { Plugin } from '@remixproject/engine' import * as packageJson from '../../../../../package.json' import Registry from '../state/registry' @@ -343,6 +344,17 @@ class FileManager extends Plugin { } } + async download(path) { + try { + const fileName = helper.extractNameFromKey(path) + path = this.normalize(path) + const content: any = await this.readFile(path) + saveAs(new Blob([content]), fileName) + } catch (e) { + throw new Error(e) + } + } + /** * Create a directory * @param {string} path path of the new directory diff --git a/libs/remix-ui/workspace/src/lib/actions/index.ts b/libs/remix-ui/workspace/src/lib/actions/index.ts index 6903ebc5e3..52a127b74f 100644 --- a/libs/remix-ui/workspace/src/lib/actions/index.ts +++ b/libs/remix-ui/workspace/src/lib/actions/index.ts @@ -296,6 +296,15 @@ export const renamePath = async (oldPath: string, newPath: string) => { } } +export const downloadPath = async (path: string) => { + const fileManager = plugin.fileManager + try { + await fileManager.download(path) + } catch (error) { + dispatch(displayPopUp('Oops! An error ocurred while downloading.' + error)) + } +} + export const copyFile = async (src: string, dest: string) => { const fileManager = plugin.fileManager diff --git a/libs/remix-ui/workspace/src/lib/components/file-explorer.tsx b/libs/remix-ui/workspace/src/lib/components/file-explorer.tsx index 09d854eba7..b557ed8d90 100644 --- a/libs/remix-ui/workspace/src/lib/components/file-explorer.tsx +++ b/libs/remix-ui/workspace/src/lib/components/file-explorer.tsx @@ -12,7 +12,6 @@ import { checkSpecialChars, extractNameFromKey, extractParentFromKey, joinPath } import { FileRender } from './file-render' import { Drag } from "@remix-ui/drag-n-drop" import { ROOT_PATH } from '../utils/constants' -import { saveAs } from 'file-saver' export const FileExplorer = (props: FileExplorerProps) => { const { name, contextMenuItems, removedContextMenuItems, files, fileState } = props @@ -175,8 +174,12 @@ export const FileExplorer = (props: FileExplorerProps) => { } } - const downloadPath = (path: string) => { - console.log('downloadPath', path) + const downloadPath = async (path: string) => { + try { + props.dispatchDownloadPath(path) + } catch (error) { + props.modal('Download Failed', 'Unexpected error while downloading: ' + typeof error === 'string' ? error : error.message, 'Close', async () => {}) + } } const uploadFile = (target) => { diff --git a/libs/remix-ui/workspace/src/lib/contexts/index.ts b/libs/remix-ui/workspace/src/lib/contexts/index.ts index 5627acdf0a..76ed084896 100644 --- a/libs/remix-ui/workspace/src/lib/contexts/index.ts +++ b/libs/remix-ui/workspace/src/lib/contexts/index.ts @@ -22,6 +22,7 @@ export const FileSystemContext = createContext<{ dispatchCreateNewFolder: (path: string, rootDir: string) => Promise, dispatchDeletePath: (path: string[]) => Promise, dispatchRenamePath: (oldPath: string, newPath: string) => Promise, + dispatchDownloadPath: (path:string) => Promise, dispatchCopyFile: (src: string, dest: string) => Promise, dispatchCopyFolder: (src: string, dest: string) => Promise, dispatchRunScript: (path: string) => Promise, diff --git a/libs/remix-ui/workspace/src/lib/providers/FileSystemProvider.tsx b/libs/remix-ui/workspace/src/lib/providers/FileSystemProvider.tsx index ffcd77abaa..2921ebe308 100644 --- a/libs/remix-ui/workspace/src/lib/providers/FileSystemProvider.tsx +++ b/libs/remix-ui/workspace/src/lib/providers/FileSystemProvider.tsx @@ -6,7 +6,7 @@ import { Toaster } from '@remix-ui/toaster' // eslint-disable-line import { FileSystemContext } from '../contexts' import { browserReducer, browserInitialState } from '../reducers/workspace' import { initWorkspace, fetchDirectory, removeInputField, deleteWorkspace, clearPopUp, publishToGist, createNewFile, setFocusElement, createNewFolder, - deletePath, renamePath, copyFile, copyFolder, runScript, emitContextMenuEvent, handleClickFile, handleExpandPath, addInputField, createWorkspace, + deletePath, renamePath, downloadPath, copyFile, copyFolder, runScript, emitContextMenuEvent, handleClickFile, handleExpandPath, addInputField, createWorkspace, fetchWorkspaceDirectory, renameWorkspace, switchToWorkspace, uploadFile, handleDownloadFiles, restoreBackupZip, cloneRepository, moveFile, moveFolder, showAllBranches, switchBranch, createNewBranch, checkoutRemoteBranch, createSolidityGithubAction, createTsSolGithubAction, createSlitherGithubAction } from '../actions' @@ -95,6 +95,10 @@ export const FileSystemProvider = (props: WorkspaceProps) => { await renamePath(oldPath, newPath) } + const dispatchDownloadPath = async (path: string) => { + await downloadPath(path) + } + const dispatchCopyFile = async (src: string, dest: string) => { await copyFile(src, dest) } @@ -261,6 +265,7 @@ export const FileSystemProvider = (props: WorkspaceProps) => { dispatchCreateNewFolder, dispatchDeletePath, dispatchRenamePath, + dispatchDownloadPath, dispatchCopyFile, dispatchCopyFolder, dispatchRunScript, diff --git a/libs/remix-ui/workspace/src/lib/remix-ui-workspace.tsx b/libs/remix-ui/workspace/src/lib/remix-ui-workspace.tsx index 7776632c24..ea0e818873 100644 --- a/libs/remix-ui/workspace/src/lib/remix-ui-workspace.tsx +++ b/libs/remix-ui/workspace/src/lib/remix-ui-workspace.tsx @@ -516,6 +516,7 @@ export function Workspace () { toast={global.toast} dispatchDeletePath={global.dispatchDeletePath} dispatchRenamePath={global.dispatchRenamePath} + dispatchDownloadPath={global.dispatchDownloadPath} dispatchUploadFile={global.dispatchUploadFile} dispatchCopyFile={global.dispatchCopyFile} dispatchCopyFolder={global.dispatchCopyFolder} @@ -555,6 +556,7 @@ export function Workspace () { toast={global.toast} dispatchDeletePath={global.dispatchDeletePath} dispatchRenamePath={global.dispatchRenamePath} + dispatchDownloadPath={global.dispatchDownloadPath} dispatchUploadFile={global.dispatchUploadFile} dispatchCopyFile={global.dispatchCopyFile} dispatchCopyFolder={global.dispatchCopyFolder} diff --git a/libs/remix-ui/workspace/src/lib/types/index.ts b/libs/remix-ui/workspace/src/lib/types/index.ts index 439081f94d..b61219a637 100644 --- a/libs/remix-ui/workspace/src/lib/types/index.ts +++ b/libs/remix-ui/workspace/src/lib/types/index.ts @@ -96,6 +96,7 @@ export interface FileExplorerProps { toast: (toasterMsg: string) => void, dispatchDeletePath: (path: string[]) => Promise, dispatchRenamePath: (oldPath: string, newPath: string) => Promise, + dispatchDownloadPath: (path: string) => Promise, dispatchUploadFile: (target?: React.SyntheticEvent, targetFolder?: string) => Promise, dispatchCopyFile: (src: string, dest: string) => Promise, dispatchCopyFolder: (src: string, dest: string) => Promise,