All files / function getFileBytes.js

100% Statements 9/9
100% Branches 3/3
100% Functions 2/2
100% Lines 9/9

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                                                      12x   12x 3x 3x     9x 6x   3x     9x 9x      
import { GDALFunctions } from '../../allCFunctions';
 
/**
    * Get bytes of the file.
    *
    * @module f/getFileBytes
    * @async
    * @param {string|TypeDefs.FilePath} filePath The path of the file to be downloaded.
    * @return {Promise<Uint8Array>} "Promise" returns an array of byte of the file.
    * @example
    * // Download file from "/output" path on the browser.
    * const files = await Gdal.getOutputFiles();
    * const filePath = files[0].path;
    * const fileBytes = Gdal.getFileBytes(filePath);
    * const fileName = filePath.split('/').pop();
    * saveAs(fileBytes, filename);
    *
    * function saveAs(fileBytes, fileName) {
    *    const blob = new Blob([fileBytes]);
    *    const link = document.createElement('a');
    *    link.href = URL.createObjectURL(blob);
    *    link.download = fileName;
    *    link.click();
    * }
*/
 
export default function getFileBytes(filePath) {
    return new Promise((resolve) => {
        let path;
        if (!filePath) {
            resolve(new Uint8Array());
            return;
        }
 
        if (filePath.local) {
            path = filePath.local;
        } else {
            path = filePath;
        }
 
        const bytes = GDALFunctions.Module.FS.readFile(path, { encoding: 'binary' });
        resolve(bytes);
    });
}