ndjson direct support

This commit is contained in:
Jan Prochazka
2022-03-27 20:35:30 +02:00
parent a20a34680d
commit 7c0f33383f
5 changed files with 34 additions and 7 deletions

View File

@@ -7,6 +7,8 @@ import { currentDatabase, extensions } from '../stores';
import { getUploadListener } from './uploadFiles';
import { getDatabaseFileLabel } from './getConnectionLabel';
import { apiCall } from './api';
import openNewTab from './openNewTab';
import _ from 'lodash';
export function canOpenByElectron(file, extensions) {
if (!file) return false;
@@ -15,6 +17,7 @@ export function canOpenByElectron(file, extensions) {
if (nameLower.endsWith('.db') || nameLower.endsWith('.sqlite') || nameLower.endsWith('.sqlite3')) return true;
for (const format of extensions.fileFormats) {
if (nameLower.endsWith(`.${format.extension}`)) return true;
if (format.extensions?.find(ext => nameLower.endsWith(`.${ext}`))) return true;
}
return false;
}
@@ -50,6 +53,18 @@ function getFileEncoding(filePath, fs) {
return e;
}
function openElectronJsonLinesFile(filePath, parsed) {
openNewTab({
title: parsed.name,
tooltip: filePath,
icon: 'img sql-file',
tabComponent: 'ArchiveFileTab',
props: {
jslid: `file://${filePath}`,
},
});
}
export function openElectronFileCore(filePath, extensions) {
const nameLower = filePath.toLowerCase();
const path = window.require('path');
@@ -74,6 +89,10 @@ export function openElectronFileCore(filePath, extensions) {
openSqliteFile(filePath);
return;
}
if (nameLower.endsWith('.jsonl') || nameLower.endsWith('.ndjson')) {
openElectronJsonLinesFile(filePath, parsed);
return;
}
for (const format of extensions.fileFormats) {
if (nameLower.endsWith(`.${format.extension}`)) {
if (uploadListener) {
@@ -100,16 +119,19 @@ export function openElectronFileCore(filePath, extensions) {
}
function getFileFormatFilters(extensions) {
return extensions.fileFormats.filter(x => x.readerFunc).map(x => ({ name: x.name, extensions: [x.extension] }));
return extensions.fileFormats
.filter(x => x.readerFunc)
.map(x => ({ name: x.name, extensions: x.extensions || [x.extension] }));
}
function getFileFormatExtensions(extensions) {
return extensions.fileFormats.filter(x => x.readerFunc).map(x => x.extension);
return _.flatten(extensions.fileFormats.filter(x => x.readerFunc).map(x => x.extensions || [x.extension]));
}
export async function openElectronFile() {
const electron = getElectron();
const ext = get(extensions);
const filePaths = await electron.showOpenDialog({
filters: [
{ name: `All supported files`, extensions: ['sql', 'sqlite', 'db', 'sqlite3', ...getFileFormatExtensions(ext)] },