Skip to content

Commit

Permalink
feat: allow spaces to be saved
Browse files Browse the repository at this point in the history
Use lowdb to save spaces.

closes #42 #43
  • Loading branch information
juancarlosfarah committed May 1, 2019
1 parent 4433367 commit 1de5dfa
Show file tree
Hide file tree
Showing 16 changed files with 757 additions and 468 deletions.
16 changes: 0 additions & 16 deletions app/channels.js

This file was deleted.

42 changes: 42 additions & 0 deletions app/config/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// eslint-disable-next-line import/no-extraneous-dependencies
const { app } = require('electron');

// types that we support downloading
const DOWNLOADABLE_MIME_TYPES = [
// video
'application/mp4',
'application/ogg',
'video/mp4',
'video/ogg',
'video/quicktime',
'video/webm',
// audio
'audio/mp4',
'audio/mpeg',
'audio/ogg',
'audio/webm',
'audio/x-aac',
'audio/x-wav',
// image
'image/bmp',
'image/gif',
'image/jpeg',
'image/png',
// pdf
'application/pdf',
];

// categories
const RESOURCE = 'Resource';
const APPLICATION = 'Application';

const VAR_FOLDER = `${app.getPath('userData')}/var`;
const DATABASE_PATH = `${VAR_FOLDER}/db.json`;

module.exports = {
DOWNLOADABLE_MIME_TYPES,
RESOURCE,
APPLICATION,
DATABASE_PATH,
VAR_FOLDER,
};
38 changes: 38 additions & 0 deletions app/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const mkdirp = require('mkdirp');
const low = require('lowdb');
const fs = require('fs');
const FileSync = require('lowdb/adapters/FileSync');
const logger = require('./logger');
const { DATABASE_PATH, VAR_FOLDER } = require('./config/config');

// use promisified fs
const fsPromises = fs.promises;

// bootstrap database
const ensureDatabaseExists = async (dbPath = DATABASE_PATH) => {
try {
await fsPromises.readFile(dbPath, { encoding: 'utf8' });
} catch (readErr) {
logger.error(readErr);
try {
mkdirp.sync(VAR_FOLDER);
await fsPromises.writeFile(dbPath, '');
} catch (writeErr) {
logger.error(writeErr);
}
}
};

const bootstrapDatabase = (dbPath = DATABASE_PATH) => {
const adapter = new FileSync(dbPath);
const db = low(adapter);

// set some defaults (required if json file is empty)
db.defaults({ spaces: [] }).write();
return db;
};

module.exports = {
ensureDatabaseExists,
bootstrapDatabase,
};
Loading

0 comments on commit 1de5dfa

Please sign in to comment.