TagSpaces is an offline, open source, document manager with tagging support
Tsn225 refine webdav io.js api (#784)
Stanimir8b8a49e2eafb
diff --git a/app/components/EntryContainer.js b/app/components/EntryContainer.jsindex 492e6a728..4b8908e2d 100644--- a/app/components/EntryContainer.js+++ b/app/components/EntryContainer.js@@ -25,6 +25,7 @@ import Button from '@material-ui/core/Button';import IconButton from '@material-ui/core/IconButton';import EditIcon from '@material-ui/icons/Edit';import moment from 'moment';+import uuidv1 from 'uuid';import SaveIcon from '@material-ui/icons/Save';import CloseIcon from '@material-ui/icons/Close';import FullScreenIcon from '@material-ui/icons/ZoomOutMap';@@ -180,7 +181,8 @@ type State = {selectedItem?: Object,isDeleteEntryModalOpened?: boolean,shouldCopyFile?: boolean,- entryPropertiesSplitSize?: number+ entryPropertiesSplitSize?: number,+ EntryPropertiesKey: string};class EntryContainer extends React.Component<Props, State> {@@ -195,7 +197,8 @@ class EntryContainer extends React.Component<Props, State> {currentEntry: null,selectedItem: {},shouldCopyFile: false,- entryPropertiesSplitSize: 0+ entryPropertiesSplitSize: 0,+ EntryPropertiesKey: uuidv1()};componentDidMount() {@@ -630,16 +633,13 @@ class EntryContainer extends React.Component<Props, State> {aria-label={i18n.t('core:downloadFile')}onClick={() => {const { currentEntry } = this.state;- const date = new Date();const downloadLink = document.getElementById('downloadFile');const entryName = `${baseName(currentEntry.path)}`;- const fileExtension = extractFileExtension(entryName);const fileName = extractFileNameWithoutExt(entryName);- const modifiedEntryName = `${fileName}_${moment(date).format('YYYYMMDD')}~${moment(date).format('hhmmss')}.${fileExtension}`;if (downloadLink) {if (AppConfig.isWeb) {- const link = `${location.protocol}//${location.hostname}${location.port !== '' ? `:${location.port}` : ''}${modifiedEntryName}`;+ const link = `${location.protocol}//${location.hostname}${location.port !== '' ? `:${location.port}` : ''}/${currentEntry.path}`;downloadLink.setAttribute('href', link);} else {downloadLink.setAttribute('href', `file:///${currentEntry.path}`);@@ -776,6 +776,12 @@ class EntryContainer extends React.Component<Props, State> {reloadDocument: this.reloadDocument,};+ resetState = (key) => {+ this.setState({+ [key]: uuidv1()+ });+ };+render() {const { classes } = this.props;const { isEditTagsModalOpened, currentEntry, isFullscreen } = this.state;@@ -966,6 +972,8 @@ class EntryContainer extends React.Component<Props, State> {}<div style={{ overflowY: AppConfig.isFirefox ? 'auto' : 'overlay', maxHeight: '100%' }}><EntryProperties+ key={this.state.EntryPropertiesKey}+ resetState={this.resetState}entryPath={currentEntry.path}shouldReload={currentEntry.shouldReload}addTags={this.props.addTags}diff --git a/app/components/EntryProperties.js b/app/components/EntryProperties.jsindex 674f90dd0..17b25698b 100644--- a/app/components/EntryProperties.js+++ b/app/components/EntryProperties.js@@ -163,7 +163,8 @@ type Props = {showNotification: () => void,addTags: () => void,removeTags: () => void,- removeAllTags: () => void+ removeAllTags: () => void,+ resetState: () => void};type State = {@@ -209,12 +210,13 @@ class EntryProperties extends Component<Props, State> {(nextProps.entryPath && nextProps.shouldReload) ||(nextProps.entryPath && this.state.path !== nextProps.entryPath)) {+ this.props.resetState('EntryPropertiesKey');this.loadEntryProperties(nextProps.entryPath);}- if (nextProps.entryPath && this.state.path !== nextProps.entryPath) {- this.setState({ isEditDescription: false });- }+ /* if (nextProps.entryPath && this.state.path !== nextProps.entryPath) {+ this.setState({ isEditDescription: false }); // state is reset isEditDescription default value is false+ } */if (nextProps.shouldCopyFile) {this.setState({ isMoveCopyFilesDialogOpened: true });diff --git a/app/components/TargetFileBox.js b/app/components/TargetFileBox.jsindex c6424a336..0a39586ca 100644--- a/app/components/TargetFileBox.js+++ b/app/components/TargetFileBox.js@@ -44,8 +44,12 @@ const styles = theme => ({});const boxTarget = {- drop(props, monitor, component) {- return component.props.onDrop(props, monitor);+ /**+ * http://react-dnd.github.io/react-dnd/docs/api/drop-target+ */+ drop(props, monitor) { // component)+ // return component.props.onDrop(props, monitor);+ return props.onDrop(props, monitor);},};diff --git a/app/components/dialogs/MoveCopyFilesDialog.js b/app/components/dialogs/MoveCopyFilesDialog.jsindex 76f9ca446..95a107bf2 100644--- a/app/components/dialogs/MoveCopyFilesDialog.js+++ b/app/components/dialogs/MoveCopyFilesDialog.js@@ -152,7 +152,7 @@ class MoveCopyFilesDialog extends React.Component<Props, State> {onChange={e => this.handleInputChange(e)}value={this.state.targetPath}endAdornment={- PlatformIO.haveObjectStoreSupport() ? undefined :+ PlatformIO.haveObjectStoreSupport() || AppConfig.isWeb ? undefined :(<InputAdornment position="end" style={{ height: 33 }}><IconButtondata-tid="openDirectoryMoveCopyDialog"diff --git a/app/containers/MainPage.js b/app/containers/MainPage.jsindex 73f4ee522..bf97619e1 100644--- a/app/containers/MainPage.js+++ b/app/containers/MainPage.js@@ -321,6 +321,7 @@ class MainPage extends Component<Props, State> {true);this.props.reflectCreateEntry(filePath, true);+ this.props.openFile(filePath);return true;}).catch(() => {diff --git a/app/reducers/app.js b/app/reducers/app.jsindex 76defdc45..fc5507744 100644--- a/app/reducers/app.js+++ b/app/reducers/app.js@@ -328,7 +328,7 @@ export default (state: Object = initialState, action: Object) => {});if (!entryAlreadyAdded &&- extractParentDirectoryPath(action.newEntry.path) === state.currentDirectoryPath+ extractParentDirectoryPath(action.newEntry.path).replace(/(^\/)|(\/$)/g, '') === state.currentDirectoryPath.replace(/(^\/)|(\/$)/g, '')) {currentDirectoryEntries = [ // TODO evtl. apply sorting...state.currentDirectoryEntries,diff --git a/app/reducers/io-actions.js b/app/reducers/io-actions.jsindex 68d206335..0043df8d3 100644--- a/app/reducers/io-actions.js+++ b/app/reducers/io-actions.js@@ -25,7 +25,8 @@ import {extractTitle,extractContainingDirectoryPath,getMetaFileLocationForFile,- getThumbFileLocationForFile+ getThumbFileLocationForFile,+ normalizePath} from '../utils/paths';import {@@ -52,7 +53,7 @@ const actions = {}); */const moveJobs = [];paths.map((path) => {- moveJobs.push([path, targetPath + AppConfig.dirSeparator + extractFileName(path)]);+ moveJobs.push([path, normalizePath(targetPath) + AppConfig.dirSeparator + extractFileName(path)]);return true;});renameFilesPromise(moveJobs).then(() => {@@ -81,7 +82,7 @@ const actions = {) => {const copyJobs = [];paths.map((path) => {- copyJobs.push([path, targetPath + AppConfig.dirSeparator + extractFileName(path)]);+ copyJobs.push([path, normalizePath(targetPath) + AppConfig.dirSeparator + extractFileName(path)]);return true;});copyFilesPromise(copyJobs).then(() => {diff --git a/app/services/webdav-io.js b/app/services/webdav-io.jsindex c5d6987f9..9a3da74e0 100644--- a/app/services/webdav-io.js+++ b/app/services/webdav-io.js@@ -18,7 +18,8 @@*/import nl from 'js-webdav-client';-import { extractParentDirectoryPath } from '../utils/paths';+import { extractParentDirectoryPath, normalizePath } from '../utils/paths';+import AppConfig from '../config';export default class WebDAVIO {davClient: any;@@ -32,12 +33,12 @@ export default class WebDAVIO {callback,headers) => {- let /** @type XMLHttpRequest */ ajax =+ const /** @type XMLHttpRequest */ ajax =typeof Components !== 'undefined' &&typeof Components.classes !== 'undefined'? Components.classes[- '@mozilla.org/xmlextras/xmlhttprequest;1'- ].createInstance(Components.interfaces.nsIXMLHttpRequest)+ '@mozilla.org/xmlextras/xmlhttprequest;1'+ ].createInstance(Components.interfaces.nsIXMLHttpRequest): new XMLHttpRequest();if (this._username !== null) {ajax.open(method, url, true, this._username, this._password);@@ -57,12 +58,12 @@ export default class WebDAVIO {if (headers === undefined) {headers = {};}- for (let header in this._headers) {+ for (const header in this._headers) {if (headers[header] === undefined) {ajax.setRequestHeader(header, this._headers[header]);}}- for (let header in headers) {+ for (const header in headers) {ajax.setRequestHeader(header, headers[header]);}return ajax;@@ -89,14 +90,12 @@ export default class WebDAVIO {return paths;};- getAppDataPath = (): string => {+ getAppDataPath = (): string =>// TODO- return 'SOMEPATH_FIX_ME';- };+ 'SOMEPATH_FIX_ME'+ ;- getUserHomePath = (): string => {- return '/';- };+ getUserHomePath = (): string => '/';getNameForPath = (entrypath: string, separator = '/') => {let path = entrypath;@@ -107,10 +106,10 @@ export default class WebDAVIO {return decodeURI(encodedName);};- isDirectory = (path: string): boolean => {+ isDirectory = (path: string): boolean =>// TODO find a better solution- return path.lastIndexOf('/') === path.length - 1;- };+ path.lastIndexOf('/') === path.length - 1+ ;checkStatusCode = (code: number): boolean => {const status = parseInt(code / 100);@@ -140,12 +139,51 @@ export default class WebDAVIO {return directoyTree;};++ listMetaDirectoryPromise = async (path: string): Promise<Array<Object>> => {+ const promise = new Promise((resolve) => {+ const entries = [];+ const metaDirPath = normalizePath(path) + AppConfig.dirSeparator + AppConfig.metaFolder + AppConfig.dirSeparator;+ const davSuccess = (status, data) => {+ const dirList = data._responses;+ for (const dir in dirList) {+ const entryPath = dirList[dir].href;+ if (entryPath.toLowerCase() === metaDirPath.toLowerCase()) {+ console.log('Skipping current folder');+ } else {+ const fileName = this.getNameForPath(entryPath);++ const entry = {};+ entry.name = fileName;+ entry.path = decodeURI(entryPath);+ entry.isFile = true;+ entries.push(entry);+ }+ }+ return resolve(entries);+ };++ this.davClient.propfind(+ metaDirPath,+ davSuccess,+ 1 // 1 , davClient.INFINITY+ );+ return resolve(entries);+ });+ const result = await promise; // this.listDirectoryPromise(normalizePath(path) + AppConfig.dirSeparator + AppConfig.metaFolder + AppConfig.dirSeparator, true);+ return result;+ };+/*** Creates a list with containing the files and the sub directories of a given directory*/- listDirectoryPromise = (directoryPath: string): Promise<Array<Object>> => new Promise((resolve, reject) => {- let anotatedDirList;+ listDirectoryPromise = (directoryPath: string, lite: boolean = false): Promise<Array<Object>> => new Promise(async (resolve, reject) => {+ let enhancedEntries;let dirPath = directoryPath.split('//').join('/');++ const metaContent = !lite ? await this.listMetaDirectoryPromise(directoryPath) : [];++ // let containsMetaFolder = false;const davSuccess = (status, data) => {console.log('Dirlist Status: ' + status);if (!this.checkStatusCode(status)) {@@ -158,9 +196,11 @@ export default class WebDAVIO {let filesize;let lmdt;let path;+ let eentry;- anotatedDirList = [];- for (let entry in dirList) {+ enhancedEntries = [];+ const metaPromises = [];+ for (const entry in dirList) {path = dirList[entry].href;if (dirPath.toLowerCase() === path.toLowerCase()) {console.log('Skipping current folder');@@ -169,36 +209,66 @@ export default class WebDAVIO {filesize = undefined;lmdt = undefined;// console.log(dirList[entry]._namespaces['DAV:']);- if (- typeof dirList[entry]._namespaces['DAV:'].getcontentlength ===- 'undefined' ||- dirList[entry]._namespaces['DAV:'].getcontentlength._xmlvalue- .length === 0 ||- (dirList[entry]._namespaces['DAV:'].resourcetype._xmlvalue- .length === 1 &&- dirList[entry]._namespaces['DAV:'].resourcetype._xmlvalue[0]- .localName === 'collection')- ) {- isDir = true;+ if (this.isFile(dirList[entry]._namespaces['DAV:'])) {+ filesize = dirList[entry]._namespaces['DAV:'].getcontentlength._xmlvalue[0].data;+ lmdt = data._responses[entry]._namespaces['DAV:'].getlastmodified._xmlvalue[0].data;} else {- filesize =- dirList[entry]._namespaces['DAV:'].getcontentlength._xmlvalue[0]- .data;- lmdt =- data._responses[entry]._namespaces['DAV:'].getlastmodified- ._xmlvalue[0].data;+ isDir = true;+ // const metaFilePath = getMetaFileLocationForFile(path, '/');}fileName = this.getNameForPath(path);- anotatedDirList.push({- name: fileName,- isFile: !isDir,- size: filesize,- lmdt: lmdt,- path: decodeURI(path)- });++ eentry = {};+ eentry.name = fileName;+ eentry.path = decodeURI(path);+ eentry.tags = [];+ eentry.thumbPath = '';+ // eentry.meta = {};+ eentry.isFile = !isDir;+ eentry.size = filesize;+ eentry.lmdt = Date.parse(lmdt);+ // const x = getMetaFileLocationForFile(eentry.path);++ if (!lite) {+ if (isDir) { // Read tsm.json from subfolders+ const metaDirAvailable = metaContent.find(obj => obj.name === AppConfig.metaFolder);+ if (metaDirAvailable) {+ metaPromises.push(this.getEntryMeta(eentry, metaDirAvailable.path));+ }+ } else {+ const metaFileAvailable = metaContent.find(obj => obj.name === fileName + AppConfig.metaFileExt);+ if (metaFileAvailable) {+ metaPromises.push(this.getEntryMeta(eentry, metaFileAvailable.path));+ }++ // Finding if thumbnail available+ const metaThumbAvailable = metaContent.find(obj => obj.name === fileName + AppConfig.thumbFileExt);+ if (metaThumbAvailable) {+ eentry.thumbPath = metaThumbAvailable.path;+ }+ }+ }++ enhancedEntries.push(eentry);}}- resolve(anotatedDirList);++ Promise.all(metaPromises).then((entriesMeta) => {+ /* entriesMeta.forEach((entryMeta) => {+ enhancedEntries.some((enhancedEntry) => {+ if (enhancedEntry.path === entryMeta.path) {+ // eslint-disable-next-line no-param-reassign+ enhancedEntry = entryMeta;+ return true;+ }+ return false;+ });+ }); */+ resolve(enhancedEntries);+ return true;+ }).catch(() => {+ resolve(enhancedEntries);+ });};if (dirPath.substring(dirPath.length - 1) !== '/') {dirPath += '/';@@ -212,40 +282,69 @@ export default class WebDAVIO {);});+ isFile = (dav: Object): boolean => !(+ typeof dav.getcontentlength === 'undefined' ||+ dav.getcontentlength._xmlvalue.length === 0 ||+ (dav.resourcetype._xmlvalue.length === 1 && dav.resourcetype._xmlvalue[0].localName === 'collection')+ );++ getEntryMeta = (eentry: Object, metaPath: string): Promise<Object> => {+ if (eentry.isFile) {+ // const metaFilePath = getMetaFileLocationForFile(eentry.path);+ return this.loadTextFilePromise(metaPath).then(result => {+ // eslint-disable-next-line no-param-reassign+ eentry.meta = JSON.parse(result.trim());+ return eentry;+ });+ }+ // const folderMetaPath = normalizePath(eentry.path) + AppConfig.dirSeparator + AppConfig.metaFolderFile; // getMetaFileLocationForDir(eentry.path);+ if (!eentry.path.endsWith(AppConfig.metaFolder + '/')) { // Skip the /.ts folder+ return this.loadTextFilePromise(metaPath).then(result => {+ // eslint-disable-next-line no-param-reassign+ eentry.meta = JSON.parse(result.trim());+ return eentry;+ });+ }++ return new Promise((resolve) => {+ resolve(eentry);+ });+ };/*** Finds out the properties of a file or directory such last modification date or file size*/- getPropertiesPromise = (filePath: string): Promise<Object> => {- return new Promise((resolve, reject) => {- this.davClient.propfind(- encodeURI(filePath),- (status, data) => {- console.log(- 'Properties Status / Content: ' ++ getPropertiesPromise = (filePath: string): Promise<Object> => new Promise((resolve, reject) => {+ this.davClient.propfind(+ encodeURI(filePath),+ (status, data) => {+ console.log(+ 'Properties Status / Content: ' +status +' / ' +JSON.stringify(data._responses)- );- const fileProperties = {};- if (this.checkStatusCode(status)) {- for (let entry in data._responses) {- fileProperties.path = filePath;+ );+ const fileProperties = {};+ if (this.checkStatusCode(status)) {+ for (const entry in data._responses) {+ fileProperties.path = filePath;+ fileProperties.name = this.getNameForPath(filePath);+ const isFile = this.isFile(data._responses[entry]._namespaces['DAV:']);+ fileProperties.isFile = isFile;+ if (isFile) {fileProperties.size =- data._responses[entry]._namespaces['DAV:'].getcontentlength;- fileProperties.lmdt =- data._responses[entry]._namespaces[- 'DAV:'- ].getlastmodified._xmlvalue[0].data;+ data._responses[entry]._namespaces['DAV:'].getcontentlength._xmlvalue[0].data;+ fileProperties.lmdt = Date.parse(data._responses[entry]._namespaces['DAV:'].getlastmodified._xmlvalue[0].data);}- resolve(fileProperties);- } else {- reject('getFileProperties ' + filePath + ' failed ' + status);}- },- 1- );- });- };+ resolve(fileProperties);+ } else {+ resolve(false);+ // reject('getFileProperties ' + filePath + ' failed ' + status);+ }+ },+ 1+ );+ });/*** Load the content of a text file@@ -284,47 +383,45 @@ export default class WebDAVIO {content: string,overWrite: boolean,mode: string- ): Promise<Object> => {- return new Promise((resolve, reject) => {- let isNewFile = false;- this.davClient.propfind(- encodeURI(filePath),- (status, data) => {- console.log(- 'Check file exists: Status / Content: ' + status + ' / ' + data- );- if (parseInt(status) === 404) {- isNewFile = true;- }- if (isNewFile || overWrite === true || mode === 'text') {- this.davClient.put(- encodeURI(filePath),- (status, data, headers) => {- console.log(- 'Creating File Status/Content/Headers: ' ++ ): Promise<Object> => new Promise((resolve, reject) => {+ let isNewFile = false;+ this.davClient.propfind(+ encodeURI(filePath),+ (status, data) => {+ console.log(+ 'Check file exists: Status / Content: ' + status + ' / ' + data+ );+ if (parseInt(status) === 404) {+ isNewFile = true;+ }+ if (isNewFile || overWrite === true || mode === 'text') {+ this.davClient.put(+ encodeURI(filePath),+ (status, data, headers) => {+ console.log(+ 'Creating File Status/Content/Headers: ' +status +' / ' +data +' / ' +headers- );- if (this.checkStatusCode(status)) {- resolve(isNewFile);- } else {- reject('saveFilePromise: ' + filePath + ' failed ' + status);- }- },- content,- 'application/octet-stream'- );- } else {- reject('File Already Exists.');- }- },- 1- );- });- };+ );+ if (this.checkStatusCode(status)) {+ resolve(isNewFile);+ } else {+ reject('saveFilePromise: ' + filePath + ' failed ' + status);+ }+ },+ content,+ 'application/octet-stream'+ );+ } else {+ reject('File Already Exists.');+ }+ },+ 1+ );+ });/*** Persists a given text content to a specified filepathdiff --git a/webpack.config.renderer.web.js b/webpack.config.renderer.web.jsindex 2debc347a..e2fe78ecf 100644--- a/webpack.config.renderer.web.js+++ b/webpack.config.renderer.web.js@@ -21,6 +21,11 @@ export default merge.smart(baseConfig, {publicPath: './dist/'},+ optimization: {+ // We no not want to minimize our code. TODO remove this for production+ minimize: false+ },+node: {fs: 'empty'},