deposit-upload.state.ts 5.62 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
import {
  Actions,
  State,
  Store,
} from "@ngxs/store";
import {StateEnum} from "@shared/enums/state.enum";
import {
  ApiService,
  defaultUploadStateInitValue,
  ENVIRONMENT,
27
  isNotNullNorUndefinedNorWhiteString,
28
29
30
  isNullOrUndefined,
  LABEL_TRANSLATE,
  LabelTranslateInterface,
31
  MappingObjectUtil,
32
  MemoizedUtil,
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  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";
55
  private readonly _METADATA_TYPE_KEY: string = "metadataType";
56
57
58
59
60
61
62
63
  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";
64

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

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

84
  protected override generateUploadFormData(action: DepositUploadAction.UploadFile): FormData {
85
86
87
88
89
    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);
    }
90
91
92
    if (isNotNullNorUndefinedNorWhiteString(fileUploadWrapper.metadataType)) {
      formData.append(this._METADATA_TYPE_KEY, fileUploadWrapper.metadataType);
    }
93
94
95
    formData.append(this._FILE_KEY, fileUploadWrapper.file, fileUploadWrapper.file.name);
    formData.append(this._CATEGORY_KEY, fileUploadWrapper.dataCategory);
    formData.append(this._TYPE_KEY, fileUploadWrapper.dataType);
96
97
98
99
100
101
102
103
104
105
106
107
108
109
    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);
    });
110
111
    return formData;
  }
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

  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;
  }
140
}