All files / app/codeCharta/ui/toolBar/uploadFilesButton uploadFiles.service.ts

35.89% Statements 14/39
0% Branches 0/3
20% Functions 1/5
34.21% Lines 13/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 735x 5x 5x 5x 5x 5x 5x 5x 5x     5x 2x     2x 2x                                                                                                                
import { Injectable } from "@angular/core"
import { Store } from "@ngrx/store"
import { LoadFileService } from "../../../services/loadFile/loadFile.service"
import { setIsLoadingFile } from "../../../state/store/appSettings/isLoadingFile/isLoadingFile.actions"
import { setIsLoadingMap } from "../../../state/store/appSettings/isLoadingMap/isLoadingMap.actions"
import { CustomConfigHelper, CUSTOM_CONFIG_FILE_EXTENSION } from "../../../util/customConfigHelper"
import { getCCFileAndDecorateFileChecksum } from "../../../util/fileHelper"
import { createCCFileInput } from "../../../util/uploadFiles/createCCFileInput"
import { readFiles } from "../../../util/uploadFiles/readFiles"
 
@Injectable({ providedIn: "root" })
export class UploadFilesService {
    isUploading = false
 
    constructor(
        private store: Store,
        private loadFileService: LoadFileService
    ) {}
 
    uploadFiles() {
        const ccFileInput = createCCFileInput()
        ccFileInput.addEventListener("change", () => {
            void this.uploadFilesOnEvent(ccFileInput)
        })
 
        ccFileInput.click()
    }
 
    private async uploadFilesOnEvent(ccFileInput: HTMLInputElement) {
        try {
            this.isUploading = true
            this.store.dispatch(setIsLoadingFile({ value: true }))
            this.store.dispatch(setIsLoadingMap({ value: true }))
 
            const plainFileContents = await Promise.all(readFiles(ccFileInput.files))
            const { customConfigs, ccFiles } = this.splitCustomConfigsAndCCFiles(ccFileInput.files, plainFileContents)
 
            for (const customConfig of customConfigs) {
                CustomConfigHelper.importCustomConfigs(customConfig)
            }
 
            Iif (ccFiles.length > 0) {
                this.loadFileService.loadFiles(ccFiles)
            }
        } catch {
            this.store.dispatch(setIsLoadingFile({ value: false }))
            this.store.dispatch(setIsLoadingMap({ value: false }))
        } finally {
            this.isUploading = false
        }
    }
 
    private splitCustomConfigsAndCCFiles(fileList: FileList, contents: string[]) {
        const customConfigs = []
        const ccFiles = []
 
        for (const [index, content] of contents.entries()) {
            const fileName = fileList[index].name
            if (fileName.includes(CUSTOM_CONFIG_FILE_EXTENSION)) {
                customConfigs.push(content)
            } else {
                ccFiles.push({
                    fileName,
                    fileSize: fileList[index].size,
                    content: getCCFileAndDecorateFileChecksum(content)
                })
            }
        }
 
        return { customConfigs, ccFiles }
    }
}