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] Integrated frontend for upload with backend for share creation

Andreas Pfohl10f4328723e6
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 3febdbe6b12..9bfcb0a055a 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,20 +30,23 @@ 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';
@Injectable()
export class UploadStorageFilesService {
- private token = 'JXSQ7knPCkgCWtp';
-
- private password = 'ad84c32b-b4bf-4b6e-bea2-4b2204d1f66e';
-
constructor(
private readonly httpClient:HttpClient,
) {}
- public uploadFile(file:File):Observable<string> {
+ public uploadFile(uploadLink:IUploadLink, file:File):Observable<string> {
+ const url = new URL(uploadLink._links.destination.href);
+ const token = url.username;
+ const password = url.password;
+ url.username = '';
+ url.password = '';
+
const headers = {
- Authorization: `Basic ${btoa(`${this.token}:${this.password}`)}`,
+ Authorization: `Basic ${btoa(`${token}:${password}`)}`,
'X-External-Request': 'true',
};
@@ -54,11 +57,11 @@ export class UploadStorageFilesService {
+ ' </d:prop>\n'
+ '</d:propfind>';
- const url = `https://nextcloud.local/public.php/webdav/${(file.name)}`;
+ const method = uploadLink._links.destination.method;
return this.httpClient
- .put(url, file, { headers })
+ .request(method, url.toString(), { body: file, headers })
.pipe(
- switchMap(() => this.httpClient.request('propfind', url, { body, headers, responseType: 'text' })),
+ 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'); }
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 bbbd0c1c3ff..928e0f9482c 100644
--- a/frontend/src/app/shared/components/storages/storage/storage.component.ts
+++ b/frontend/src/app/shared/components/storages/storage/storage.component.ts
@@ -38,7 +38,7 @@ import {
BehaviorSubject,
Observable,
} from 'rxjs';
-import { take } from 'rxjs/operators';
+import { switchMap, take } from 'rxjs/operators';
import { CookieService } from 'ngx-cookie-service';
import { v4 as uuidv4 } from 'uuid';
@@ -72,6 +72,8 @@ import {
} from 'core-app/shared/components/storages/location-picker-modal/location-picker-modal.component';
import { UploadStorageFilesService } from 'core-app/shared/components/storages/services/upload-storage-files.service';
import { ToastService } from 'core-app/shared/components/toaster/toast.service';
+import { UploadFile } from 'core-app/core/file-upload/op-file-upload.service';
+import { StorageFilesResourceService } from 'core-app/core/state/storage-files/storage-files.service';
@Component({
selector: 'op-storage',
@@ -142,6 +144,7 @@ export class StorageComponent extends UntilDestroyedMixin implements OnInit {
private readonly configurationService:ConfigurationService,
private readonly fileLinkResourceService:FileLinksResourceService,
private readonly uploadStorageFilesService:UploadStorageFilesService,
+ private readonly storageFilesResourceService:StorageFilesResourceService,
) {
super();
}
@@ -219,23 +222,26 @@ export class StorageComponent extends UntilDestroyedMixin implements OnInit {
this.opModalService.show<LocationPickerModalComponent>(LocationPickerModalComponent, 'global', locals)
.subscribe((modal) => {
modal.closingEvent.subscribe((data) => {
- this.uploadFiles(files, data.location);
+ this.uploadFile(files![0], data.location);
});
});
}
- private uploadFiles(files:FileList|null, location:string):void {
- if (files === null) {
- return;
- }
-
- // TODO: get prepare upload information
-
- this.uploadStorageFilesService.uploadFile(files[0])
+ private uploadFile(file:UploadFile, location:string):void {
+ this.storageFilesResourceService
+ .uploadLink(
+ this.UploadResourceLink(this.storage._links.self),
+ file.name,
+ location,
+ )
+ .pipe(
+ switchMap((link) => this.uploadStorageFilesService.uploadFile(link, file)),
+ )
.subscribe(
(data) => {
this.toastService.addSuccess(`Uploaded file with id ${data}`);
- }, (error) => {
+ },
+ (error) => {
console.error(error);
},
);
@@ -244,6 +250,13 @@ export class StorageComponent extends UntilDestroyedMixin implements OnInit {
// this.fileLinkResourceService.addFileLinks()
}
+ private UploadResourceLink(storageLink:IHalResourceLink):IHalResourceLink {
+ return {
+ href: `${storageLink.href}/files/prepare_upload`,
+ title: 'Upload resource link',
+ };
+ }
+
private instantiateStorageInformation(fileLinks:IFileLink[]):StorageInformationBox[] {
if (!this.isLoggedIn) {
return [];
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/upload_link_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/upload_link_query.rb
index 442db38a256..2a80ab88966 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/upload_link_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/upload_link_query.rb
@@ -85,7 +85,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
method: :put,
relative_path: "#{URI_BASE_PATH}/#{share.id}",
payload: {
- permissions: 4
+ permissions: 5
}
).map { share }
end
diff --git a/modules/storages/lib/api/v3/storage_files/storage_upload_link_representer.rb b/modules/storages/lib/api/v3/storage_files/storage_upload_link_representer.rb
index 82323e8c9e0..f7e51a0c36a 100644
--- a/modules/storages/lib/api/v3/storage_files/storage_upload_link_representer.rb
+++ b/modules/storages/lib/api/v3/storage_files/storage_upload_link_representer.rb
@@ -35,7 +35,7 @@ module API::V3::StorageFiles
link :destination do
{
href: represented.destination,
- method: :post,
+ method: :put,
title: 'Upload File'
}
end