import {Reader} from './reader';
import {ReaderErrors} from './reader-errors';

/**
 * Reads contents of a file.
 * Expects a UTF-8 encoded text file.
 *
 * @author Sebastian Cuy
 * @author Jan G. Wieners
 */
export class FileSystemReader implements Reader {

    constructor(private file: File) {}


    /**
     * Read content of file
     *
     * @returns {Promise<string>} file content | msgWithParams
     */
    public go(): Promise<string> {

        return new Promise((resolve, reject) => {

            let reader = new FileReader();

            reader.onload = (event: any) => {
                resolve(event.target.result);
            };

            reader.onerror = (event: any) => {
                console.error(event.target.error);
                reject([ReaderErrors.FILE_UNREADABLE, this.file.name]);
            };

            reader.readAsText(this.file);
        });
    }
}

import {Reader} from './reader';
import {ReaderErrors} from './reader-errors';

const fs = typeof window !== 'undefined' ? window.require('fs') : require('fs');


/**
 * Reads contents of a file.
 * Expects a UTF-8 encoded text file.
 *
 * @author Sebastian Cuy
 * @author Jan G. Wieners
 * @author Thomas Kleinke
 */
export class FileSystemReader implements Reader {

    constructor(private file: any) {}


    public go(): Promise<string> {

        return new Promise((resolve, reject) => {

            fs.readFile(this.file.path, 'utf-8', (err: any, content: any) => {
                if (err) {
                    reject([ReaderErrors.FILE_UNREADABLE, this.file.path]);
                } else {
                    resolve(content);
                }
            });
        });
    }
}

Use fs instead of FileReader in FileSystemReader to fix a bug where a file could not be loaded if it was changed after selection
Use fs instead of FileReader in FileSystemReader to fix a bug where a file could not be loaded if it was changed after selection
