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

[#46150] introduce nextcloud upload service

Eric Schubertb2439bd974bb
diff --git a/frontend/src/app/core/upload/upload.service.ts b/frontend/src/app/core/upload/upload.service.ts
new file mode 100644
index 00000000000..ef4b57f05c5
--- /dev/null
+++ b/frontend/src/app/core/upload/upload.service.ts
@@ -0,0 +1,38 @@
+// -- copyright
+// OpenProject is an open source project management software.
+// Copyright (C) 2012-2023 the OpenProject GmbH
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License version 3.
+//
+// OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+// Copyright (C) 2006-2013 Jean-Philippe Lang
+// Copyright (C) 2010-2013 the ChiliProject Team
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+//
+// See COPYRIGHT and LICENSE files for more details.
+//++
+
+import { Observable } from 'rxjs';
+import { HttpEvent } from '@angular/common/http';
+
+export interface IUploadFile {
+ file:File;
+}
+
+export abstract class OpUploadService {
+ public abstract upload<T>(href:string, uploadFiles:IUploadFile[]):Observable<HttpEvent<T>>[];
+}
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 3eda4560c90..84a05359139 100644
--- a/frontend/src/app/shared/components/storages/storage/storage.component.ts
+++ b/frontend/src/app/shared/components/storages/storage/storage.component.ts
@@ -52,10 +52,8 @@ import {
catchError,
filter,
map,
- share,
switchMap,
take,
- tap,
} from 'rxjs/operators';
import { CookieService } from 'ngx-cookie-service';
import { v4 as uuidv4 } from 'uuid';
@@ -89,6 +87,7 @@ import {
} from 'core-app/shared/components/storages/location-picker-modal/location-picker-modal.component';
import { ToastService } from 'core-app/shared/components/toaster/toast.service';
import { StorageFilesResourceService } from 'core-app/core/state/storage-files/storage-files.service';
+import { OpUploadService } from 'core-app/core/upload/upload.service';
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';
@@ -96,7 +95,10 @@ import { PathHelperService } from 'core-app/core/path-helper/path-helper.service
import {
UploadConflictModalComponent,
} from 'core-app/shared/components/storages/upload-conflict-modal/upload-conflict-modal.component';
-import { EXTERNAL_REQUEST_HEADER } from 'core-app/features/hal/http/openproject-header-interceptor';
+import {
+ NextcloudUploadFile,
+ NextcloudUploadService,
+} from 'core-app/shared/components/storages/upload/nextcloud-upload.service';
import { FileUploadResponse, LocationData, UploadData } from 'core-app/shared/components/storages/storage/interfaces';
import isNotNull from 'core-app/core/state/is-not-null';
import compareId from 'core-app/core/state/compare-id';
@@ -105,6 +107,7 @@ import compareId from 'core-app/core/state/compare-id';
selector: 'op-storage',
templateUrl: './storage.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
+ providers: [{ provide: OpUploadService, useClass: NextcloudUploadService }],
})
export class StorageComponent extends UntilDestroyedMixin implements OnInit, OnDestroy {
@Input() public resource:HalResource;
@@ -214,6 +217,7 @@ export class StorageComponent extends UntilDestroyedMixin implements OnInit, OnD
private readonly cdRef:ChangeDetectorRef,
private readonly toastService:ToastService,
private readonly cookieService:CookieService,
+ private readonly uploadService:OpUploadService,
private readonly opModalService:OpModalService,
private readonly timezoneService:TimezoneService,
private readonly pathHelperService:PathHelperService,
@@ -387,36 +391,14 @@ export class StorageComponent extends UntilDestroyedMixin implements OnInit, OnD
}
private uploadAndNotify(link:IUploadLink, file:File, overwrite:boolean|null):Observable<FileUploadResponse> {
- const { method, href } = link._links.destination;
-
- const formData = new FormData();
- formData.append('file', file, file.name);
- if (overwrite !== null) {
- formData.append('overwrite', String(overwrite));
- }
- const observable = this.http.request<FileUploadResponse>(
- method,
- href,
- {
- body: formData,
- headers: { [EXTERNAL_REQUEST_HEADER]: 'true' },
- observe: 'events',
- reportProgress: true,
- responseType: 'json',
- },
- ).pipe(share());
- const notification = this.toastService.add({
- data: [[file, observable]],
- type: 'upload',
- message: this.text.toast.uploadingLabel,
- });
+ const { href } = link._links.destination;
+ const uploadFiles:NextcloudUploadFile[] = [{ file, overwrite }];
+ const observable = this.uploadService.upload(href, uploadFiles)[0];
+ this.toastService.addUpload(this.text.toast.uploadingLabel, [[file, observable]]);
return observable
.pipe(
filter((ev) => ev.type === HttpEventType.Response),
- tap(() => {
- setTimeout(() => this.toastService.remove(notification), 700);
- }),
map((ev:HttpResponse<FileUploadResponse>) => ev.body),
map((data) => {
if (data === null) {
diff --git a/frontend/src/app/shared/components/storages/upload/nextcloud-upload.service.ts b/frontend/src/app/shared/components/storages/upload/nextcloud-upload.service.ts
new file mode 100644
index 00000000000..0fe2b476a8f
--- /dev/null
+++ b/frontend/src/app/shared/components/storages/upload/nextcloud-upload.service.ts
@@ -0,0 +1,81 @@
+// -- copyright
+// OpenProject is an open source project management software.
+// Copyright (C) 2012-2023 the OpenProject GmbH
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License version 3.
+//
+// OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+// Copyright (C) 2006-2013 Jean-Philippe Lang
+// Copyright (C) 2010-2013 the ChiliProject Team
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+//
+// See COPYRIGHT and LICENSE files for more details.
+//++
+
+import { Observable } from 'rxjs';
+import { share } from 'rxjs/operators';
+import { Injectable } from '@angular/core';
+import { HttpClient, HttpEvent } from '@angular/common/http';
+
+import { IUploadFile, OpUploadService } from 'core-app/core/upload/upload.service';
+import { EXTERNAL_REQUEST_HEADER } from 'core-app/features/hal/http/openproject-header-interceptor';
+
+export interface NextcloudFileUploadResponse {
+ file_name:string;
+ file_id:number;
+}
+
+export interface NextcloudUploadFile extends IUploadFile {
+ overwrite:boolean|null;
+}
+
+@Injectable()
+export class NextcloudUploadService extends OpUploadService {
+ constructor(
+ private readonly http:HttpClient,
+ ) {
+ super();
+ }
+
+ public upload<T>(
+ href:string,
+ uploadFiles:NextcloudUploadFile[],
+ ):Observable<HttpEvent<T>>[] {
+ return uploadFiles.map((file) => this.uploadSingle(href, file));
+ }
+
+ private uploadSingle<T>(href:string, uploadFile:NextcloudUploadFile):Observable<HttpEvent<T>> {
+ const body = new FormData();
+ body.append('file', uploadFile.file, uploadFile.file.name);
+
+ if (uploadFile.overwrite !== null) {
+ body.append('overwrite', String(uploadFile.overwrite));
+ }
+
+ return this.http.request<T>(
+ 'post',
+ href,
+ {
+ body,
+ headers: { [EXTERNAL_REQUEST_HEADER]: 'true' },
+ observe: 'events',
+ reportProgress: true,
+ responseType: 'json',
+ },
+ ).pipe(share());
+ }
+}
diff --git a/frontend/src/app/shared/components/toaster/toast.service.ts b/frontend/src/app/shared/components/toaster/toast.service.ts
index 377501d528a..ac36bc40db5 100644
--- a/frontend/src/app/shared/components/toaster/toast.service.ts
+++ b/frontend/src/app/shared/components/toaster/toast.service.ts
@@ -26,21 +26,20 @@
// See COPYRIGHT and LICENSE files for more details.
//++
-import { ConfigurationService } from 'core-app/core/config/configuration.service';
-import {
- input,
- State,
-} from 'reactivestates';
+import { forkJoin, Observable } from 'rxjs';
+import { filter, take } from 'rxjs/operators';
+import { input, State } from 'reactivestates';
import { Injectable } from '@angular/core';
+import { HttpErrorResponse, HttpEvent, HttpEventType } from '@angular/common/http';
+
+import { I18nService } from 'core-app/core/i18n/i18n.service';
+import { ConfigurationService } from 'core-app/core/config/configuration.service';
import { UploadInProgress } from 'core-app/core/file-upload/op-file-upload.service';
import {
IHalErrorBase,
IHalMultipleError,
isHalError,
} from 'core-app/features/hal/resources/error-resource';
-import { HttpErrorResponse } from '@angular/common/http';
-import { Observable } from 'rxjs';
-import { I18nService } from 'core-app/core/i18n/i18n.service';
export function removeSuccessFlashMessages():void {
jQuery('.flash.notice').remove();
@@ -65,11 +64,10 @@ export class ToastService {
readonly configurationService:ConfigurationService,
readonly I18n:I18nService,
) {
- jQuery(window)
- .on(OPToastEvent,
- (event:JQuery.TriggeredEvent, toast:IToast) => {
- this.add(toast);
- });
+ jQuery(window).on(
+ OPToastEvent,
+ (event:JQuery.TriggeredEvent, toast:IToast) => { this.add(toast); },
+ );
}
/**
@@ -146,6 +144,23 @@ export class ToastService {
return this.add(this.createAttachmentUploadToast(message, uploads));
}
+ public addUpload(message:string, uploads:[[File, Observable<HttpEvent<unknown>>]]):IToast {
+ const notification = this.add({
+ data: uploads,
+ type: 'upload',
+ message,
+ });
+
+ const observables = uploads.map((o) => o[1].pipe(filter((ev) => ev.type === HttpEventType.Response)));
+ forkJoin(observables)
+ .pipe(take(1))
+ .subscribe(() => {
+ setTimeout(() => this.remove(notification), 700);
+ });
+
+ return notification;
+ }
+
public addLoading(observable:Observable<unknown>):IToast {
return this.add(this.createLoadingToast(this.I18n.t('js.ajax.updating'), observable));
}