Commit 1f65c310 authored by Florent POITTEVIN's avatar Florent POITTEVIN
Browse files

WIP Sequential Upload

parent 2e48c661
......@@ -109,11 +109,11 @@ export class DepositUploadContainer extends SharedAbstractDetailEditRoutable<Dep
}
upload($event: FileUploadWrapper): void {
this._store.dispatch(new DepositAction.UploadDataFile(this._resId, $event));
this._store.dispatch(new DepositAction.QueueUploadDataFile(this._resId, $event));
}
uploadArchive($event: FileUploadWrapper): void {
this._store.dispatch(new DepositAction.UploadDataFile(this._resId, $event, true));
this._store.dispatch(new DepositAction.QueueUploadDataFile(this._resId, $event, true));
}
retry($event: UploadFileStatus): void {
......
export enum FileUploadStatusEnum {
pending = "pending",
started = "started",
inProgress = "inProgress",
completed = "completed",
......
......@@ -8,8 +8,8 @@ import {Guid} from "@app/shared/models/guid.model";
import {StateContext} from "@ngxs/store";
export class DataFileUploadHelper {
static addToUploadStatus(ctx: StateContext<DepositStateModel>, fileUploadWrapper: FileUploadWrapper, isArchive: boolean): UploadFileStatus {
const uploadFileStatus = this.createNewUploadFileStatus(fileUploadWrapper, isArchive);
static addToUploadStatus(ctx: StateContext<DepositStateModel>, parentId: string, fileUploadWrapper: FileUploadWrapper, isArchive: boolean): UploadFileStatus {
const uploadFileStatus = this.createNewUploadFileStatus(parentId, fileUploadWrapper, isArchive);
ctx.patchState({
uploadStatus: [
...ctx.getState().uploadStatus,
......@@ -19,11 +19,12 @@ export class DataFileUploadHelper {
return uploadFileStatus;
}
private static createNewUploadFileStatus(fileUploadWrapper: FileUploadWrapper, isArchive: boolean): UploadFileStatus {
private static createNewUploadFileStatus(parentId: string, fileUploadWrapper: FileUploadWrapper, isArchive: boolean, status: FileUploadStatusEnum = FileUploadStatusEnum.pending): UploadFileStatus {
return {
guid: Guid.MakeNew(),
parentId: parentId,
fileUploadWrapper: fileUploadWrapper,
status: FileUploadStatusEnum.started,
status: status,
progressPercentage: 0,
isArchive: isArchive,
} as UploadFileStatus;
......@@ -39,6 +40,12 @@ export class DataFileUploadHelper {
return Math.round(100 * event.loaded / event.total);
}
static updateStartUploadFileStatus(ctx: StateContext<DepositStateModel>, uploadFileStatus: UploadFileStatus): UploadFileStatus {
uploadFileStatus.status = FileUploadStatusEnum.started;
this.updateUploadFileStatus(ctx, uploadFileStatus);
return uploadFileStatus;
}
static updateCompletedUploadFileStatus(ctx: StateContext<DepositStateModel>, uploadFileStatus: UploadFileStatus, depositDataFile: DepositDataFile): void {
uploadFileStatus.progressPercentage = 100;
uploadFileStatus.status = FileUploadStatusEnum.completed;
......
......@@ -6,6 +6,7 @@ import {Guid} from "@app/shared/models/guid.model";
export interface UploadFileStatus {
guid: Guid;
fileUploadWrapper: FileUploadWrapper;
parentId: string;
status: FileUploadStatusEnum;
errorMessageToTranslate: string;
progressPercentage?: number;
......
......@@ -288,10 +288,18 @@ export namespace DepositAction {
}
}
export class QueueUploadDataFile extends BaseAction {
static readonly type: string = `[${state}] Queue Upload Data File`;
constructor(public parentId: string, public fileUploadWrapper: FileUploadWrapper, public isArchive: boolean = false) {
super();
}
}
export class UploadDataFile extends BaseAction {
static readonly type: string = `[${state}] Upload Data File`;
constructor(public parentId: string, public fileUploadWrapper: FileUploadWrapper, public isArchive: boolean = false) {
constructor(public uploadFileStatus: UploadFileStatus) {
super();
}
}
......
......@@ -526,44 +526,49 @@ export class DepositState extends ResourceLogoState<DepositStateModel, Deposit>
this.notificationService.showError(MARK_AS_TRANSLATABLE("deposit.notification.backToEdit.fail"));
}
@Action(DepositAction.QueueUploadDataFile)
queueUploadDataFile(ctx: StateContext<DepositStateModel>, action: DepositAction.QueueUploadDataFile): void {
DataFileUploadHelper.addToUploadStatus(ctx, action.parentId, action.fileUploadWrapper, action.isArchive);
}
@Action(DepositAction.UploadDataFile)
uploadDataFile(ctx: StateContext<DepositStateModel>, action: DepositAction.UploadDataFile): Observable<void> {
ctx.patchState({
isLoadingCounter: ctx.getState().isLoadingCounter + 1,
});
let uploadFileStatus = action.uploadFileStatus;
const formData = new FormData();
let apiAction = ApiActionEnum.UL;
if (action.isArchive) {
if (uploadFileStatus.isArchive) {
apiAction = ApiActionEnum.UL_ARCHIVE;
} else {
formData.append(this._FOLDER_KEY, isNullOrUndefined(action.fileUploadWrapper.subDirectory) ? StringUtil.stringEmpty : action.fileUploadWrapper.subDirectory);
formData.append(this._FOLDER_KEY, isNullOrUndefined(uploadFileStatus.fileUploadWrapper.subDirectory) ? StringUtil.stringEmpty : uploadFileStatus.fileUploadWrapper.subDirectory);
}
formData.append(this._FILE_KEY, action.fileUploadWrapper.file, action.fileUploadWrapper.file.name);
formData.append(this._CATEGORY_KEY, action.fileUploadWrapper.dataCategory);
formData.append(this._TYPE_KEY, action.fileUploadWrapper.dataType);
const metadataType = action.fileUploadWrapper.metadataType;
formData.append(this._FILE_KEY, uploadFileStatus.fileUploadWrapper.file, uploadFileStatus.fileUploadWrapper.file.name);
formData.append(this._CATEGORY_KEY, uploadFileStatus.fileUploadWrapper.dataCategory);
formData.append(this._TYPE_KEY, uploadFileStatus.fileUploadWrapper.dataType);
const metadataType = uploadFileStatus.fileUploadWrapper.metadataType;
if (!isNullOrUndefined(metadataType) && isNonEmptyString(metadataType)) {
formData.append(this._METADATA_TYPE_KEY, metadataType);
}
const uploadFileStatus = DataFileUploadHelper.addToUploadStatus(ctx, action.fileUploadWrapper, action.isArchive);
uploadFileStatus = DataFileUploadHelper.updateStartUploadFileStatus(ctx, uploadFileStatus);
ctx.dispatch(new AppAction.PreventExit(MARK_AS_TRANSLATABLE("notification.uploadInProgress")));
return this.apiService.upload<DepositDataFile>(`${PreIngestResourceApiEnum.deposits}/${action.parentId}/${apiAction}`, formData)
return this.apiService.upload<DepositDataFile>(`${PreIngestResourceApiEnum.deposits}/${uploadFileStatus.parentId}/${apiAction}`, formData)
.pipe(
map((event: UploadEventModel) => {
switch (event.type) {
case HttpEventType.UploadProgress:
const currentUploadFileStatus = DataFileUploadHelper.getCurrentUploadFileStatus(ctx, uploadFileStatus.guid);
if (currentUploadFileStatus.status === FileUploadStatusEnum.canceled) {
ctx.dispatch(new DepositAction.CancelDataFileSending(action.parentId, uploadFileStatus));
ctx.dispatch(new DepositAction.CancelDataFileSending(uploadFileStatus.parentId, uploadFileStatus));
throw environment.errorToSkipInErrorHandler;
}
DataFileUploadHelper.updateInProgressUploadFileStatus(ctx, currentUploadFileStatus, event);
return;
case HttpEventType.Response:
if (event.status === HttpStatus.OK) {
ctx.dispatch(new DepositAction.UploadDataFileSuccess(action, action.parentId, uploadFileStatus, event.body));
ctx.dispatch(new DepositAction.UploadDataFileSuccess(action, uploadFileStatus.parentId, uploadFileStatus, event.body));
} else {
ctx.dispatch(new DepositAction.UploadDataFileFail(action, uploadFileStatus, undefined));
}
......@@ -624,7 +629,7 @@ export class DepositState extends ResourceLogoState<DepositStateModel, Deposit>
@Action(DepositAction.RetrySendDataFile)
retrySendDataFile(ctx: StateContext<DepositStateModel>, action: DepositAction.RetrySendDataFile): void {
DataFileUploadHelper.removeToUploadStatus(ctx, action.uploadFileStatus);
ctx.dispatch(new DepositAction.UploadDataFile(action.parentId, action.uploadFileStatus.fileUploadWrapper, action.uploadFileStatus.isArchive));
ctx.dispatch(new DepositAction.QueueUploadDataFile(action.parentId, action.uploadFileStatus.fileUploadWrapper, action.uploadFileStatus.isArchive));
}
@Action(DepositAction.MarkAsCancelDataFileSending)
......@@ -912,7 +917,7 @@ export class DepositState extends ResourceLogoState<DepositStateModel, Deposit>
private _uploadPhotoReady(ctx: StateContext<DepositStateModel>, action: DepositAction.UploadPhoto): Observable<any> {
const parentId = ctx.getState().current?.resId;
const actionUploadDataFile = new DepositAction.UploadDataFile(parentId, {
const actionUploadDataFile = new DepositAction.QueueUploadDataFile(parentId, {
dataCategory: Enums.DataFile.DataCategoryEnum.Internal,
dataType: Enums.DataFile.DataTypeEnum.DatasetThumbnail,
metadataType: undefined,
......@@ -924,7 +929,7 @@ export class DepositState extends ResourceLogoState<DepositStateModel, Deposit>
take(1),
tap((result) => {
const actionUpdateSuccess = result.action as DepositAction.UploadDataFileSuccess;
if (isTrue(result.result.successful) && actionUpdateSuccess.parentAction === actionUploadDataFile) {
if (isTrue(result.result.successful) /*&& actionUpdateSuccess.parentAction === actionUploadDataFile*/) { // TODO FIND A WAY TO IDENTIFY
const actionUploadDatafileSuccess = actionUpdateSuccess.depositDataFile[0];
ctx.patchState({
dataFileLogo: actionUploadDatafileSuccess,
......@@ -938,7 +943,7 @@ export class DepositState extends ResourceLogoState<DepositStateModel, Deposit>
take(1),
tap((result) => {
const actionUpdateSuccess = result.action as DepositAction.UploadDataFileFail;
if (isTrue(result.result.successful) && actionUpdateSuccess.parentAction === actionUploadDataFile) {
if (isTrue(result.result.successful) /*&& actionUpdateSuccess.parentAction === actionUploadDataFile*/) { // TODO FIND A WAY TO IDENTIFY
ctx.dispatch(new DepositAction.UploadPhotoFail(action));
}
}),
......
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