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

Construct Valid origin_ids for Sharepoint File Links (#20364)

Marcello Rocha5625dfb5a339
diff --git a/frontend/src/app/core/upload/upload.service.ts b/frontend/src/app/core/upload/upload.service.ts
index 6d12c604460..bdd5639df07 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;
+ location?:string;
overwrite?:boolean;
}
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 fe3d4c4c241..40d47240737 100644
--- a/frontend/src/app/shared/components/storages/storage/storage.component.ts
+++ b/frontend/src/app/shared/components/storages/storage/storage.component.ts
@@ -393,7 +393,7 @@ export class StorageComponent extends UntilDestroyedMixin implements OnInit, OnD
const link = this.uploadResourceLink(storage, data.file.name, data.location);
return this.storageFilesResourceService.uploadLink(link);
}),
- switchMap((link) => this.uploadAndNotify(link, data.file, data.overwrite)),
+ switchMap((link) => this.uploadAndNotify(link, data.file, data.location, data.overwrite)),
catchError((error) => {
isUploadError = true;
return throwError(error);
@@ -471,9 +471,9 @@ export class StorageComponent extends UntilDestroyedMixin implements OnInit, OnD
}
}
- private uploadAndNotify(link:IUploadLink, file:File, overwrite:boolean|null):Observable<IStorageFileUploadResponse> {
+ private uploadAndNotify(link:IUploadLink, file:File, location:string|null, overwrite:boolean|null):Observable<IStorageFileUploadResponse> {
const { href } = link._links.destination;
- const uploadFiles:IUploadFile[] = [{ file, overwrite: overwrite ?? undefined }];
+ const uploadFiles:IUploadFile[] = [{ file, location: location ?? undefined, overwrite: overwrite ?? undefined }];
const observable = this.uploadService.upload<IStorageFileUploadResponse>(href, uploadFiles)[0];
this.toastService.addUpload(this.text.toast.uploadingLabel, [[file, observable]]);
diff --git a/frontend/src/app/shared/components/storages/upload/sharepoint-upload.strategy.ts b/frontend/src/app/shared/components/storages/upload/sharepoint-upload.strategy.ts
new file mode 100644
index 00000000000..f03f598cd62
--- /dev/null
+++ b/frontend/src/app/shared/components/storages/upload/sharepoint-upload.strategy.ts
@@ -0,0 +1,83 @@
+//-- copyright
+// OpenProject is an open source project management software.
+// Copyright (C) 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 SharepointFileUploadResponse {
+ id:string;
+ name:string;
+ file:{ mimeType:string };
+ size:number;
+}
+
+export class SharepointUploadStrategy implements IUploadStrategy {
+ // eslint-disable-next-line no-unused-vars
+ 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}`;
+ const driveId = (uploadFile.location ?? '').split(':')[0];
+ return this.http.request<SharepointFileUploadResponse>(
+ '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: `${ driveId }:${ responseBody.id }`,
+ name: responseBody.name,
+ size: responseBody.size,
+ mimeType: responseBody.file.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.ts
index 3a77c1dae09..13920ff44e9 100644
--- a/frontend/src/app/shared/components/storages/upload/storage-upload.service.ts
+++ b/frontend/src/app/shared/components/storages/upload/storage-upload.service.ts
@@ -36,6 +36,7 @@ import { IUploadStrategy } from 'core-app/shared/components/storages/upload/uplo
import { NextcloudUploadStrategy } from 'core-app/shared/components/storages/upload/nextcloud-upload.strategy';
import { nextcloud, oneDrive, sharepoint } from 'core-app/shared/components/storages/storages-constants.const';
import { OneDriveUploadStrategy } from 'core-app/shared/components/storages/upload/one-drive-upload.strategy';
+import { SharepointUploadStrategy } from 'core-app/shared/components/storages/upload/sharepoint-upload.strategy';
export interface IStorageFileUploadResponse {
id:ID;
@@ -74,7 +75,7 @@ export class StorageUploadService extends OpUploadService {
this.uploadStrategy = new OneDriveUploadStrategy(this.http);
break;
case sharepoint:
- this.uploadStrategy = new OneDriveUploadStrategy(this.http);
+ this.uploadStrategy = new SharepointUploadStrategy(this.http);
break;
default:
throw new Error('unknown storage type');