{"size": 1312, "ext": "js", "max_stars_repo_path": "src/wrappers/gdalOpen.js", "max_stars_repo_name": "unitehenry/loam", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/wrappers/gdalOpen.js", "max_issues_repo_name": "unitehenry/loam", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/wrappers/gdalOpen.js", "max_forks_repo_name": "unitehenry/loam", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 30.511627907, "max_line_length": 87, "alphanum_fraction": 0.5533536585}
import randomKey from '../randomKey.js';

/* global FS WORKERFS */
export default function (GDALOpen, errorHandling, rootPath) {
    return function (file) {
        let filename;

        let directory = rootPath + randomKey();

        FS.mkdir(directory);

        if (file instanceof File) {
            filename = file.name;
            FS.mount(WORKERFS, { files: [file] }, directory);
        } else if (file instanceof Blob) {
            filename = 'geotiff.tif';
            FS.mount(WORKERFS, { blobs: [{ name: filename, data: file }] }, directory);
        }
        let filePath = directory + '/' + filename;

        let datasetPtr = GDALOpen(filePath);

        let errorType = errorHandling.CPLGetLastErrorType();

        // Check for errors; clean up and throw if error is detected
        if (errorType === errorHandling.CPLErr.CEFailure ||
                errorType === errorHandling.CPLErr.CEFatal) {
            FS.unmount(directory);
            FS.rmdir(directory);
            let message = errorHandling.CPLGetLastErrorMsg();

            throw new Error(message);
        } else {
            return {
                datasetPtr: datasetPtr,
                filePath: filePath,
                directory: directory,
                filename: filename
            };
        }
    };
}
