Skip to content
This repository has been archived by the owner on Aug 19, 2022. It is now read-only.

Commit

Permalink
fix(#87): remove invalid last doc/projects on error
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefanie S committed Jun 23, 2020
1 parent c196cc6 commit d5098f8
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 25 deletions.
7 changes: 5 additions & 2 deletions server/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ def parseToString(self, *msg):
try:
concatted += ' ' + m
except:
m = json.dumps(m)
concatted += ' ' + m
try:
m = json.dumps(m)
concatted += ' ' + m
except:
pass
return concatted
2 changes: 1 addition & 1 deletion server/pathUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def concatPathParts(pathParts):
path = parts[0]
parts.pop(0)
for p in parts:
if p == '':
if p == '' or p == None:
continue
if p.endswith('/'):
p = p[:-1]
Expand Down
9 changes: 7 additions & 2 deletions server/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from flask import request
from flask import request, abort
from flask_restful import Resource
from pathlib import Path
from writerey_config import basePath, metaSubPath
Expand All @@ -24,10 +24,15 @@ def get(self):
'files': []
}
treeBase = request.args.get('base')

log.logDebug('========== GET TREE =========')
log.logDebug('os.getcwd:', os.getcwd())
log.logDebug('base:', treeBase)
log.logDebug('==========')

if (treeBase and not os.path.exists(PathUtils.sanitizePathList([basePath, treeBase]))):
abort(400, 'given treeBase is not part of the dir structure')

for (dirpath, dirnames, filenames) in os.walk(basePath):
filePath = PathUtils.sanitizePathString(dirpath, True)
path = filePath.split('/')
Expand Down Expand Up @@ -62,7 +67,7 @@ def get(self):
result = directoryStructure
if (treeBase):
result = next((x for x in directoryStructure['dirs'] if x['name'] == treeBase), result)

return json.dumps(result)

@staticmethod
Expand Down
49 changes: 31 additions & 18 deletions src/app/services/directory.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { LinkService } from 'src/app/services/link.service';
// Copyright (c) 2020 s-blu
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.


import { MatSnackBar } from '@angular/material/snack-bar';
import { LinkService } from 'src/app/services/link.service';
import { DirectoryStore } from './../stores/directory.store';
import { ProjectStore, LAST_PROJECT_KEY } from './../stores/project.store';
import { catchError, flatMap, map, tap, take, filter } from 'rxjs/operators';
Expand All @@ -27,7 +29,8 @@ export class DirectoryService implements OnDestroy {
private httpClient: HttpClient,
private projectStore: ProjectStore,
private directoryStore: DirectoryStore,
private linkService: LinkService
private linkService: LinkService,
private snackBar: MatSnackBar
) {}

ngOnDestroy() {
Expand Down Expand Up @@ -121,22 +124,32 @@ export class DirectoryService implements OnDestroy {
},
};
if (params) parameter.params = params;

return this.httpClient.get(this.api.getTreeRoute(), parameter).pipe(
map(
(res: any) => {
try {
res = JSON.parse(res);
console.log('got new tree', res);
this.directoryStore.setTree(res);
return res;
} catch (err) {
console.error('Could not parse response of tree route. Will return empty object.');
return {};
}
},
catchError(err => this.api.handleHttpError(err))
)
catchError(err => {
this.snackBar.open(translate('error.couldNotFetchTree', { name: this.project }), '', {
duration: 10000,
});

if (this.project === localStorage.getItem(LAST_PROJECT_KEY)) {
console.warn(
'Was not able to open last project. Will unset last project to avoid future problems.',
this.project
);
localStorage.removeItem(LAST_PROJECT_KEY);
}

return this.api.handleHttpError(err);
}),
map((res: any) => {
try {
res = JSON.parse(res);
this.directoryStore.setTree(res);
return res;
} catch (err) {
console.error('Could not parse response of tree route. Will return empty object.');
return {};
}
})
);
}

Expand All @@ -147,7 +160,7 @@ export class DirectoryService implements OnDestroy {
this.subscription.add(
this.projectStore.project$
.pipe(
filter(res => res !== undefined),
filter(res => res !== undefined && res !== ''),
tap(res => (this.project = res)),
flatMap(_ => {
return this.getTree();
Expand Down
22 changes: 20 additions & 2 deletions src/app/services/document.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

import { MatSnackBar } from '@angular/material/snack-bar';
import { ProjectStore } from './../stores/project.store';
import { LinkService } from 'src/app/services/link.service';
import { FileInfo } from '../models/fileInfo.interface';
Expand All @@ -30,7 +31,8 @@ export class DocumentService implements OnDestroy {
private paragraphService: ParagraphService,
private documentStore: DocumentStore,
private linkService: LinkService,
private projectStore: ProjectStore
private projectStore: ProjectStore,
private snackBar: MatSnackBar
) {}

ngOnDestroy() {
Expand Down Expand Up @@ -121,7 +123,7 @@ export class DocumentService implements OnDestroy {

deleteDocument(path: string, name: string) {
const params: any = {
doc_path: path
doc_path: path,
};

return this.httpClient.delete(this.api.getDocumentRoute(name), { params }).pipe(
Expand All @@ -143,14 +145,30 @@ export class DocumentService implements OnDestroy {

init() {
const lastSaved = this.getLastSavedFileInfo();
let fInfo;
if (lastSaved) this.documentStore.setFileInfo(lastSaved);

this.subscription.add(
this.documentStore.fileInfo$
.pipe(
flatMap((fileInfo: FileInfo) => {
if (!fileInfo) return of(null);
fInfo = fileInfo;
return this.getDocument(fileInfo.path, fileInfo.name, false);
}),
catchError(err => {
this.snackBar.open(translate('error.couldNotLoadDocument', { name: fInfo.name }), '', {
duration: 10000,
});
if (fInfo.name === lastSaved.name && fInfo.path === lastSaved.path) {
console.warn(
'Was not able to open last document. Will unset last document to avoid future problems.',
fInfo.name
);
localStorage.removeItem(LAST_DOCUMENT_KEY);
}

return this.api.handleHttpError(err);
})
)
.subscribe((document: DocumentDefinition) => {
Expand Down
4 changes: 4 additions & 0 deletions src/assets/i18n/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -249,5 +249,9 @@
},
"wordcount": {
"words": "Wörter"
},
"error": {
"couldNotFetchTree": "Konnte Ordner für {{name}} nicht laden. Bitte versuche, die Anwendung neuzustarten oder zur Projektübersicht zurückzukehren.",
"couldNotLoadDocument": "Das Dokument '{{name}}' konnte nicht geladen werden :("
}
}
4 changes: 4 additions & 0 deletions src/assets/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -250,5 +250,9 @@
"wordcount": {
"word": "Word",
"words": "Words"
},
"error": {
"couldNotFetchTree": "Was unable to fetch tree, cannot display directories. Please try restart the application or return to project overview.",
"couldNotLoadDocument": "Was unable to load document :("
}
}

0 comments on commit d5098f8

Please sign in to comment.