deposit-upload.state.ts 5.36 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
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";
14
import {Enums} from "@enums";
15
import {DlcmEnvironment} from "@environments/environment.defaults.model";
16
import {TranslateService} from "@ngx-translate/core";
17
18
19
20
21
22
23
24
25
26
27
28
29
import {
  Actions,
  State,
  Store,
} from "@ngxs/store";
import {StateEnum} from "@shared/enums/state.enum";
import {
  ApiService,
  defaultUploadStateInitValue,
  ENVIRONMENT,
  isNullOrUndefined,
  LABEL_TRANSLATE,
  LabelTranslateInterface,
30
  MappingObjectUtil,
31
  MemoizedUtil,
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  NotificationService,
  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
58
59
60
61
  private readonly _CHECKSUM_MD5: string = "checksumMd5";
  private readonly _CHECKSUM_MD5_ORIGIN: string = "checksumMd5Origin";
  private readonly _CHECKSUM_SHA1: string = "checksumSha1";
  private readonly _CHECKSUM_SHA1_ORIGIN: string = "checksumSha1Origin";
  private readonly _CHECKSUM_SHA256: string = "checksumSha256";
  private readonly _CHECKSUM_SHA256_ORIGIN: string = "checksumSha256Origin";
  private readonly _CHECKSUM_CRC32: string = "checksumCrc32";
  private readonly _CHECKSUM_CRC32_ORIGIN: string = "checksumCrc32Origin";
62

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

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

82
  protected override generateUploadFormData(action: DepositUploadAction.UploadFile): FormData {
83
84
85
86
87
88
89
90
    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);
91
92
93
94
95
96
97
98
99
100
101
102
103
104
    MappingObjectUtil.forEach(fileUploadWrapper.checksums, (checksum, algo: Enums.DataFile.ChecksumAlgoEnum) => {
      const algoKey = this._getChecksumKey(algo);
      if (isNullOrUndefined(algoKey)) {
        return;
      }
      formData.append(algoKey, checksum);
    });
    MappingObjectUtil.forEach(fileUploadWrapper.checksumsOrigin, (origin: Enums.DataFile.ChecksumOriginEnum, algo: Enums.DataFile.ChecksumAlgoEnum) => {
      const originKey = this._getChecksumOriginKey(algo);
      if (isNullOrUndefined(originKey)) {
        return;
      }
      formData.append(originKey, origin);
    });
105
106
    return formData;
  }
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134

  private _getChecksumKey(checksumAlgo: Enums.DataFile.ChecksumAlgoEnum): string | undefined {
    switch (checksumAlgo) {
      case Enums.DataFile.ChecksumAlgoEnum.MD5:
        return this._CHECKSUM_MD5;
      case Enums.DataFile.ChecksumAlgoEnum.SHA1:
        return this._CHECKSUM_SHA1;
      case Enums.DataFile.ChecksumAlgoEnum.SHA256:
        return this._CHECKSUM_SHA256;
      case Enums.DataFile.ChecksumAlgoEnum.CRC32:
        return this._CHECKSUM_CRC32;
    }
    return undefined;
  }

  private _getChecksumOriginKey(checksumAlgo: Enums.DataFile.ChecksumAlgoEnum): string | undefined {
    switch (checksumAlgo) {
      case Enums.DataFile.ChecksumAlgoEnum.MD5:
        return this._CHECKSUM_MD5_ORIGIN;
      case Enums.DataFile.ChecksumAlgoEnum.SHA1:
        return this._CHECKSUM_SHA1_ORIGIN;
      case Enums.DataFile.ChecksumAlgoEnum.SHA256:
        return this._CHECKSUM_SHA256_ORIGIN;
      case Enums.DataFile.ChecksumAlgoEnum.CRC32:
        return this._CHECKSUM_CRC32_ORIGIN;
    }
    return undefined;
  }
135
}