From 6dcd9cf81ea1f08c810f3f3f9c3ea8c6f766619e Mon Sep 17 00:00:00 2001 From: Niklas Date: Fri, 27 Aug 2021 21:16:44 +0200 Subject: [PATCH] download button --- src/components/FileBrowser.tsx | 5 +++++ src/components/FileBrowserContextMenu.tsx | 2 ++ src/functions/downloadFile.ts | 9 +++++++++ 3 files changed, 16 insertions(+) create mode 100644 src/functions/downloadFile.ts diff --git a/src/components/FileBrowser.tsx b/src/components/FileBrowser.tsx index 44ee517..100087f 100644 --- a/src/components/FileBrowser.tsx +++ b/src/components/FileBrowser.tsx @@ -2,6 +2,8 @@ import React from "react" import { useState } from "react" import { useContextMenu } from "react-contexify" import { RouteComponentProps } from "react-router-dom" +import downloadFile from "../functions/downloadFile" +import genDownloadLink from "../functions/genDownloadLink" import normalizeDirPath from "../functions/normalizeDirPath" import uploadFile from "../functions/uploadFile" import { useCopyMutation, useCreateDirMutation, useDeleteDirMutation, useDeleteFileMutation, useMoveMutation, useOpenDirQuery } from "../generated/graphql" @@ -106,6 +108,9 @@ const FileBrowser: React.FC = (props) => { await deleteDirMutation({variables:{path:id}}) refetchDir() break + case Action.FileDownload: + downloadFile(genDownloadLink(id)) + break default: break } diff --git a/src/components/FileBrowserContextMenu.tsx b/src/components/FileBrowserContextMenu.tsx index 1356a23..573afdf 100644 --- a/src/components/FileBrowserContextMenu.tsx +++ b/src/components/FileBrowserContextMenu.tsx @@ -10,6 +10,7 @@ export enum Action { FileCopy, FilePaste, FileMove, + FileDownload, DirDelete } @@ -32,6 +33,7 @@ const FileBrowserContextMenu: React.FC = (props) => { Delete Copy Move + Download Paste diff --git a/src/functions/downloadFile.ts b/src/functions/downloadFile.ts new file mode 100644 index 0000000..bf2fee1 --- /dev/null +++ b/src/functions/downloadFile.ts @@ -0,0 +1,9 @@ +function downloadFile(uri:string): void { + const link = document.createElement("a") + link.download = "" + link.href = uri + link.click() + link.remove() +} + +export default downloadFile