Skip to content

TagSpaces is an offline, open source, document manager with tagging support

Add download menu to the files context menu in the default perspective (#2127)

Stanimir6b58598162bc
diff --git a/src/renderer/components/EntryContainer.tsx b/src/renderer/components/EntryContainer.tsx
index 8306d9cc3..f603a2415 100644
--- a/src/renderer/components/EntryContainer.tsx
+++ b/src/renderer/components/EntryContainer.tsx
@@ -945,8 +945,6 @@ function EntryContainer() {
saveAs={saveAs}
override={override}
/>
- {/* eslint-disable-next-line jsx-a11y/anchor-has-content,jsx-a11y/anchor-is-valid */}
- <a href="#" id="downloadFile" />
</GlobalHotKeys>
);
}
diff --git a/src/renderer/components/EntryContainerMenu.tsx b/src/renderer/components/EntryContainerMenu.tsx
index cb26f8ee4..db9f03f2a 100644
--- a/src/renderer/components/EntryContainerMenu.tsx
+++ b/src/renderer/components/EntryContainerMenu.tsx
@@ -41,6 +41,7 @@ import { useNotificationContext } from '-/hooks/useNotificationContext';
import { useIOActionsContext } from '-/hooks/useIOActionsContext';
import {
createNewInstance,
+ downloadFile,
openDirectoryMessage,
openFileMessage,
} from '-/services/utils-io';
@@ -82,87 +83,6 @@ function EntryContainerMenu(props: Props) {
const [isDeleteEntryModalOpened, setDeleteEntryModalOpened] =
useState<boolean>(false);
- const downloadCordova = (uri, filename) => {
- const { Downloader } = window.plugins;
-
- const downloadSuccessCallback = (result) => {
- // result is an object
- /* {
- path: "file:///storage/sdcard0/documents/My Pdf.pdf", // Returns full file path
- file: "My Pdf.pdf", // Returns Filename
- folder: "documents" // Returns folder name
- } */
- console.log(result.file); // My Pdf.pdf
- };
-
- const downloadErrorCallback = (error) => {
- console.log(error);
- };
-
- const options = {
- title: 'Downloading File:' + filename, // Download Notification Title
- url: uri, // File Url
- path: filename, // The File Name with extension
- description: 'The file is downloading', // Download description Notification String
- visible: true, // This download is visible and shows in the notifications while in progress and after completion.
- folder: 'documents', // Folder to save the downloaded file, if not exist it will be created
- };
-
- Downloader.download(
- options,
- downloadSuccessCallback,
- downloadErrorCallback,
- );
- };
-
- function downloadFile() {
- const entryName = `${baseName(
- openedEntry.path,
- currentLocation?.getDirSeparator(),
- )}`;
- const fileName = extractFileName(
- entryName,
- currentLocation?.getDirSeparator(),
- );
-
- if (AppConfig.isCordova) {
- if (openedEntry.url) {
- downloadCordova(openedEntry.url, entryName);
- } else {
- console.log('Can only download HTTP/HTTPS URIs');
- showNotification(t('core:cantDownloadLocalFile'));
- }
- } else {
- const downloadLink = document.getElementById('downloadFile');
- if (downloadLink) {
- if (AppConfig.isWeb) {
- // eslint-disable-next-line no-restricted-globals
- const { protocol } = location;
- // eslint-disable-next-line no-restricted-globals
- const { hostname } = location;
- // eslint-disable-next-line no-restricted-globals
- const { port } = location;
- const link = `${protocol}//${hostname}${
- port !== '' ? `:${port}` : ''
- }/${openedEntry.path}`;
- downloadLink.setAttribute('href', link);
- } else {
- downloadLink.setAttribute('href', `file:///${openedEntry.path}`);
- }
-
- if (openedEntry.url) {
- // mostly the s3 case
- downloadLink.setAttribute('target', '_blank');
- downloadLink.setAttribute('href', openedEntry.url);
- }
-
- downloadLink.setAttribute('download', fileName); // works only for same origin
- downloadLink.click();
- }
- }
- handleClose();
- }
-
const navigateToFolder = () => {
if (openedEntry.isFile) {
openLink(sharingParentFolderLink);
@@ -217,7 +137,17 @@ function EntryContainerMenu(props: Props) {
key={'downloadFileKey'}
data-tid="downloadFileTID"
aria-label={t('core:downloadFile')}
- onClick={() => downloadFile()}
+ onClick={() => {
+ const downloadResult = downloadFile(
+ openedEntry.path,
+ openedEntry.url,
+ currentLocation?.getDirSeparator(),
+ );
+ if (downloadResult === -1) {
+ showNotification(t('core:cantDownloadLocalFile'));
+ }
+ handleClose();
+ }}
>
<ListItemIcon>
<DownloadIcon />
diff --git a/src/renderer/components/FolderContainer.tsx b/src/renderer/components/FolderContainer.tsx
index 5d3156143..b99f91a4a 100644
--- a/src/renderer/components/FolderContainer.tsx
+++ b/src/renderer/components/FolderContainer.tsx
@@ -311,6 +311,8 @@ function FolderContainer(props: Props) {
</div>
<div style={{ minHeight: '100%', width: '100%' }}>
{/*<LoadingAnimation />*/}
+ {/* eslint-disable-next-line jsx-a11y/anchor-has-content,jsx-a11y/anchor-is-valid */}
+ <a href="#" id="downloadFile" />
<RenderPerspective openRenameEntryDialog={openRenameEntryDialog} />
{isRenameEntryDialogOpened && (
<RenameEntryDialog
diff --git a/src/renderer/components/dialogs/components/CreateDirectory.tsx b/src/renderer/components/dialogs/components/CreateDirectory.tsx
index 584a9388b..110eaae38 100644
--- a/src/renderer/components/dialogs/components/CreateDirectory.tsx
+++ b/src/renderer/components/dialogs/components/CreateDirectory.tsx
@@ -256,7 +256,7 @@ function CreateDirectory(props: Props) {
}}
>
<Button
- data-tid={tid('cancelRenameEntryTID')}
+ data-tid={tid('downloadFileUrlTID')}
className={classes.createButton}
onClick={() => downloadURL()}
>
diff --git a/src/renderer/components/menus/FileMenu.tsx b/src/renderer/components/menus/FileMenu.tsx
index e2b0b3508..0ea2fe93e 100644
--- a/src/renderer/components/menus/FileMenu.tsx
+++ b/src/renderer/components/menus/FileMenu.tsx
@@ -24,7 +24,7 @@ import { Menu, MenuList, MenuItem } from '@mui/material';
import Divider from '@mui/material/Divider';
import OpenFile from '@mui/icons-material/SubdirectoryArrowRight';
import OpenFileNatively from '@mui/icons-material/Launch';
-import { ParentFolderIcon } from '-/components/CommonIcons';
+import { ParentFolderIcon, DownloadIcon } from '-/components/CommonIcons';
import OpenFolderInternally from '@mui/icons-material/Folder';
import MoveCopy from '@mui/icons-material/FileCopy';
import MoveToTopIcon from '@mui/icons-material/VerticalAlignTop';
@@ -41,6 +41,7 @@ import {
} from '@tagspaces/tagspaces-common/paths';
import {
createNewInstance,
+ downloadFile,
getRelativeEntryPath,
openDirectoryMessage,
} from '-/services/utils-io';
@@ -535,6 +536,32 @@ function FileMenu(props: Props) {
<ListItemText primary={t('core:copySharingLink')} />
</MenuItem>,
);
+
+ menuItems.push(
+ <MenuItem
+ key="downloadFileUrl"
+ data-tid="downloadFileUrlTID"
+ onClick={() => {
+ const url = currentLocation.haveObjectStoreSupport()
+ ? currentLocation.generateURLforPath(selectedFilePath, 86400)
+ : undefined;
+ const downloadResult = downloadFile(
+ selectedFilePath,
+ url,
+ currentLocation?.getDirSeparator(),
+ );
+ if (downloadResult === -1) {
+ showNotification(t('core:cantDownloadLocalFile'));
+ }
+ onClose();
+ }}
+ >
+ <ListItemIcon>
+ <DownloadIcon />
+ </ListItemIcon>
+ <ListItemText primary={t('core:downloadFile')} />
+ </MenuItem>,
+ );
}
if (selectedEntries.length < 2) {
diff --git a/src/renderer/hooks/DirectoryContentContextProvider.tsx b/src/renderer/hooks/DirectoryContentContextProvider.tsx
index 6f7e86a9c..1bb2a65c1 100644
--- a/src/renderer/hooks/DirectoryContentContextProvider.tsx
+++ b/src/renderer/hooks/DirectoryContentContextProvider.tsx
@@ -1198,6 +1198,9 @@ export const DirectoryContentContextProvider = ({
dirEntries: TS.FileSystemEntry[],
pageFiles?: TS.FileSystemEntry[],
): Promise<TS.FileSystemEntry[]> {
+ if (!currentLocation) {
+ return Promise.resolve([]);
+ }
return currentLocation
.listMetaDirectoryPromise(directoryPath)
.then((meta) => {
diff --git a/src/renderer/hooks/IOActionsContextProvider.tsx b/src/renderer/hooks/IOActionsContextProvider.tsx
index e6af017f6..f1b1b6637 100644
--- a/src/renderer/hooks/IOActionsContextProvider.tsx
+++ b/src/renderer/hooks/IOActionsContextProvider.tsx
@@ -752,7 +752,7 @@ export const IOActionsContextProvider = ({
}
/**
- * S3 TODO work for test files only
+ * S3 TODO deprecated fetch must be moved in main thread
* @param url
* @param targetPath
* @param onDownloadProgress
diff --git a/src/renderer/services/utils-io.ts b/src/renderer/services/utils-io.ts
index 8ff98d039..3a7319c20 100644
--- a/src/renderer/services/utils-io.ts
+++ b/src/renderer/services/utils-io.ts
@@ -19,7 +19,8 @@ import { getUuid } from '@tagspaces/tagspaces-common/utils-io';
import { saveAs } from 'file-saver';
import { prepareTagForExport } from '@tagspaces/tagspaces-common/misc';
import {
- extractFileExtension,
+ baseName,
+ extractFileName,
cleanTrailingDirSeparator,
cleanFrontDirSeparator,
} from '@tagspaces/tagspaces-common/paths';
@@ -980,6 +981,92 @@ export function getDevicePaths(): Promise<any> {
}
}
+/**
+ * @param filePath
+ * @param fileUrl
+ * @param dirSeparator
+ * return 0- succeeded; -1 -error cantDownloadLocalFile; 1 - unknown error
+ */
+export function downloadFile(
+ filePath: string,
+ fileUrl: string,
+ dirSeparator: string,
+): number {
+ const entryName = `${baseName(filePath, dirSeparator)}`;
+ const fileName = extractFileName(entryName, dirSeparator);
+
+ if (AppConfig.isCordova) {
+ if (fileUrl) {
+ const downloadCordova = (uri, filename) => {
+ const { Downloader } = window.plugins;
+
+ const downloadSuccessCallback = (result) => {
+ // result is an object
+ /* {
+ path: "file:///storage/sdcard0/documents/My Pdf.pdf", // Returns full file path
+ file: "My Pdf.pdf", // Returns Filename
+ folder: "documents" // Returns folder name
+ } */
+ console.log(result.file); // My Pdf.pdf
+ };
+
+ const downloadErrorCallback = (error) => {
+ console.log(error);
+ };
+
+ const options = {
+ title: 'Downloading File:' + filename, // Download Notification Title
+ url: uri, // File Url
+ path: filename, // The File Name with extension
+ description: 'The file is downloading', // Download description Notification String
+ visible: true, // This download is visible and shows in the notifications while in progress and after completion.
+ folder: 'documents', // Folder to save the downloaded file, if not exist it will be created
+ };
+
+ Downloader.download(
+ options,
+ downloadSuccessCallback,
+ downloadErrorCallback,
+ );
+ };
+ downloadCordova(fileUrl, entryName);
+ } else {
+ console.log('Can only download HTTP/HTTPS URIs');
+ return -1;
+ //showNotification(t('core:cantDownloadLocalFile'));
+ }
+ } else {
+ const downloadLink = document.getElementById('downloadFile');
+ if (downloadLink) {
+ if (AppConfig.isWeb) {
+ // eslint-disable-next-line no-restricted-globals
+ const { protocol } = location;
+ // eslint-disable-next-line no-restricted-globals
+ const { hostname } = location;
+ // eslint-disable-next-line no-restricted-globals
+ const { port } = location;
+ const link = `${protocol}//${hostname}${
+ port !== '' ? `:${port}` : ''
+ }/${filePath}`;
+ downloadLink.setAttribute('href', link);
+ } else {
+ downloadLink.setAttribute('href', `file:///${filePath}`);
+ }
+
+ if (fileUrl) {
+ // mostly the s3 case
+ downloadLink.setAttribute('target', '_blank');
+ downloadLink.setAttribute('href', fileUrl);
+ }
+
+ downloadLink.setAttribute('download', fileName); // works only for same origin
+ downloadLink.click();
+ return 0;
+ }
+ }
+ return 1;
+}
+
export function selectDirectoryDialog(): Promise<any> {
if (AppConfig.isElectron) {
return window.electronIO.ipcRenderer.invoke('selectDirectoryDialog');