All files / app/codeCharta/util/uploadFiles readFiles.ts

96% Statements 24/25
71.42% Branches 5/7
100% Functions 6/6
95.45% Lines 21/22

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 3812x 12x 12x   12x 2x   2x 2x   2x     12x 2x 2x 2x 2x 1x   1x         2x 2x 2x 2x         2x 2x      
import { parseGameObjectsFile } from "../gameObjectsParser/gameObjectsImporter"
import { validateGameObjects } from "../gameObjectsParser/gameObjectsValidator"
import { ungzip } from "pako"
 
export const readFiles = (files: FileList): Promise<string>[] => {
    const readFilesPromises = []
    // biome-ignore lint/style/useForOf: FileList is not iterable, therefore we cannot use for-of loop
    for (let index = 0; index < files.length; index++) {
        readFilesPromises.push(readFile(files[index]))
    }
    return readFilesPromises
}
 
const readFile = async (file: File): Promise<string> =>
    new Promise(resolve => {
        const isCompressed = file.name.endsWith(".gz")
        const reader = new FileReader()
        if (isCompressed) {
            reader.readAsArrayBuffer(file)
        } else {
            reader.readAsText(file, "utf8")
        }
 
        let content: string
 
        reader.onload = event => {
            const result = event.target.result.toString()
            content = isCompressed ? ungzip(event.target.result, { to: "string" }) : result
            Iif (result.includes("gameObjectPositions") && validateGameObjects(result)) {
                content = JSON.stringify(parseGameObjectsFile(result))
            }
        }
 
        reader.onloadend = () => {
            resolve(content)
        }
    })