Commit fa5f97d1 authored by Florent POITTEVIN's avatar Florent POITTEVIN
Browse files

fix: display forbidden message when archive download not authorized

parent 057e01c1
......@@ -41,6 +41,7 @@ import {defaultStatusHistoryInitValue} from "@shared/stores/status-history/statu
import {Observable} from "rxjs";
import {
catchError,
map,
tap,
} from "rxjs/operators";
import {
......@@ -50,6 +51,7 @@ import {
CompositionStateModel,
defaultCompositionStateInitValue,
defaultResourceStateInitValue,
HttpStatus,
isNullOrUndefined,
MappingObjectUtil,
MARK_AS_TRANSLATABLE,
......@@ -228,7 +230,9 @@ export class DepositDataFileState extends CompositionState<DepositDataFileStateM
@Action(DepositDataFileAction.Download)
download(ctx: StateContext<DepositDataFileStateModel>, action: DepositDataFileAction.Download): Observable<boolean> {
const url = `${this._urlResource}/${action.parentId}/${ApiResourceNameEnum.DATAFILE}/${action.dataFile.resId}/${ApiActionEnum.DL}`;
return this.downloadService.download(url, action.dataFile.fileName, action.dataFile.fileSize);
return this.downloadService.download(url, action.dataFile.fileName, action.dataFile.fileSize).pipe(
map(result => result === HttpStatus.OK),
);
}
@Action(DepositDataFileAction.ChangeCurrentFolder)
......
......@@ -893,8 +893,9 @@ export class DepositState extends ResourceLogoState<DepositStateModel, Deposit>
fileName = fileName + action.fullFolderName;
}
return this.downloadService.download(url, fileName + this._ZIP_EXTENSION).pipe(
tap(result => {
map(result => {
this.notificationService.showInformation(LabelTranslateEnum.fileDownloadedSuccessfully);
return result === HttpStatus.OK;
}),
);
}
......
......@@ -26,7 +26,7 @@ export class DownloadService {
constructor(private oauth2Service: OAuth2Service,
private httpClient: HttpClient) {}
download(url: string, fileName: string, size?: number): Observable<boolean> {
download(url: string, fileName: string, size?: number): Observable<number> {
if (isNullOrUndefined(streamSaver.WritableStream)) {
streamSaver.WritableStream = WritableStream;
}
......@@ -52,7 +52,7 @@ export class DownloadService {
headers: requestHeaders,
}).then(res => {
if (res.status !== HttpStatus.OK) {
return false;
return res.status;
}
const readableStream = res.body;
// const readableStream = new ReadableStream(res.body); // TODO To allow optimize version on firefox but half work and break Chrome download
......@@ -60,7 +60,7 @@ export class DownloadService {
// Optimized way for supported browser (like Chrome)
if (writableStream && readableStream.pipeTo) {
return readableStream.pipeTo(fileStream)
.then(() => true);
.then(() => HttpStatus.OK);
}
// Standard way for other browser (like Firefox)
......@@ -70,10 +70,10 @@ export class DownloadService {
.then(result => {
if (result.done) {
writer.close();
return true;
return HttpStatus.OK;
} else {
writer.write(result.value).then(pump);
return true;
return HttpStatus.OK;
}
});
return pump();
......
......@@ -375,13 +375,16 @@ export class SharedArchiveState extends ResourceState<SharedArchiveStateModel, A
const fileSize = actionDownload.archiveSize;
this.downloadService.download(`${this._urlResource}/${actionDownload.archiveResId}/${ApiActionEnum.DL}`, fileName, fileSize)
.subscribe((result) => {
if (result) {
ctx.dispatch(new SharedArchiveAction.DownloadSuccess(action));
} else {
ctx.dispatch(new SharedArchiveAction.DownloadFail(action));
}
});
.pipe(
tap((result) => {
if (result === HttpStatus.OK) {
ctx.dispatch(new SharedArchiveAction.DownloadSuccess(action));
} else {
ctx.dispatch(new SharedArchiveAction.DownloadFail(action, result === HttpStatus.FORBIDDEN ? LabelTranslateEnum.notAllowToDownloadArchive : undefined));
}
}),
)
.subscribe();
}
@Action(SharedArchiveAction.DownloadSuccess)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment