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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
[chore] added one drive upload strategy for frontend
Eric Schubert8022ac72fd32
diff --git a/frontend/src/app/core/upload/convert-http-event.ts b/frontend/src/app/core/upload/convert-http-event.tsnew file mode 100644index 00000000000..c42e82e785c--- /dev/null+++ b/frontend/src/app/core/upload/convert-http-event.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 { HttpEvent } from '@angular/common/http';+import isHttpResponse from 'core-app/core/upload/is-http-response';++export default function convertHttpEvent<T, K>(event:HttpEvent<T>, convert:(body:T) => K):HttpEvent<K> {+ if (isHttpResponse(event) && event.body !== null) {+ return event.clone({ body: convert(event.body) });+ }++ return event as HttpEvent<K>;+}diff --git a/frontend/src/app/core/upload/upload.service.ts b/frontend/src/app/core/upload/upload.service.tsindex ef4b57f05c5..3b76c905d00 100644--- a/frontend/src/app/core/upload/upload.service.ts+++ b/frontend/src/app/core/upload/upload.service.ts@@ -31,6 +31,7 @@ import { HttpEvent } from '@angular/common/http';export interface IUploadFile {file:File;+ overwrite?:boolean;}export abstract class OpUploadService {diff --git a/frontend/src/app/shared/components/storages/storage/storage.component.ts b/frontend/src/app/shared/components/storages/storage/storage.component.tsindex 3b35a300b38..8e6d307f754 100644--- a/frontend/src/app/shared/components/storages/storage/storage.component.ts+++ b/frontend/src/app/shared/components/storages/storage/storage.component.ts@@ -76,7 +76,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 { IUploadFile, 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';@@ -84,11 +84,6 @@ import { PathHelperService } from 'core-app/core/path-helper/path-helper.serviceimport {UploadConflictModalComponent,} from 'core-app/shared/components/storages/upload-conflict-modal/upload-conflict-modal.component';-import {- NextcloudFileUploadResponse,- NextcloudUploadFile,- NextcloudUploadService,-} from 'core-app/shared/components/storages/upload/nextcloud-upload.service';import { 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';@@ -100,12 +95,16 @@ import {} from 'core-app/shared/components/storages/storage-information/storage-information.service';import { storageLocaleString } from 'core-app/shared/components/storages/functions/storages.functions';import { storageIconMappings } from 'core-app/shared/components/storages/icons.mapping';+import {+ IStorageFileUploadResponse,+ StorageUploadService,+} from 'core-app/shared/components/storages/upload/storage-upload.service';@Component({selector: 'op-storage',templateUrl: './storage.component.html',changeDetection: ChangeDetectionStrategy.OnPush,- providers: [{ provide: OpUploadService, useClass: NextcloudUploadService }],+ providers: [{ provide: OpUploadService, useClass: StorageUploadService }],})export class StorageComponent extends UntilDestroyedMixin implements OnInit, OnDestroy {@Input() public resource:HalResource;@@ -252,6 +251,10 @@ export class StorageComponent extends UntilDestroyedMixin implements OnInit, OnDmap((storage) => this.i18n.t(storageLocaleString(storage._links.type.href))),);+ this.storage.pipe(take(1)).subscribe((storage) => {+ (this.uploadService as StorageUploadService).setUploadStrategy(storage._links.type.href);+ });+this.storageErrors = combineLatest([this.storage,this.fileLinks,@@ -278,10 +281,10 @@ export class StorageComponent extends UntilDestroyedMixin implements OnInit, OnD.pipe(switchMap((key) => this.fileLinkResourceService.remove(key, fileLink)),)- .subscribe(- () => { /* Do nothing */ },- (error:HttpErrorResponse) => this.toastService.addError(error),- );+ .subscribe({+ next: () => { /* Do nothing */ },+ error: (error:HttpErrorResponse) => this.toastService.addError(error),+ });}public openLinkFilesDialog():void {@@ -372,7 +375,7 @@ export class StorageComponent extends UntilDestroyedMixin implements OnInit, OnDisUploadError = true;return throwError(error);}),- switchMap((uploadResponse) => this.createFileLinkData(data.file, uploadResponse)),+ switchMap((uploadResponse) => this.createFileLinkData(uploadResponse)),tap((fileLinkCreationData) => {// Update the file link list of this storage only in case of a linked file got updatedif (fileLinkCreationData === null) {@@ -396,11 +399,11 @@ export class StorageComponent extends UntilDestroyedMixin implements OnInit, OnD)),)),)- .subscribe(- (collection) => {+ .subscribe({+ next: (collection) => {this.toastService.addSuccess(this.text.toast.successFileLinksCreated(collection.count));},- (error) => {+ error: (error) => {if (isUploadError) {this.handleUploadError(error as HttpErrorResponse, data.file.name);} else {@@ -409,7 +412,7 @@ export class StorageComponent extends UntilDestroyedMixin implements OnInit, OnDconsole.error(error);},- );+ });}private handleUploadError(error:HttpErrorResponse, fileName:string):void {@@ -434,10 +437,10 @@ export class StorageComponent extends UntilDestroyedMixin implements OnInit, OnD}}- private uploadAndNotify(link:IUploadLink, file:File, overwrite:boolean|null):Observable<NextcloudFileUploadResponse> {+ private uploadAndNotify(link:IUploadLink, file:File, overwrite:boolean|null):Observable<IStorageFileUploadResponse> {const { href } = link._links.destination;- const uploadFiles:NextcloudUploadFile[] = [{ file, overwrite }];- const observable = this.uploadService.upload<NextcloudFileUploadResponse>(href, uploadFiles)[0];+ const uploadFiles:IUploadFile[] = [{ file, overwrite: overwrite !== null ? overwrite : undefined }];+ const observable = this.uploadService.upload<IStorageFileUploadResponse>(href, uploadFiles)[0];this.toastService.addUpload(this.text.toast.uploadingLabel, [[file, observable]]);return observable@@ -454,22 +457,22 @@ export class StorageComponent extends UntilDestroyedMixin implements OnInit, OnD);}- private createFileLinkData(file:File, response:NextcloudFileUploadResponse):Observable<IFileLinkOriginData|null> {+ private createFileLinkData(response:IStorageFileUploadResponse):Observable<IFileLinkOriginData|null> {return this.fileLinks.pipe(take(1),map((fileLinks) => {- const existingFileLink = fileLinks.find((l) => compareId(l.originData.id, response.file_id));+ const existingFileLink = fileLinks.find((l) => compareId(l.originData.id, response.id));if (existingFileLink) {return null;}const now = this.timezoneService.parseDate(new Date()).toISOString();return ({- id: response.file_id,- name: response.file_name,- mimeType: file.type,- size: file.size,+ id: response.id,+ name: response.name,+ mimeType: response.mimeType,+ size: response.size,createdAt: now,lastModifiedAt: now,});diff --git a/frontend/src/app/shared/components/storages/storages-constants.const.ts b/frontend/src/app/shared/components/storages/storages-constants.const.tsindex 4017ea0f4c0..330565c5b6c 100644--- a/frontend/src/app/shared/components/storages/storages-constants.const.ts+++ b/frontend/src/app/shared/components/storages/storages-constants.const.ts@@ -1,5 +1,6 @@// Storage typesexport const nextcloud = 'urn:openproject-org:api:v3:storages:Nextcloud';+export const oneDrive = 'urn:openproject-org:api:v3:storages:OneDrive';// Storage authorization stateexport const storageConnected = 'urn:openproject-org:api:v3:storages:authorization:Connected';diff --git a/frontend/src/app/shared/components/storages/upload/nextcloud-upload.service.ts b/frontend/src/app/shared/components/storages/upload/nextcloud-upload.strategy.tssimilarity index 68%rename from frontend/src/app/shared/components/storages/upload/nextcloud-upload.service.tsrename to frontend/src/app/shared/components/storages/upload/nextcloud-upload.strategy.tsindex 0fe2b476a8f..dfb95ebed6b 100644--- a/frontend/src/app/shared/components/storages/upload/nextcloud-upload.service.ts+++ b/frontend/src/app/shared/components/storages/upload/nextcloud-upload.strategy.ts@@ -27,46 +27,38 @@//++import { Observable } from 'rxjs';-import { share } from 'rxjs/operators';-import { Injectable } from '@angular/core';+import { map, share } from 'rxjs/operators';import { HttpClient, HttpEvent } from '@angular/common/http';-import { IUploadFile, OpUploadService } from 'core-app/core/upload/upload.service';+import { IUploadFile } from 'core-app/core/upload/upload.service';import { EXTERNAL_REQUEST_HEADER } from 'core-app/features/hal/http/openproject-header-interceptor';+import { IUploadStrategy } from 'core-app/shared/components/storages/upload/upload-strategy';+import convertHttpEvent from 'core-app/core/upload/convert-http-event';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();- }+export class NextcloudUploadStrategy implements IUploadStrategy {+ constructor(private readonly http:HttpClient) { }- public upload<T>(+ public execute<T>(href:string,- uploadFiles:NextcloudUploadFile[],+ uploadFiles:IUploadFile[],):Observable<HttpEvent<T>>[] {return uploadFiles.map((file) => this.uploadSingle(href, file));}- private uploadSingle<T>(href:string, uploadFile:NextcloudUploadFile):Observable<HttpEvent<T>> {+ private uploadSingle<T>(href:string, uploadFile:IUploadFile):Observable<HttpEvent<T>> {const body = new FormData();body.append('file', uploadFile.file, uploadFile.file.name);- if (uploadFile.overwrite !== null) {+ if (uploadFile.overwrite !== undefined) {body.append('overwrite', String(uploadFile.overwrite));}- return this.http.request<T>(+ return this.http.request<NextcloudFileUploadResponse>('post',href,{@@ -76,6 +68,15 @@ export class NextcloudUploadService extends OpUploadService {reportProgress: true,responseType: 'json',},- ).pipe(share());+ ).pipe(+ share(),+ map((event) =>+ convertHttpEvent(event, (responseBody) => ({+ id: responseBody.file_id,+ name: responseBody.file_name,+ size: uploadFile.file.size,+ mimeType: uploadFile.file.type,+ } as T))),+ );}}diff --git a/frontend/src/app/shared/components/storages/upload/one-drive-upload.strategy.ts b/frontend/src/app/shared/components/storages/upload/one-drive-upload.strategy.tsnew file mode 100644index 00000000000..c6bbdb0e8e4--- /dev/null+++ b/frontend/src/app/shared/components/storages/upload/one-drive-upload.strategy.ts@@ -0,0 +1,82 @@+// -- 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 { map, share } from 'rxjs/operators';+import { HttpClient, HttpEvent } from '@angular/common/http';++import { IUploadFile } from 'core-app/core/upload/upload.service';+import { EXTERNAL_REQUEST_HEADER } from 'core-app/features/hal/http/openproject-header-interceptor';+import { IUploadStrategy } from 'core-app/shared/components/storages/upload/upload-strategy';+import convertHttpEvent from 'core-app/core/upload/convert-http-event';++export interface OneDriveFileUploadResponse {+ id:string;+ name:string;+ mimeType:string;+ size:number;+}++export class OneDriveUploadStrategy implements IUploadStrategy {+ constructor(private readonly http:HttpClient) { }++ public execute<T>(+ href:string,+ uploadFiles:IUploadFile[],+ ):Observable<HttpEvent<T>>[] {+ return uploadFiles.map((file) => this.uploadSingle(href, file));+ }++ private uploadSingle<T>(href:string, uploadFile:IUploadFile):Observable<HttpEvent<T>> {+ const contentRangeHeader = `bytes 0-${uploadFile.file.size - 1}/${uploadFile.file.size}`;++ return this.http.request<OneDriveFileUploadResponse>(+ 'put',+ href,+ {+ body: uploadFile.file,+ headers: {+ [EXTERNAL_REQUEST_HEADER]: 'true',+ 'Content-Range': contentRangeHeader,+ },+ observe: 'events',+ reportProgress: true,+ responseType: 'json',+ },+ ).pipe(+ share(),+ map((event) =>+ convertHttpEvent(event, (responseBody) => ({+ id: responseBody.id,+ name: responseBody.name,+ size: responseBody.size,+ mimeType: responseBody.mimeType,+ } as T))),+ );+ }+}diff --git a/frontend/src/app/shared/components/storages/upload/storage-upload.service.ts b/frontend/src/app/shared/components/storages/upload/storage-upload.service.tsnew file mode 100644index 00000000000..d3557b9fd20--- /dev/null+++ b/frontend/src/app/shared/components/storages/upload/storage-upload.service.ts@@ -0,0 +1,80 @@+// -- 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 { ID } from '@datorama/akita';+import { Injectable } from '@angular/core';+import { HttpClient, HttpEvent } from '@angular/common/http';++import { IUploadFile, OpUploadService } from 'core-app/core/upload/upload.service';+import { IUploadStrategy } from 'core-app/shared/components/storages/upload/upload-strategy';+import { NextcloudUploadStrategy } from 'core-app/shared/components/storages/upload/nextcloud-upload.strategy';+import { nextcloud, oneDrive } from 'core-app/shared/components/storages/storages-constants.const';+import { OneDriveUploadStrategy } from 'core-app/shared/components/storages/upload/one-drive-upload.strategy';++export interface IStorageFileUploadResponse {+ id:ID;+ name:string;+ mimeType:string;+ size:number;+}++@Injectable()+export class StorageUploadService extends OpUploadService {+ private uploadStrategy:IUploadStrategy;++ constructor(+ private readonly http:HttpClient,+ ) {+ super();+ }++ public upload<T>(+ href:string,+ uploadFiles:IUploadFile[],+ ):Observable<HttpEvent<T>>[] {+ if (!this.uploadStrategy) {+ throw new Error('missing strategy');+ }++ return this.uploadStrategy.execute(href, uploadFiles);+ }++ public setUploadStrategy(storageType:string):void {+ switch (storageType) {+ case nextcloud:+ this.uploadStrategy = new NextcloudUploadStrategy(this.http);+ break;+ case oneDrive:+ this.uploadStrategy = new OneDriveUploadStrategy(this.http);+ break;+ default:+ throw new Error('unknown storage type');+ }+ }+}diff --git a/frontend/src/app/shared/components/storages/upload/upload-strategy.ts b/frontend/src/app/shared/components/storages/upload/upload-strategy.tsnew file mode 100644index 00000000000..21aaa77ef67--- /dev/null+++ b/frontend/src/app/shared/components/storages/upload/upload-strategy.ts@@ -0,0 +1,36 @@+// -- 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';++import { IUploadFile } from 'core-app/core/upload/upload.service';++export interface IUploadStrategy {+ execute<T>(href:string, uploadFiles:IUploadFile[]):Observable<HttpEvent<T>>[];+}diff --git a/modules/storages/lib/api/v3/storages/storage_representer.rb b/modules/storages/lib/api/v3/storages/storage_representer.rbindex c9fa7ed5afe..2812686937a 100644--- a/modules/storages/lib/api/v3/storages/storage_representer.rb+++ b/modules/storages/lib/api/v3/storages/storage_representer.rb@@ -38,13 +38,16 @@ module API::V3::StoragesURN_CONNECTION_ERROR = "#{::API::V3::URN_PREFIX}storages:authorization:Error".freezeURN_STORAGE_TYPE_NEXTCLOUD = "#{::API::V3::URN_PREFIX}storages:Nextcloud".freeze+ URN_STORAGE_TYPE_ONE_DRIVE = "#{::API::V3::URN_PREFIX}storages:OneDrive".freezeSTORAGE_TYPE_MAP = {- URN_STORAGE_TYPE_NEXTCLOUD => Storages::Storage::PROVIDER_TYPE_NEXTCLOUD+ URN_STORAGE_TYPE_NEXTCLOUD => Storages::Storage::PROVIDER_TYPE_NEXTCLOUD,+ URN_STORAGE_TYPE_ONE_DRIVE => Storages::Storage::PROVIDER_TYPE_ONE_DRIVE}.freezeSTORAGE_TYPE_URN_MAP = {- Storages::Storage::PROVIDER_TYPE_NEXTCLOUD => URN_STORAGE_TYPE_NEXTCLOUD+ Storages::Storage::PROVIDER_TYPE_NEXTCLOUD => URN_STORAGE_TYPE_NEXTCLOUD,+ Storages::Storage::PROVIDER_TYPE_ONE_DRIVE => URN_STORAGE_TYPE_ONE_DRIVE}.freezeclass StorageRepresenter < ::API::Decorators::Single