archive, export into archive

This commit is contained in:
Jan Prochazka
2020-10-17 17:59:36 +02:00
parent 39a4c39b6d
commit b0f0710a75
22 changed files with 357 additions and 33 deletions

View File

@@ -0,0 +1,41 @@
const fs = require('fs-extra');
const path = require('path');
const { archivedir } = require('../utility/directories');
const socket = require('../utility/socket');
module.exports = {
folders_meta: 'get',
async folders() {
const folders = await fs.readdir(archivedir());
return [
{
name: 'default',
type: 'jsonl',
},
...folders
.filter((x) => x != 'default')
.map((name) => ({
name,
type: 'jsonl',
})),
];
},
createFolder_meta: 'post',
async createFolder({ folder }) {
await fs.mkdir(path.join(archivedir(), folder));
socket.emitChanged('archive-folders-changed');
return true;
},
files_meta: 'get',
async files({ folder }) {
const files = await fs.readdir(path.join(archivedir(), folder));
return files
.filter((name) => name.endsWith('.jsonl'))
.map((name) => ({
name: name.slice(0, -'.jsonl'.length),
type: 'jsonl',
}));
},
};

View File

@@ -1,7 +1,7 @@
const path = require('path');
const fs = require('fs');
const lineReader = require('line-reader');
const { jsldir } = require('../utility/directories');
const { jsldir, archivedir } = require('../utility/directories');
const socket = require('../utility/socket');
function readFirstLine(file) {
@@ -20,6 +20,14 @@ function readFirstLine(file) {
});
}
function getJslFileName(jslid) {
const archiveMatch = jslid.match(/^archive:\/\/([^/]+)\/(.*)$/);
if (archiveMatch) {
return path.join(archivedir(), archiveMatch[1], `${archiveMatch[2]}.jsonl`);
}
return path.join(jsldir(), `${jslid}.jsonl`);
}
module.exports = {
openedReaders: {},
@@ -57,7 +65,7 @@ module.exports = {
// 'OPENING READER, LINES=',
// fs.readFileSync(path.join(jsldir(), `${jslid}.jsonl`), 'utf-8').split('\n').length
// );
const file = path.join(jsldir(), `${jslid}.jsonl`);
const file = getJslFileName(jslid);
return new Promise((resolve, reject) =>
lineReader.open(file, (err, reader) => {
if (err) reject(err);
@@ -93,7 +101,7 @@ module.exports = {
getInfo_meta: 'get',
async getInfo({ jslid }) {
const file = path.join(jsldir(), `${jslid}.jsonl`);
const file = getJslFileName(jslid);
const firstLine = await readFirstLine(file);
if (firstLine) return JSON.parse(firstLine);
return null;
@@ -119,8 +127,9 @@ module.exports = {
getStats_meta: 'get',
getStats({ jslid }) {
const file = path.join(jsldir(), `${jslid}.jsonl.stats`);
return JSON.parse(fs.readFileSync(file, 'utf-8'));
const file = `${getJslFileName(jslid)}.stats`;
if (fs.existsSync(file)) return JSON.parse(fs.readFileSync(file, 'utf-8'));
return {};
},
async notifyChangedStats(stats) {