Simplify ServersImporter using file.text() instead of a FileReader

This commit is contained in:
Alejandro Celaya
2023-09-02 19:44:29 +02:00
parent a11a2c84fe
commit 973f3e3c8b
3 changed files with 19 additions and 37 deletions

View File

@@ -8,32 +8,20 @@ const validateServers = (servers: any): servers is ServerData[] =>
Array.isArray(servers) && servers.every(validateServer);
export class ServersImporter {
public constructor(private readonly csvToJson: CsvToJson, private readonly fileReaderFactory: () => FileReader) {}
public constructor(private readonly csvToJson: CsvToJson) {}
public readonly importServersFromFile = async (file?: File | null): Promise<ServerData[]> => {
public async importServersFromFile(file: File | null | undefined): Promise<ServerData[]> {
if (!file) {
throw new Error('No file provided');
}
const reader = this.fileReaderFactory();
const content = await file.text();
const servers = await this.csvToJson(content);
return new Promise((resolve, reject) => {
reader.addEventListener('loadend', async (e: ProgressEvent<FileReader>) => {
try {
// TODO Read as stream, otherwise, if the file is too big, this will block the browser tab
const content = e.target?.result?.toString() ?? '';
const servers = await this.csvToJson(content);
if (!validateServers(servers)) {
throw new Error('Provided file does not have the right format.');
}
if (!validateServers(servers)) {
throw new Error('Provided file does not have the right format.');
}
resolve(servers);
} catch (error) {
reject(error);
}
});
reader.readAsText(file);
});
};
return servers;
}
}

View File

@@ -62,8 +62,7 @@ export const provideServices = (bottle: Bottle, connect: ConnectDecorator) => {
bottle.decorator('ServerError', connect(['servers', 'selectedServer']));
// Services
bottle.constant('fileReaderFactory', () => new FileReader());
bottle.service('ServersImporter', ServersImporter, 'csvToJson', 'fileReaderFactory');
bottle.service('ServersImporter', ServersImporter, 'csvToJson');
bottle.service('ServersExporter', ServersExporter, 'Storage', 'window', 'jsonToCsv');
// Actions