deposit-upload.state.ts 3.2 KB
Newer Older
1
2
3
4
5
import {
  Inject,
  Injectable,
} from "@angular/core";
import {ApiEnum} from "@app/shared/enums/api.enum";
6
import {AppSystemPropertyState} from "@app/stores/system-property/app-system-property.state";
7
8
9
10
11
12
13
14
import {DepositDataFile} from "@deposit/models/deposit-data-file.model";
import {DlcmFileUploadWrapper} from "@deposit/models/dlcm-file-upload-wrapper.model";
import {DepositDataFileAction} from "@deposit/stores/data-file/deposit-data-file.action";
import {
  DepositUploadAction,
  depositUploadActionNameSpace,
} from "@deposit/stores/upload/deposit-upload.action";
import {DlcmEnvironment} from "@environments/environment.defaults.model";
15
import {TranslateService} from "@ngx-translate/core";
16
17
18
19
20
21
22
23
24
25
26
27
28
import {
  Actions,
  State,
  Store,
} from "@ngxs/store";
import {StateEnum} from "@shared/enums/state.enum";
import {
  ApiService,
  defaultUploadStateInitValue,
  ENVIRONMENT,
  isNullOrUndefined,
  LABEL_TRANSLATE,
  LabelTranslateInterface,
29
  MemoizedUtil,
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  NotificationService,
  Override,
  StringUtil,
  UploadState,
  UploadStateModel,
} from "solidify-frontend";

export interface DepositUploadStateModel extends UploadStateModel<DepositDataFile> {
}

@Injectable()
@State<DepositUploadStateModel>({
  name: StateEnum.deposit_upload,
  defaults: {
    ...defaultUploadStateInitValue(),
  },
  children: [],
})
export class DepositUploadState extends UploadState<DepositUploadStateModel, DepositDataFile> {
  private readonly _FILE_KEY: string = "file";
  private readonly _CATEGORY_KEY: string = "category";
  private readonly _TYPE_KEY: string = "type";
  private readonly _FOLDER_KEY: string = "folder";

54
55
56
57
  constructor(protected readonly apiService: ApiService,
              protected readonly store: Store,
              protected readonly notificationService: NotificationService,
              protected readonly actions$: Actions,
58
59
              @Inject(ENVIRONMENT) protected readonly _environment: DlcmEnvironment,
              @Inject(LABEL_TRANSLATE) protected readonly _labelTranslateInterface: LabelTranslateInterface,
60
              protected readonly _translate: TranslateService,
61
62
63
64
  ) {
    super(apiService, store, notificationService, actions$, {
      nameSpace: depositUploadActionNameSpace,
      callbackAfterAllUploadFinished: action => store.dispatch(new DepositDataFileAction.Refresh(action.parentId)),
65
66
      fileSizeLimit: () => +MemoizedUtil.selectSnapshot(this.store, AppSystemPropertyState, state => state.current.fileSizeLimit),
    }, _environment, _labelTranslateInterface, _translate);
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  }

  protected get _urlResource(): string {
    return ApiEnum.preIngestDeposits;
  }

  @Override()
  protected generateUploadFormData(action: DepositUploadAction.UploadFile): FormData {
    const formData = new FormData();
    const fileUploadWrapper = action.fileUploadWrapper as DlcmFileUploadWrapper;
    if (!action.isArchive) {
      formData.append(this._FOLDER_KEY, isNullOrUndefined(fileUploadWrapper.subDirectory) ? StringUtil.stringEmpty : fileUploadWrapper.subDirectory);
    }
    formData.append(this._FILE_KEY, fileUploadWrapper.file, fileUploadWrapper.file.name);
    formData.append(this._CATEGORY_KEY, fileUploadWrapper.dataCategory);
    formData.append(this._TYPE_KEY, fileUploadWrapper.dataType);
    return formData;
  }
}