Skip to content

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

locations export (#1296)

Stanimirdf2cbbdaf680
diff --git a/app/components/LocationManager.tsx b/app/components/LocationManager.tsx
index caf180b4a..45c2235ad 100644
--- a/app/components/LocationManager.tsx
+++ b/app/components/LocationManager.tsx
@@ -16,7 +16,7 @@
*
*/
-import React, { useEffect, useState } from 'react';
+import React, { useEffect, useRef, useState } from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { withStyles } from '@material-ui/core/styles';
@@ -39,6 +39,7 @@ import i18n from '../services/i18n';
import AppConfig from '../config';
import LoadingLazy from '-/components/LoadingLazy';
import LocationView from '-/components/LocationView';
+import { Pro } from '-/pro';
const CreateEditLocationDialog = React.lazy(() =>
import(
@@ -62,6 +63,7 @@ interface Props {
toggleOpenLinkDialog: () => void;
setDefaultLocations: () => void;
addLocation: (location: Location, openAfterCreate?: boolean) => void;
+ addLocations: (locations: Array<Location>) => void;
editLocation: () => void;
removeLocation: (location: Location) => void;
}
@@ -74,6 +76,7 @@ type SubFolder = {
};
const LocationManager = (props: Props) => {
+ const fileInputRef = useRef<HTMLInputElement>(null);
const [selectedLocation, setSelectedLocation] = useState<Location>(null);
const [selectedDirectoryPath, setSelectedDirectoryPath] = useState<string>(
null
@@ -97,6 +100,17 @@ const LocationManager = (props: Props) => {
isSelectDirectoryDialogOpened,
setSelectDirectoryDialogOpened
] = useState<boolean>(false);
+ const [
+ isExportLocationsDialogOpened,
+ setExportLocationsDialogOpened
+ ] = useState<boolean>(false);
+ const [importFile, setImportFile] = useState<File>(undefined);
+
+ const ExportLocationsDialog =
+ Pro && Pro.UI ? Pro.UI.ExportLocationsDialog : false;
+
+ const ImportLocationsDialog =
+ Pro && Pro.UI ? Pro.UI.ImportLocationsDialog : false;
useEffect(() => {
if (props.locations.length < 1) {
@@ -120,12 +134,42 @@ const LocationManager = (props: Props) => {
setSelectedDirectoryPath(currentPath);
};
+ function handleFileInputChange(selection: any) {
+ const target = selection.currentTarget;
+ const file = target.files[0];
+ setImportFile(file);
+ /* const reader: any = new FileReader();
+
+ reader.onload = () => {
+ try {
+ const locations = Pro.LocationsExport.importLocations(reader.result);
+ if (locations) {
+ props.importLocations(locations);
+ }
+ } catch (e) {
+ console.error('Error : ', e);
+ }
+ };
+ reader.readAsText(file); */
+ target.value = null;
+ }
+
const { classes } = props;
return (
<div className={classes.panel} style={props.style}>
<CustomLogo />
<LocationManagerMenu
+ importLocations={() => {
+ if (AppConfig.isCordovaAndroid && AppConfig.isCordovaiOS) {
+ // TODO Select directory or file from dialog
+ showSelectDirectoryDialog();
+ } else {
+ fileInputRef.current.click();
+ }
+ }}
+ // importLocations={() => setImportLocationsDialogOpened(true)}
+ exportLocations={() => setExportLocationsDialogOpened(true)}
classes={classes}
openURLExternally={props.openURLExternally}
showCreateLocationDialog={() => {
@@ -229,6 +273,29 @@ const LocationManager = (props: Props) => {
))}
</List>
</div>
+ <input
+ style={{ display: 'none' }}
+ ref={fileInputRef}
+ accept="*"
+ type="file"
+ onChange={handleFileInputChange}
+ />
+ {ExportLocationsDialog && isExportLocationsDialogOpened && (
+ <ExportLocationsDialog
+ open={isExportLocationsDialogOpened}
+ onClose={() => setExportLocationsDialogOpened(false)}
+ locations={props.locations}
+ />
+ )}
+ {ImportLocationsDialog && importFile && (
+ <ImportLocationsDialog
+ open={Boolean(importFile)}
+ onClose={() => setImportFile(undefined)}
+ importFile={importFile}
+ addLocations={props.addLocations}
+ locations={props.locations}
+ />
+ )}
</div>
);
};
@@ -245,6 +312,7 @@ function mapDispatchToProps(dispatch) {
{
setDefaultLocations: LocationActions.setDefaultLocations,
addLocation: LocationActions.addLocation,
+ addLocations: LocationActions.addLocations,
editLocation: LocationActions.editLocation,
removeLocation: LocationActions.removeLocation,
openFileNatively: AppActions.openFileNatively,
diff --git a/app/components/menus/LocationManagerMenu.tsx b/app/components/menus/LocationManagerMenu.tsx
index 34b3ce84b..1c71c2ce9 100644
--- a/app/components/menus/LocationManagerMenu.tsx
+++ b/app/components/menus/LocationManagerMenu.tsx
@@ -22,6 +22,7 @@ import ListItemIcon from '@material-ui/core/ListItemIcon';
import Menu from '@material-ui/core/Menu';
import MenuItem from '@material-ui/core/MenuItem';
import CreateLocationIcon from '@material-ui/icons/CreateNewFolder';
+import ExportImportIcon from '@material-ui/icons/SwapHoriz';
import OpenLinkIcon from '@material-ui/icons/Link';
import HelpIcon from '@material-ui/icons/Help';
import classNames from 'classnames';
@@ -30,9 +31,12 @@ import IconButton from '@material-ui/core/IconButton';
import MoreVertIcon from '@material-ui/icons/MoreVert';
import AppConfig from '-/config';
import i18n from '-/services/i18n';
+import { Pro } from '../../pro';
interface Props {
classes: any;
+ exportLocations: () => void;
+ importLocations: () => void;
showCreateLocationDialog: () => void;
toggleOpenLinkDialog: () => void;
openURLExternally: (url: string, skipConfirmation?: boolean) => void;
@@ -60,7 +64,6 @@ const LocationManagerMenu = (props: Props) => {
<MoreVertIcon />
</IconButton>
</div>
- {/* <div style={{ overflowY: 'hidden' }}> */}
<Menu
anchorEl={locationManagerMenuAnchorEl}
open={Boolean(locationManagerMenuAnchorEl)}
@@ -94,6 +97,34 @@ const LocationManagerMenu = (props: Props) => {
</ListItemIcon>
<ListItemText primary={i18n.t('core:openLink')} />
</MenuItem>
+ {Pro && (
+ <>
+ <MenuItem
+ data-tid="locationManagerMenuExportLocationsTID"
+ onClick={() => {
+ setLocationManagerMenuAnchorEl(null);
+ props.exportLocations();
+ }}
+ >
+ <ListItemIcon>
+ <ExportImportIcon />
+ </ListItemIcon>
+ <ListItemText primary={i18n.t('core:exportLocationTitle')} />
+ </MenuItem>
+ <MenuItem
+ data-tid="locationManagerMenuImportLocationsTID"
+ onClick={() => {
+ setLocationManagerMenuAnchorEl(null);
+ props.importLocations();
+ }}
+ >
+ <ListItemIcon>
+ <ExportImportIcon />
+ </ListItemIcon>
+ <ListItemText primary={i18n.t('core:importLocationTitle')} />
+ </MenuItem>
+ </>
+ )}
<MenuItem
data-tid="locationManagerMenuHelp"
onClick={() => {
diff --git a/app/locales/en/core.json b/app/locales/en/core.json
index 48e83b592..2d2fec861 100644
--- a/app/locales/en/core.json
+++ b/app/locales/en/core.json
@@ -48,6 +48,18 @@
"filePropertiesDescription": "Description",
"directoryPropertiesTitle": "Directory Properties",
"createLocationTitle": "Connect a Location",
+ "exportLocationTitle": "Export Locations",
+ "importLocationTitle": "Import Locations",
+ "ExportLocationsDialogTitle": "Export Locations",
+ "ImportLocationsDialogTitle": "Import Locations",
+ "addAES": "Add AES Encryption",
+ "aesPassword": "Encryption password",
+ "retypePassword": "Retype password",
+ "decryptFile": "Decrypt file",
+ "wrongPasswordError": "Wrong password",
+ "retypePasswordDiffError": "Retyped password does not match",
+ "locationExist": "Location already exist, it will be replaced with this one",
+ "notImportedLocation": "Location will not be imported",
"createLocationPath": "Location Path",
"createLocationName": "Location Name",
"createLocationDefaultPerspective": "Default Perspective",
@@ -498,5 +510,7 @@
"musicFolder": "Music",
"picturesFolder": "Pictures",
"videosFolder": "Videos",
+ "startImportButton": "Import",
+ "startExportButton": "Export",
"-": "-"
}
diff --git a/app/reducers/locations.ts b/app/reducers/locations.ts
index 03a52f827..8b36c4172 100644
--- a/app/reducers/locations.ts
+++ b/app/reducers/locations.ts
@@ -95,7 +95,10 @@ export default (state: Array<Location> = initialState, action: any) => {
{
...state[indexForEditing],
...action.location,
- uuid: action.location.newuuid
+ uuid:
+ action.location.newuuid !== undefined
+ ? action.location.newuuid
+ : action.location.uuid
},
...state.slice(indexForEditing + 1)
];
@@ -179,6 +182,27 @@ export const actions = {
dispatch(AppActions.openLocation(location));
}
},
+ /**
+ * @param arrLocations
+ * @param override = true - if location exist override else skip
+ */
+ addLocations: (arrLocations: Array<Location>, override: boolean = true) => (
+ dispatch: (actions: Object) => void,
+ getState: () => any
+ ) => {
+ arrLocations.forEach((newLocation: Location, idx, array) => {
+ const { locations } = getState();
+ const locationExist: boolean = locations.some(
+ location => location.uuid === newLocation.uuid
+ );
+ const isLast = idx === array.length - 1;
+ if (!locationExist) {
+ dispatch(actions.addLocation(newLocation, isLast));
+ } else if (override) {
+ dispatch(actions.editLocation(newLocation, isLast));
+ }
+ });
+ },
createLocation: (location: Location) => ({
type: types.ADD_LOCATION,
location
@@ -188,7 +212,7 @@ export const actions = {
type: types.MOVE_DOWN_LOCATION,
uuid
}),
- editLocation: (location: Location) => (
+ editLocation: (location: Location, openAfterEdit: boolean = true) => (
dispatch: (actions: Object) => void
) => {
dispatch(actions.changeLocation(location));
@@ -196,17 +220,22 @@ export const actions = {
// disableObjectStoreSupport to revoke objectStoreAPI cached object
PlatformIO.disableObjectStoreSupport();
}
- /**
- * check if location uuid is changed
- */
- if (location.newuuid !== location.uuid) {
- dispatch(
- AppActions.openLocation({ ...location, uuid: location.newuuid })
- );
- } else {
- dispatch(AppActions.openLocation(location));
+ if (openAfterEdit) {
+ /**
+ * check if location uuid is changed
+ */
+ if (
+ location.newuuid !== undefined &&
+ location.newuuid !== location.uuid
+ ) {
+ dispatch(
+ AppActions.openLocation({ ...location, uuid: location.newuuid })
+ );
+ } else {
+ dispatch(AppActions.openLocation(location));
+ }
+ dispatch(AppActions.setReadOnlyMode(location.isReadOnly || false));
}
- dispatch(AppActions.setReadOnlyMode(location.isReadOnly || false));
},
changeLocation: (location: Location) => ({
type: types.EDIT_LOCATION,
diff --git a/package.json b/package.json
index 6ebb48673..15fdc515c 100644
--- a/package.json
+++ b/package.json
@@ -239,12 +239,13 @@
"classnames": "^2.2.6",
"clone": "^2.1.2",
"core-js": "2",
+ "crypto-js": "^4.0.0",
"date-fns": "^2.0.0-beta.4",
"dompurify": "^2.2.2",
"electron-debug": "^3.1.0",
"electron-window-state": "^5.0.3",
"exif-js": "^2.3.0",
- "file-saver": "^1.3.3",
+ "file-saver": "^2.0.5",
"fs-extra": "^9.0.1",
"fuse.js": "^6.4.3",
"geolocation-utils": "^1.2.3",
diff --git a/yarn.lock b/yarn.lock
index 537690e69..bcf0ad6a8 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -5271,6 +5271,11 @@ crypto-browserify@^3.11.0:
randombytes "^2.0.0"
randomfill "^1.0.3"
+crypto-js@^4.0.0:
+ version "4.0.0"
+ resolved "https://registry.yarnpkg.com/crypto-js/-/crypto-js-4.0.0.tgz#2904ab2677a9d042856a2ea2ef80de92e4a36dcc"
+ integrity sha512-bzHZN8Pn+gS7DQA6n+iUmBfl0hO5DJq++QP3U6uTucDtk/0iGpXd/Gg7CGR0p8tJhofJyaKoWBuJI4eAO00BBg==
+
crypto-random-string@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/crypto-random-string/-/crypto-random-string-1.0.0.tgz#a230f64f568310e1498009940790ec99545bca7e"
@@ -7236,10 +7241,10 @@ file-loader@^3.0.1:
loader-utils "^1.0.2"
schema-utils "^1.0.0"
-file-saver@^1.3.3:
- version "1.3.8"
- resolved "https://registry.yarnpkg.com/file-saver/-/file-saver-1.3.8.tgz#e68a30c7cb044e2fb362b428469feb291c2e09d8"
- integrity sha512-spKHSBQIxxS81N/O21WmuXA2F6wppUCsutpzenOeZzOCCJ5gEfcbqJP983IrpLXzYmXnMUa6J03SubcNPdKrlg==
+file-saver@^2.0.5:
+ version "2.0.5"
+ resolved "https://registry.yarnpkg.com/file-saver/-/file-saver-2.0.5.tgz#d61cfe2ce059f414d899e9dd6d4107ee25670c38"
+ integrity sha512-P9bmyZ3h/PRG+Nzga+rbdI4OEpNDzAVyy74uVO9ATgzLK6VtAsYybF/+TOCvrc0MO793d6+42lLyZTw7/ArVzA==
version "1.0.0"