Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

[#44856] added linking after upload and error handling

Eric Schubert25613577d83f
diff --git a/frontend/src/app/shared/components/storages/location-picker-modal/location-picker-modal.component.ts b/frontend/src/app/shared/components/storages/location-picker-modal/location-picker-modal.component.ts
index c323eaea747..28d07a74742 100644
--- a/frontend/src/app/shared/components/storages/location-picker-modal/location-picker-modal.component.ts
+++ b/frontend/src/app/shared/components/storages/location-picker-modal/location-picker-modal.component.ts
@@ -55,6 +55,8 @@ import {
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class LocationPickerModalComponent extends FilePickerBaseModalComponent {
+ public submitted = false;
+
public readonly text = {
header: this.i18n.t('js.storages.select_location'),
buttons: {
@@ -91,6 +93,7 @@ export class LocationPickerModalComponent extends FilePickerBaseModalComponent {
}
public chooseLocation():void {
+ this.submitted = true;
this.service.close();
}
diff --git a/frontend/src/app/shared/components/storages/services/upload-storage-files.service.ts b/frontend/src/app/shared/components/storages/services/upload-storage-files.service.ts
index 9bfcb0a055a..594e1b59542 100644
--- a/frontend/src/app/shared/components/storages/services/upload-storage-files.service.ts
+++ b/frontend/src/app/shared/components/storages/services/upload-storage-files.service.ts
@@ -30,15 +30,19 @@ import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
+
import { IUploadLink } from 'core-app/core/state/storage-files/upload-link.model';
+import { IStorageFile } from 'core-app/core/state/storage-files/storage-file.model';
+import { TimezoneService } from 'core-app/core/datetime/timezone.service';
@Injectable()
export class UploadStorageFilesService {
constructor(
private readonly httpClient:HttpClient,
+ private readonly timezoneService:TimezoneService,
) {}
- public uploadFile(uploadLink:IUploadLink, file:File):Observable<string> {
+ public uploadFile(uploadLink:IUploadLink, file:File):Observable<IStorageFile> {
const url = new URL(uploadLink._links.destination.href);
const token = url.username;
const password = url.password;
@@ -50,23 +54,80 @@ export class UploadStorageFilesService {
'X-External-Request': 'true',
};
- const body = '<?xml version="1.0"?>\n'
- + '<d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns">\n'
- + ' <d:prop>\n'
- + ' <oc:fileid />\n'
- + ' </d:prop>\n'
- + '</d:propfind>';
-
const method = uploadLink._links.destination.method;
return this.httpClient
.request(method, url.toString(), { body: file, headers })
.pipe(
- switchMap(() => this.httpClient.request('propfind', url.toString(), { body, headers, responseType: 'text' })),
- map((xml) => {
- const fileId = /<oc:fileid>(.*)<\/oc:fileid>/.exec(xml)?.pop();
- if (!fileId) { throw new Error('no file id found'); }
- return fileId;
- }),
+ switchMap(() => this.httpClient.request(
+ 'propfind',
+ url.toString(),
+ {
+ body: this.propfindBody,
+ headers,
+ responseType: 'text',
+ },
+ )),
+ map((xml) => this.parseXmlResponse(xml)),
);
}
+
+ private parseXmlResponse(xml:string):IStorageFile {
+ const error = new Error(`Invalid response for uploaded file: ${xml}`);
+
+ const id = /<oc:fileid>(.*)<\/oc:fileid>/.exec(xml)?.pop();
+ if (!id) { throw error; }
+
+ const mimeType = /<d:getcontenttype>(.*)<\/d:getcontenttype>/.exec(xml)?.pop();
+ if (!mimeType) { throw error; }
+
+ const size = /<oc:size>(.*)<\/oc:size>/.exec(xml)?.pop();
+ if (!size) { throw error; }
+
+ const location = /<d:href>(.*)<\/d:href>/.exec(xml)?.pop();
+ if (!location) { throw error; }
+
+ const date = /<d:getlastmodified>(.*)<\/d:getlastmodified>/.exec(xml)?.pop();
+ if (!date) { throw error; }
+ const createdAt = this.timezoneService.parseDatetime(date).toISOString();
+ const lastModifiedAt = createdAt;
+
+ const creator = /<oc:owner-display-name>(.*)<\/oc:owner-display-name>/.exec(xml)?.pop();
+ if (!creator) { throw error; }
+
+ return {
+ id,
+ name: location.split('/').pop() || '',
+ mimeType,
+ size: parseInt(size, 10),
+ location,
+ createdAt,
+ createdByName: creator,
+ lastModifiedAt,
+ lastModifiedByName: creator,
+ };
+ }
+
+ private get propfindBody() {
+ return '<?xml version="1.0"?>\n'
+ + '<d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns" xmlns:nc="http://nextcloud.org/ns">\n'
+ + ' <d:prop>\n'
+ + ' <oc:fileid />\n'
+ + ' <d:getlastmodified />\n'
+ + ' <d:getetag />\n'
+ + ' <d:getcontenttype />\n'
+ + ' <d:resourcetype />\n'
+ + ' <oc:fileid />\n'
+ + ' <oc:permissions />\n'
+ + ' <oc:size />\n'
+ + ' <d:getcontentlength />\n'
+ + ' <nc:has-preview />\n'
+ + ' <oc:favorite />\n'
+ + ' <oc:comments-unread />\n'
+ + ' <oc:owner-display-name />\n'
+ + ' <oc:share-types />\n'
+ + ' <nc:contained-folder-count />\n'
+ + ' <nc:contained-file-count />\n'
+ + ' </d:prop>\n'
+ + '</d:propfind>';
+ }
}
diff --git a/frontend/src/app/shared/components/storages/storage/storage.component.ts b/frontend/src/app/shared/components/storages/storage/storage.component.ts
index 928e0f9482c..a6f09c08ab9 100644
--- a/frontend/src/app/shared/components/storages/storage/storage.component.ts
+++ b/frontend/src/app/shared/components/storages/storage/storage.component.ts
@@ -37,8 +37,9 @@ import {
import {
BehaviorSubject,
Observable,
+ throwError,
} from 'rxjs';
-import { switchMap, take } from 'rxjs/operators';
+import { catchError, switchMap, take } from 'rxjs/operators';
import { CookieService } from 'ngx-cookie-service';
import { v4 as uuidv4 } from 'uuid';
@@ -120,6 +121,12 @@ export class StorageComponent extends UntilDestroyedMixin implements OnInit {
linkExisting: this.i18n.t('js.storages.link_existing_files'),
uploadFile: this.i18n.t('js.storages.upload_files'),
},
+ toast: {
+ successFileLinksCreated: (count:number):string => this.i18n.t('js.storages.file_links.success_create', { count }),
+ uploadFailed: (fileName:string):string => this.i18n.t('js.storages.file_links.upload_error', { fileName }),
+ linkingAfterUploadFailed: (fileName:string, workPackageId:string):string =>
+ this.i18n.t('js.storages.file_links.link_uploaded_file_error', { fileName, workPackageId }),
+ },
openStorage: ():string => this.i18n.t('js.storages.open_storage', { storageType: this.storageType }),
};
@@ -135,6 +142,10 @@ export class StorageComponent extends UntilDestroyedMixin implements OnInit {
return this.storage._links.open.href;
}
+ private get addFileLinksHref() {
+ return (this.resource.$links as unknown&{ addFileLink:IHalResourceLink }).addFileLink.href;
+ }
+
constructor(
private readonly i18n:I18nService,
private readonly toastService:ToastService,
@@ -192,7 +203,7 @@ export class StorageComponent extends UntilDestroyedMixin implements OnInit {
storageName: this.storage.name,
storageLocation: this.storageFilesLocation,
storageLink: this.storage._links.self,
- addFileLinksHref: (this.resource.$links as unknown&{ addFileLink:IHalResourceLink }).addFileLink.href,
+ addFileLinksHref: this.addFileLinksHref,
collectionKey: this.collectionKey,
fileLinks,
};
@@ -220,14 +231,18 @@ export class StorageComponent extends UntilDestroyedMixin implements OnInit {
storageLink: this.storage._links.self,
};
this.opModalService.show<LocationPickerModalComponent>(LocationPickerModalComponent, 'global', locals)
- .subscribe((modal) => {
- modal.closingEvent.subscribe((data) => {
- this.uploadFile(files![0], data.location);
+ .subscribe((m) => {
+ m.closingEvent.subscribe((modal) => {
+ if (modal.submitted && files !== null) {
+ this.uploadFile(files[0], modal.location);
+ }
});
});
}
private uploadFile(file:UploadFile, location:string):void {
+ let isUploadError = false;
+
this.storageFilesResourceService
.uploadLink(
this.UploadResourceLink(this.storage._links.self),
@@ -236,18 +251,31 @@ export class StorageComponent extends UntilDestroyedMixin implements OnInit {
)
.pipe(
switchMap((link) => this.uploadStorageFilesService.uploadFile(link, file)),
+ catchError((error) => {
+ isUploadError = true;
+ return throwError(error);
+ }),
+ switchMap((f) => this.fileLinkResourceService.addFileLinks(
+ this.collectionKey,
+ this.addFileLinksHref,
+ this.storage._links.self,
+ [f],
+ )),
)
.subscribe(
- (data) => {
- this.toastService.addSuccess(`Uploaded file with id ${data}`);
+ (collection) => {
+ this.toastService.addSuccess(this.text.toast.successFileLinksCreated(collection.count));
},
(error) => {
+ if (isUploadError) {
+ this.toastService.addError(this.text.toast.uploadFailed(file.name));
+ } else {
+ this.toastService.addError(this.text.toast.linkingAfterUploadFailed(file.name, this.resource.id as string));
+ }
+
console.error(error);
},
);
-
- // TODO: create file links
- // this.fileLinkResourceService.addFileLinks()
}
private UploadResourceLink(storageLink:IHalResourceLink):IHalResourceLink {
diff --git a/modules/storages/config/locales/js-en.yml b/modules/storages/config/locales/js-en.yml
index 1a445ec595e..293d5c8cfbc 100644
--- a/modules/storages/config/locales/js-en.yml
+++ b/modules/storages/config/locales/js-en.yml
@@ -45,6 +45,9 @@ en:
success_create:
one: "Successfully created 1 file link."
other: "Successfully created %{count} file links."
+ upload_error: "An error occurred uploading the file '%{fileName}'."
+ link_uploaded_file_error: >
+ An error occurred linking the recently uploaded file '%{fileName}' to the work package %{workPackageId}.
not_allowed_tooltip: "Please log in to Nextcloud to access this file"
already_linked_file: "This file is already linked to this work package."
already_linked_directory: "This directory is already linked to this work package."