From a87dfae3b0f427ed58442221c724e3daab83820d Mon Sep 17 00:00:00 2001 From: mdelez <60604010+mdelez@users.noreply.github.com> Date: Wed, 2 Mar 2022 09:12:07 +0100 Subject: [PATCH] feat(archive-value): Download archive files with original filename (DEV-295) (#677) * feat(archive-value): download file with original filename * feat(archive): sets the filename --- .../archive/archive.component.ts | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/app/workspace/resource/representation/archive/archive.component.ts b/src/app/workspace/resource/representation/archive/archive.component.ts index 477f0edc96..b06e3b11cf 100644 --- a/src/app/workspace/resource/representation/archive/archive.component.ts +++ b/src/app/workspace/resource/representation/archive/archive.component.ts @@ -1,4 +1,4 @@ -import { HttpClient } from '@angular/common/http'; +import { HttpClient, HttpHeaders } from '@angular/common/http'; import { Component, Input, OnInit } from '@angular/core'; import { ErrorHandlerService } from 'src/app/main/error/error-handler.service'; import { FileRepresentation } from '../file-representation'; @@ -11,13 +11,28 @@ import { FileRepresentation } from '../file-representation'; export class ArchiveComponent implements OnInit { @Input() src: FileRepresentation; + originalFilename: string; + temp: string; constructor( private readonly _http: HttpClient, private _errorHandler: ErrorHandlerService ) { } - ngOnInit(): void { } + ngOnInit(): void { + const requestOptions = { + headers: new HttpHeaders({ 'Content-Type': 'application/json' }), + withCredentials: true + }; + + const pathToJson = this.src.fileValue.fileUrl.substring(0, this.src.fileValue.fileUrl.lastIndexOf('/')) + '/knora.json'; + + this._http.get(pathToJson, requestOptions).subscribe( + res => { + this.originalFilename = res['originalFilename']; + } + ); + } // https://stackoverflow.com/questions/66986983/angular-10-download-file-from-firebase-link-without-opening-into-new-tab async downloadArchive(url: string) { @@ -33,7 +48,14 @@ export class ArchiveComponent implements OnInit { const url = window.URL.createObjectURL(data); const e = document.createElement('a'); e.href = url; - e.download = url.substr(url.lastIndexOf('/') + 1); + + // set filename + if (this.originalFilename === undefined) { + e.download = url.substr(url.lastIndexOf('/') + 1); + } else { + e.download = this.originalFilename; + } + document.body.appendChild(e); e.click(); document.body.removeChild(e);