larvitfiles
Advanced tools
Comparing version 4.0.1 to 4.1.0
'use strict'; | ||
const async = require('async'); | ||
const fs = require('fs'); | ||
const fs = require('fs'); | ||
exports.run = function (req, res, cb) { | ||
console.log('Broken, needs to be updated'); | ||
process.exit(1); | ||
const tasks = []; | ||
const data = {'global': res.globalData, 'prefix': req.fileLib.prefix}; | ||
const data = {global: res.globalData, prefix: req.fileLib.prefix}; | ||
@@ -13,8 +15,8 @@ let file; | ||
data.global.menuControllerName = 'adminFiles'; | ||
data.global.errors = []; | ||
data.global.errors = []; | ||
if (data.global.urlParsed.query.uuid !== undefined) { | ||
tasks.push(function (cb) { | ||
req.fileLib.file({'uuid': data.global.urlParsed.query.uuid}, function (err, fajl) { | ||
file = fajl; | ||
req.fileLib.file({uuid: data.global.urlParsed.query.uuid}, function (err, result) { | ||
file = result; | ||
cb(err); | ||
@@ -28,9 +30,9 @@ }); | ||
newFileData.slug = data.global.formFields.slug; | ||
newFileData.slug = data.global.formFields.slug; | ||
newFileData.metadata = {}; | ||
// Set metadata | ||
for (let i = 0; data.global.formFields.metaDataName[i] !== undefined; i ++) { | ||
for (let i = 0; data.global.formFields.metaDataName[i] !== undefined; i++) { | ||
if (data.global.formFields.metaDataName[i] !== '' && data.global.formFields.metaDataValue[i] !== '') { | ||
const name = data.global.formFields.metaDataName[i]; | ||
const name = data.global.formFields.metaDataName[i]; | ||
const value = data.global.formFields.metaDataValue[i]; | ||
@@ -107,3 +109,3 @@ | ||
} else { | ||
file.slug = newFileData.slug; | ||
file.slug = newFileData.slug; | ||
file.metadata = newFileData.metadata; | ||
@@ -129,4 +131,4 @@ | ||
} else if (file.uuid !== undefined && data.global.urlParsed.query.uuid === undefined) { | ||
req.session.data.nextCallData = {'global': {'messages': ['New file created']}}; | ||
res.statusCode = 302; | ||
req.session.data.nextCallData = {global: {messages: ['New file created']}}; | ||
res.statusCode = 302; | ||
res.setHeader('Location', '/adminFileEdit?uuid=' + file.uuid); | ||
@@ -147,4 +149,4 @@ } else { | ||
req.session.data.nextCallData = {'global': {'messages': ['Movie deleted']}}; | ||
res.statusCode = 302; | ||
req.session.data.nextCallData = {global: {messages: ['Movie deleted']}}; | ||
res.statusCode = 302; | ||
res.setHeader('Location', '/adminFiles'); | ||
@@ -160,8 +162,8 @@ cb(); | ||
data.global.formFields.slug = file.slug; | ||
data.global.formFields.metaDataName = []; | ||
data.global.formFields.slug = file.slug; | ||
data.global.formFields.metaDataName = []; | ||
data.global.formFields.metaDataValue = []; | ||
for (const key of Object.keys(file.metadata)) { | ||
for (let i = 0; file.metadata[key][i] !== undefined; i ++) { | ||
for (let i = 0; file.metadata[key][i] !== undefined; i++) { | ||
data.global.formFields.metaDataName.push(key); | ||
@@ -168,0 +170,0 @@ data.global.formFields.metaDataValue.push(file.metadata[key][i]); |
'use strict'; | ||
exports.run = function (req, res, cb) { | ||
console.log('Broken, needs to be updated'); | ||
process.exit(1); | ||
const files = req.fileLib.files(); | ||
const data = {'global': res.globalData, 'prefix': req.fileLib.prefix}; | ||
const data = {global: res.globalData, prefix: req.fileLib.prefix}; | ||
@@ -7,0 +9,0 @@ data.global.menuControllerName = 'adminFiles'; |
@@ -7,3 +7,3 @@ /** | ||
const Lfs = require('larvitfs'); | ||
const fs = require('fs'); | ||
const fs = require('fs'); | ||
@@ -17,27 +17,28 @@ /** | ||
function run(req, res, cb) { | ||
const lfs = new Lfs({'log': req.log, 'fs': fs}); | ||
const lfs = new Lfs({log: req.log, fs: fs}); | ||
const notFoundPath = lfs.getPathSync('controllers/404.js'); | ||
const url = require('url'); | ||
const url = require('url'); | ||
req.urlParsed = url.parse(req.url); | ||
req.fileLib.get({ | ||
'slug': decodeURIComponent(req.urlParsed.pathname.substr(req.fileLib.prefix.length)) | ||
}).then(function (file) { | ||
if (file.uuid === undefined) { | ||
// 404!!! | ||
require(notFoundPath).run(req, res, cb); | ||
const slug = decodeURIComponent(req.urlParsed.pathname.substr(req.fileLib.prefix.length)); | ||
return; | ||
} | ||
req.fileLib.get({slug}) | ||
.then(function (file) { | ||
if (file.uuid === undefined) { | ||
// 404!!! | ||
require(notFoundPath).run(req, res, cb); | ||
const header = {}; | ||
return; | ||
} | ||
header['Content-Type'] = 'application/octet-stream'; | ||
header['Content-Disposition'] = 'attachment; filename="' + file.slug + '"'; | ||
res.writeHead(200, header); | ||
res.end(file.data); | ||
cb(); | ||
}) | ||
.catch(function (err) { | ||
const header = {}; | ||
header['Content-Type'] = 'application/octet-stream'; | ||
header['Content-Disposition'] = 'attachment; filename="' + file.slug + '"'; | ||
res.writeHead(200, header); | ||
res.end(file.data); | ||
cb(); | ||
}) | ||
.catch(err => { | ||
cb(err, req, res); | ||
@@ -44,0 +45,0 @@ }); |
@@ -5,6 +5,6 @@ 'use strict'; | ||
const topLogPrefix = 'larvitfiles: dataWriter.js: '; | ||
const DbMigration = require('larvitdbmigration'); | ||
const LUtils = require('larvitutils'); | ||
const amsync = require('larvitamsync'); | ||
const async = require('async'); | ||
const DbMigration = require('larvitdbmigration'); | ||
const LUtils = require('larvitutils'); | ||
const amsync = require('larvitamsync'); | ||
const async = require('async'); | ||
@@ -28,7 +28,7 @@ let isReady = false; | ||
if (! that.log) { | ||
if (!that.log) { | ||
that.log = new (new LUtils()).Log(); | ||
} | ||
that.lUtils = new LUtils({'log': that.log}); | ||
that.lUtils = new LUtils({log: that.log}); | ||
@@ -42,5 +42,5 @@ that.listenToQueue(cb); | ||
const logPrefix = topLogPrefix + 'listenToQueue() - '; | ||
const that = this; | ||
const options = {'exchange': that.exchangeName}; | ||
const tasks = []; | ||
const that = this; | ||
const options = {exchange: that.exchangeName}; | ||
const tasks = []; | ||
@@ -50,3 +50,3 @@ let listenMethod; | ||
if (typeof retries === 'function') { | ||
cb = retries; | ||
cb = retries; | ||
retries = 0; | ||
@@ -65,3 +65,3 @@ } | ||
if (that.mode === 'master') { | ||
listenMethod = 'consume'; | ||
listenMethod = 'consume'; | ||
options.exclusive = true; // It is important no other client tries to sneak | ||
@@ -136,7 +136,7 @@ // out messages from us, and we want "consume" | ||
const logPrefix = topLogPrefix + 'ready() - '; | ||
const that = this; | ||
const tasks = []; | ||
const that = this; | ||
const tasks = []; | ||
if (typeof retries === 'function') { | ||
cb = retries; | ||
cb = retries; | ||
retries = 0; | ||
@@ -167,4 +167,4 @@ } | ||
new amsync.SyncClient({ | ||
'exchange': that.exchangeName + '_dataDump', | ||
'intercom': that.intercom | ||
exchange: that.exchangeName + '_dataDump', | ||
intercom: that.intercom | ||
}, cb); | ||
@@ -182,9 +182,9 @@ } else { | ||
options.dbType = 'mariadb'; | ||
options.dbDriver = that.db; | ||
options.tableName = 'larvitfiles_db_version'; | ||
options.dbType = 'mariadb'; | ||
options.dbDriver = that.db; | ||
options.tableName = 'larvitfiles_db_version'; | ||
options.migrationScriptsPath = __dirname + '/dbmigration'; | ||
options.storagePath = that.storagePath; | ||
options.log = that.log; | ||
dbMigration = new DbMigration(options); | ||
options.storagePath = that.storagePath; | ||
options.log = that.log; | ||
dbMigration = new DbMigration(options); | ||
@@ -216,5 +216,5 @@ dbMigration.run(function (err) { | ||
const logPrefix = topLogPrefix + 'rm() - '; | ||
const that = this; | ||
const options = params.data; | ||
const tasks = []; | ||
const that = this; | ||
const options = params.data; | ||
const tasks = []; | ||
@@ -236,3 +236,3 @@ if (options.uuid === undefined) { | ||
if (! uuiBuffer) { | ||
if (!uuiBuffer) { | ||
const err = new Error('Not a valid uuid: ' + options.uuid); | ||
@@ -251,3 +251,3 @@ | ||
if (! uuiBuffer) { | ||
if (!uuiBuffer) { | ||
const err = new Error('Not a valid uuid: ' + options.uuid); | ||
@@ -271,6 +271,6 @@ | ||
const options = { | ||
'exchange': that.exchangeName + '_dataDump', | ||
'host': that.amsync ? that.amsync.host : null, | ||
'minPort': that.amsync ? that.amsync.minPort : null, | ||
'maxPort': that.amsync ? that.amsync.maxPort : null | ||
exchange: that.exchangeName + '_dataDump', | ||
host: that.amsync ? that.amsync.host : null, | ||
minPort: that.amsync ? that.amsync.minPort : null, | ||
maxPort: that.amsync ? that.amsync.maxPort : null | ||
}; | ||
@@ -301,8 +301,8 @@ const args = []; | ||
options.dataDumpCmd = { | ||
'command': 'mysqldump', | ||
'args': args | ||
command: 'mysqldump', | ||
args: args | ||
}; | ||
options['Content-Type'] = 'application/sql'; | ||
options.intercom = that.intercom; | ||
options.intercom = that.intercom; | ||
@@ -314,5 +314,5 @@ new amsync.SyncServer(options, cb); | ||
const logPrefix = topLogPrefix + 'save() - '; | ||
const that = this; | ||
const options = params.data; | ||
const tasks = []; | ||
const that = this; | ||
const options = params.data; | ||
const tasks = []; | ||
@@ -343,3 +343,3 @@ if (options.slug === undefined) { | ||
if (uuid !== null && uuid !== options.uuid) { | ||
const err = new Error('Slug "' + options.slug + '" is take by another file'); | ||
const err = new Error('Slug "' + options.slug + '" is taken by another file'); | ||
@@ -359,3 +359,3 @@ that.log.info(logPrefix + err.message); | ||
if (! uuiBuffer) { | ||
if (!uuiBuffer) { | ||
const err = new Error('Not a valid uuid: ' + options.uuid); | ||
@@ -375,3 +375,3 @@ | ||
if (! uuiBuffer) { | ||
if (!uuiBuffer) { | ||
const err = new Error('Not a valid uuid: ' + options.uuid); | ||
@@ -392,8 +392,12 @@ | ||
if (!options.metadata) { | ||
options.metadata = {}; | ||
} | ||
for (const name of Object.keys(options.metadata)) { | ||
if (! (options.metadata[name] instanceof Array)) { | ||
if (!(options.metadata[name] instanceof Array)) { | ||
options.metadata[name] = [options.metadata[name]]; | ||
} | ||
for (let i = 0; options.metadata[name][i] !== undefined; i ++) { | ||
for (let i = 0; options.metadata[name][i] !== undefined; i++) { | ||
const uuiBuffer = that.lUtils.uuidToBuffer(options.uuid); | ||
@@ -400,0 +404,0 @@ |
'use strict'; | ||
const logPrefix = 'larvitfiles: ./dbmigration/2.js: '; | ||
const LUtils = require('larvitutils'); | ||
const async = require('async'); | ||
const fs = require('fs'); | ||
const LUtils = require('larvitutils'); | ||
const async = require('async'); | ||
const fs = require('fs'); | ||
exports = module.exports = function (cb) { | ||
const tasks = []; | ||
const that = this; | ||
const lUtils = new LUtils({'log': that.log}); | ||
const db = that.options.dbDriver; | ||
const tasks = []; | ||
const that = this; | ||
const lUtils = new LUtils({log: that.log}); | ||
const db = that.options.dbDriver; | ||
@@ -23,3 +23,3 @@ let files; | ||
if (! fs.existsSync(that.options.storagePath)) { | ||
if (!fs.existsSync(that.options.storagePath)) { | ||
tasks.push(function (cb) { | ||
@@ -26,0 +26,0 @@ that.log.info(logPrefix + 'storagePath "' + that.options.storagePath + '" does not exist, creating'); |
137
index.js
@@ -19,2 +19,3 @@ 'use strict'; | ||
* @param {object[]} dbFields - Parameters for the query | ||
* @return {Promise} Promise | ||
*/ | ||
@@ -38,2 +39,3 @@ async function _runQuery(db, sql, dbFields) { | ||
* @param {string} filePath - Path to the file to read | ||
* @return {Promise} Promise | ||
*/ | ||
@@ -78,3 +80,3 @@ async function _readFile(log, filePath) { | ||
if (! options) options = {}; | ||
if (!options) options = {}; | ||
@@ -84,3 +86,3 @@ if (Array.isArray(options.uuids)) { | ||
for (let i = 0; i < options.uuids.length; i ++) { | ||
for (let i = 0; i < options.uuids.length; i++) { | ||
sql += '?,'; | ||
@@ -90,3 +92,3 @@ | ||
if (! uuidBuf) throw new Error('Invalid uuid: "' + options.uuids[i] + '"'); | ||
if (!uuidBuf) throw new Error('Invalid uuid: "' + options.uuids[i] + '"'); | ||
@@ -101,3 +103,3 @@ dbFields.push(uuidBuf); | ||
for (let i = 0; i < options.slugs.length; i ++) { | ||
for (let i = 0; i < options.slugs.length; i++) { | ||
sql += '?,'; | ||
@@ -111,4 +113,4 @@ } | ||
} else { | ||
if (! options.filter) options.filter = {}; | ||
if (! options.order) options.order = {}; | ||
if (!options.filter) options.filter = {}; | ||
if (!options.order) options.order = {}; | ||
@@ -129,10 +131,10 @@ if (options.filter.operator !== 'or') { | ||
if (! (values instanceof Array)) { | ||
if (!(values instanceof Array)) { | ||
values = [values]; | ||
} | ||
for (let i = 0; values[i] !== undefined; i ++) { | ||
for (let i = 0; values[i] !== undefined; i++) { | ||
const value = values[i]; | ||
counter ++; | ||
counter++; | ||
sql += ' JOIN larvitfiles_files_metadata fm' + counter; | ||
@@ -187,7 +189,7 @@ sql += ' ON f.uuid = fm' + counter + '.fileUuid'; | ||
if (! (values instanceof Array)) { | ||
if (!(values instanceof Array)) { | ||
values = [values]; | ||
} | ||
for (let i = 0; values[i] !== undefined; i ++) { | ||
for (let i = 0; values[i] !== undefined; i++) { | ||
const value = values[i]; | ||
@@ -229,9 +231,9 @@ | ||
if (! rows || rows.length === 0) return []; | ||
if (!rows || rows.length === 0) return []; | ||
const dbFiles = rows.map(r => { | ||
return { | ||
'uuid': lUtils.formatUuid(r.uuid), | ||
'slug': r.slug, | ||
'metadata': {} | ||
uuid: lUtils.formatUuid(r.uuid), | ||
slug: r.slug, | ||
metadata: {} | ||
}; | ||
@@ -244,3 +246,3 @@ }); | ||
for (let i = 0; i < dbFiles.length; i ++) { | ||
for (let i = 0; i < dbFiles.length; i++) { | ||
metadataSql += '?,'; | ||
@@ -254,3 +256,3 @@ } | ||
for (let i = 0; metadataRows[i] !== undefined; i ++) { | ||
for (let i = 0; metadataRows[i] !== undefined; i++) { | ||
const row = metadataRows[i]; | ||
@@ -287,27 +289,33 @@ | ||
* @param {string} [options.amsync_maxPort=null] - Max. port in range used when syncing data | ||
* @param {function} cb - Callback | ||
*/ | ||
constructor(options) { | ||
constructor(options, cb) { | ||
const logPrefix = topLogPrefix + 'constructor() - '; | ||
if (! options.db) throw new Error('Missing required option "db"'); | ||
if (! options.storagePath) throw new Error('Missing required option storage path'); | ||
// Make sure there always is a callback function to be called | ||
if (!cb) { | ||
cb = ()=>{}; | ||
} | ||
if (! options.lUtils) { | ||
if (!options.db) throw new Error('Missing required option "db"'); | ||
if (!options.storagePath) throw new Error('Missing required option storage path'); | ||
if (!options.lUtils) { | ||
options.lUtils = new LUtils(); | ||
} | ||
if (! options.log) options.log = new options.lUtils.Log('info'); | ||
if (!options.log) options.log = new options.lUtils.Log('info'); | ||
if (! options.exchangeName) { | ||
if (!options.exchangeName) { | ||
options.exchangeName = 'larvitfiles'; | ||
} | ||
if (! options.prefix) { | ||
if (!options.prefix) { | ||
options.prefix = '/dbfiles/'; | ||
} | ||
if (! options.mode) { | ||
if (!options.mode) { | ||
options.log.info(logPrefix + 'No "mode" option given, defaulting to "noSync"'); | ||
options.mode = 'noSync'; | ||
} else if (['noSync', 'master', 'slave'].indexOf(options.mode) === - 1) { | ||
} else if (['noSync', 'master', 'slave'].indexOf(options.mode) === -1) { | ||
const err = new Error('Invalid "mode" option given: "' + options.mode + '"'); | ||
@@ -319,3 +327,3 @@ | ||
if (! options.intercom) { | ||
if (!options.intercom) { | ||
options.log.info(logPrefix + 'No "intercom" option given, defaulting to "loopback interface"'); | ||
@@ -333,3 +341,3 @@ options.intercom = new Intercom('loopback interface'); | ||
throw err; | ||
return cb(err); | ||
} else { | ||
@@ -339,13 +347,13 @@ this.log.debug(topLogPrefix + 'Folder "' + options.storagePath + '" created if it did not already exist'); | ||
this.dataWriter = new DataWriter({ | ||
'storagePath': this.storagePath, | ||
'exchangeName': this.exchangeName, | ||
'intercom': this.intercom, | ||
'mode': this.mode, | ||
'log': this.log, | ||
'db': this.db, | ||
'amsync_host': options.amsync_host || null, | ||
'amsync_minPort': options.amsync_minPort || null, | ||
'amsync_maxPort': options.amsync_maxPort || null | ||
}); | ||
this.dataWriter = new DataWriter({ | ||
storagePath: this.storagePath, | ||
exchangeName: this.exchangeName, | ||
intercom: this.intercom, | ||
mode: this.mode, | ||
log: this.log, | ||
db: this.db, | ||
amsync_host: options.amsync_host || null, | ||
amsync_minPort: options.amsync_minPort || null, | ||
amsync_maxPort: options.amsync_maxPort || null | ||
}, cb); | ||
}); | ||
@@ -360,7 +368,7 @@ }; | ||
async uuidFromSlug(slug) { | ||
if (! slug) throw new Error('Slug not set'); | ||
if (!slug) throw new Error('Slug not set'); | ||
const rows = await _runQuery(this.db, 'SELECT uuid FROM larvitfiles_files WHERE slug = ?', [slug]); | ||
if (! rows || rows.length === 0) return null; | ||
if (!rows || rows.length === 0) return null; | ||
@@ -422,6 +430,7 @@ return this.lUtils.formatUuid(rows[0].uuid); | ||
* @param {Buffer} file.data - The buffered contents of the file | ||
* @param {boolean} [updateMatchingSlug] - If slug is already taken, update that file. Defaults to false | ||
* @returns {Promise} promise - Returns a promise that resolves to the file saved | ||
*/ | ||
save(file) { | ||
return new Promise((resolve, reject) => { | ||
return new Promise(async (resolve, reject) => { | ||
const logPrefix = topLogPrefix + 'save() - '; | ||
@@ -432,21 +441,30 @@ const tasks = []; | ||
if (! file.slug) { | ||
if (!file.slug) { | ||
throw new Error('Slug is required to save file'); | ||
} | ||
if (! file.uuid) { | ||
if (!file.uuid && file.updateMatchingSlug) { | ||
const existingFile = await this.get({slug: file.slug}); | ||
if (existingFile) { | ||
this.log.verbose(logPrefix + 'Updating file by matching slug: "' + file.slug + '", uuid: "' + file.uuid + '"'); | ||
file.uuid = existingFile.uuid; | ||
} | ||
} | ||
if (!file.uuid) { | ||
file.uuid = uuid(); | ||
this.log.verbose(logPrefix + 'New file with slug "' + file.slug + '" was given uuid "' + file.uuid + '"'); | ||
this.log.verbose(logPrefix + 'New file with slug "' + file.slug + '" was given uuid "' + file.uuid + '"'); | ||
} | ||
tasks.push((cb) => { | ||
const options = {'exchange': this.dataWriter.exchangeName}; | ||
tasks.push(cb => { | ||
const options = {exchange: this.dataWriter.exchangeName}; | ||
const message = {}; | ||
message.action = 'save'; | ||
message.params = {}; | ||
message.action = 'save'; | ||
message.params = {}; | ||
message.params.data = { | ||
'uuid': file.uuid, | ||
'slug': file.slug, | ||
'metadata': file.metadata | ||
uuid: file.uuid, | ||
slug: file.slug, | ||
metadata: file.metadata | ||
}; | ||
@@ -470,3 +488,3 @@ | ||
tasks.push(cb => { | ||
this.get({'uuid': file.uuid}).then(result => { | ||
this.get({uuid: file.uuid}).then(result => { | ||
savedFile = result; | ||
@@ -498,5 +516,5 @@ cb(); | ||
const logPrefix = topLogPrefix + 'rm() - '; | ||
const tasks = []; | ||
const tasks = []; | ||
if (! uuid) { | ||
if (!uuid) { | ||
const err = new Error('uuid is not defined'); | ||
@@ -510,8 +528,8 @@ | ||
tasks.push(cb => { | ||
const options = {'exchange': this.dataWriter.exchangeName}; | ||
const options = {exchange: this.dataWriter.exchangeName}; | ||
const message = {}; | ||
message.action = 'rm'; | ||
message.params = {}; | ||
message.params.data = {'uuid': uuid}; | ||
message.action = 'rm'; | ||
message.params = {}; | ||
message.params.data = {uuid}; | ||
@@ -545,2 +563,1 @@ this.dataWriter.intercom.send(message, options, (err, msgUuid) => { | ||
exports = module.exports = Files; | ||
{ | ||
"name": "larvitfiles", | ||
"version": "4.0.1", | ||
"version": "4.1.0", | ||
"description": "Storage of files with an API and database to use in web environments", | ||
@@ -33,8 +33,11 @@ "main": "index.js", | ||
"devDependencies": { | ||
"coveralls": "^3.0.3", | ||
"eslint": "^5.16.0", | ||
"freeport": "1.0.5", | ||
"larvitbase": "3.1.3", | ||
"larvitdb": "2.1.1", | ||
"larvitbase": "3.1.3", | ||
"mocha": "6.0.2", | ||
"mocha-eslint": "5.0.0" | ||
"mocha-eslint": "5.0.0", | ||
"nyc": "^13.3.0" | ||
} | ||
} |
@@ -1,2 +0,3 @@ | ||
[![Build Status](https://travis-ci.org/larvit/larvitfiles.svg)](https://travis-ci.org/larvit/larvitfiles) [![Dependencies](https://david-dm.org/larvit/larvitfiles.svg)](https://david-dm.org/larvit/larvitfiles.svg) | ||
[![Build Status](https://travis-ci.org/larvit/larvitfiles.svg?branch=master)](https://travis-ci.org/larvit/larvitfiles) [![Dependencies](https://david-dm.org/larvit/larvitfiles.svg)](https://david-dm.org/larvit/larvitfiles.svg) | ||
[![Coverage Status](https://coveralls.io/repos/larvit/larvitfiles/badge.svg)](https://coveralls.io/github/larvit/larvitfiles) | ||
@@ -16,27 +17,28 @@ # larvitfiles | ||
```javascript | ||
const Files = require('larvitfiles'), | ||
db = require('larvitdb'), | ||
fs = require('fs'); | ||
const FileLib = require('larvitfiles'); | ||
const db = require('larvitdb'); | ||
const fs = require('fs'); | ||
let files; | ||
db.setup(conf); // Only needed once per script. See https://github.com/larvit/larvitdb for details | ||
files = new Files({ | ||
'db': db, | ||
'storagePath': '/tmp/larvitfiles', | ||
const fileLib = new FileLib({ | ||
db: db, | ||
storagePath: '/tmp/larvitfiles', | ||
// All below settings are optional, and their default is whats shown here | ||
'log': new (new (require('larvitutils'))).Log(), | ||
'mode': 'noSync', // or 'slave' or 'master' | ||
'intercom': new (require('larvitamintercom'))('loopback interface'), | ||
'exchangeName': 'larvitfiles', | ||
'prefix': '/dbfiles/', | ||
'amsync_host': null, | ||
'amsync_minPort': null, | ||
'amzync_maxPort': null | ||
}); | ||
log: new (new (require('larvitutils'))).Log(), | ||
mode: 'noSync', // or 'slave' or 'master' | ||
intercom: new (require('larvitamintercom'))('loopback interface'), | ||
exchangeName: 'larvitfiles', | ||
prefix: '/dbfiles/', | ||
amsync_host: null, | ||
amsync_minPort: null, | ||
amsync_maxPort: null | ||
}, err => { | ||
// This is called when the initialization (db migration etc) is done | ||
// If err is set, initialization failed | ||
}; | ||
``` | ||
### Add file from buffer | ||
### Add file from disk | ||
@@ -49,6 +51,6 @@ ```javascript | ||
const file = await Files.save({ | ||
'slug': 'slug/foo/bar.txt', | ||
'data': data, | ||
'metadata': {'metadata1': 'metavalue1', 'metadata2': ['multiple', 'values']}, | ||
const file = await fileLib.save({ | ||
slug: 'slug/foo/bar.txt', | ||
data: data, | ||
metadata: {metadata1: 'metavalue1', metadata2: ['multiple', 'values']}, // optional, will erase previous metadata if left blank | ||
//uuid: uuid() - optional | ||
@@ -63,10 +65,33 @@ }); | ||
### Update file on disk | ||
By default .save() will not accept a duplicate slug without also supplying a matching uuid. | ||
If the below script is ran when a file with the slug "slug/foo/bar.txt" already exists in the database, this will throw an error. | ||
```javascript | ||
const file = await fileLib.save({ | ||
slug: 'slug/foo/bar.txt', | ||
data: Buffer.from('någe') | ||
}); | ||
``` | ||
To overwrite the existing file, on the same uuid, use option "updateMatchingSlug": | ||
```javascript | ||
const file = await fileLib.save({ | ||
slug: 'slug/foo/bar.txt', | ||
data: Buffer.from('någe'), | ||
updateMatchingSlug: true // Defaults to false | ||
}); | ||
``` | ||
### Get file from storage | ||
```javascript | ||
file = await Files.get({'slug': 'slug/foo/bar.txt'}); | ||
const file = await fileLib.get({slug: 'slug/foo/bar.txt'}); | ||
// or | ||
// or | ||
file = await Files.get({'uuid': 'uuid of file'}); | ||
const file = await fileLib.get({uuid: 'uuid of file'}); | ||
@@ -82,3 +107,3 @@ console.log('file saved with uuid: ' + file.uuid); | ||
```javascript | ||
Files.rm(await Files.uuidFromSlug('slog/foo/bar.txt')); | ||
fileLib.rm(await fileLib.uuidFromSlug('slog/foo/bar.txt')); | ||
console.log('File is now removed from storage'); | ||
@@ -92,4 +117,4 @@ ``` | ||
```javascript | ||
const files = await Files.list(); | ||
console.log(result); // Array of objects with uuid, slugs, uuids and metadata, but NOT file data as values. | ||
const files = await fileLib.list(); | ||
console.log(result); // Array of objects with uuid, slugs and metadata, but NOT file data as values. | ||
``` | ||
@@ -114,4 +139,4 @@ | ||
const files = await Files.list(options); | ||
console.log(files); // Array of objects with uuid, slugs, uuids and metadata, but NOT file data as values. | ||
const files = await fileLib.list(options); | ||
console.log(files); // Array of objects with uuid, slugs and metadata, but NOT file data as values. | ||
``` | ||
@@ -134,5 +159,5 @@ | ||
const files = await Files.list(options); | ||
console.log(files); // Array of objects with uuid, slugs, uuids and metadata, but NOT file data as values. | ||
const files = await fileLib.list(options); | ||
console.log(files); // Array of objects with uuid, slugs and metadata, but NOT file data as values. | ||
}); | ||
``` |
'use strict'; | ||
const freeport = require('freeport'); | ||
const FileLib = require(__dirname + '/../index.js'); | ||
const assert = require('assert'); | ||
const async = require('async'); | ||
const LUtils = require('larvitutils'); | ||
const http = require('http'); | ||
const App = require('larvitbase'); | ||
const log = new (new LUtils()).Log('warning'); | ||
const lUtils = new LUtils({'log': log}); | ||
const db = require('larvitdb'); | ||
const fs = require('fs'); | ||
const tmpdir = require('os').tmpdir(); | ||
const freeport = require('freeport'); | ||
const FileLib = require(__dirname + '/../index.js'); | ||
const assert = require('assert'); | ||
const async = require('async'); | ||
const LUtils = require('larvitutils'); | ||
const http = require('http'); | ||
const App = require('larvitbase'); | ||
const log = new (new LUtils()).Log('warning'); | ||
const lUtils = new LUtils({log: log}); | ||
const db = require('larvitdb'); | ||
const fs = require('fs'); | ||
const tmpdir = require('os').tmpdir(); | ||
const storagePath = tmpdir + '/larvitfiles'; | ||
let filesLib; | ||
let fileLib; | ||
@@ -79,10 +79,8 @@ before(function (done) { | ||
tasks.push(function (cb) { | ||
filesLib = new FileLib({ | ||
'mode': 'noSync', | ||
'log': log, | ||
'db': db, | ||
'storagePath': storagePath | ||
}); | ||
cb(); | ||
fileLib = new FileLib({ | ||
mode: 'noSync', | ||
log: log, | ||
db: db, | ||
storagePath: storagePath | ||
}, cb); | ||
}); | ||
@@ -125,6 +123,6 @@ | ||
filesLib.save({ | ||
'slug': 'slug/foo/bar.txt', | ||
'data': data, | ||
'metadata': {'metadata1': 'metavalue1', 'metadata2': ['multiple', 'values']} | ||
fileLib.save({ | ||
slug: 'slug/foo/bar.txt', | ||
data: data, | ||
metadata: {metadata1: 'metavalue1', metadata2: ['multiple', 'values']} | ||
}).then((result) => { | ||
@@ -149,11 +147,11 @@ assert.notStrictEqual(result.uuid, undefined); | ||
filesLib.get({'slug': 'slug/foo/bar.txt'}).then(file => { | ||
fileLib.get({slug: 'slug/foo/bar.txt'}).then(file => { | ||
if (err) throw err; | ||
assert.deepEqual(file.uuid, lUtils.formatUuid(file.uuid)); | ||
assert.deepEqual(file.metadata.metadata1, ['metavalue1']); | ||
assert.deepEqual(file.metadata.metadata2, ['multiple', 'values']); | ||
assert.deepEqual(file.uuid, lUtils.formatUuid(file.uuid)); | ||
assert.deepEqual(file.metadata.metadata1, ['metavalue1']); | ||
assert.deepEqual(file.metadata.metadata2, ['multiple', 'values']); | ||
assert.deepEqual(Object.keys(file.metadata).length, 2); | ||
assert.deepEqual(file.slug, 'slug/foo/bar.txt'); | ||
assert.deepEqual(file.data, data); | ||
assert.deepEqual(file.slug, 'slug/foo/bar.txt'); | ||
assert.deepEqual(file.data, data); | ||
@@ -167,13 +165,13 @@ done(); | ||
it('Write another to db', function (done) { | ||
filesLib.save({ | ||
'slug': 'boll.txt', | ||
'data': Buffer.from('buhu'), | ||
'metadata': {'metadata1': 'metavalue2', 'other': 'value'} | ||
fileLib.save({ | ||
slug: 'boll.txt', | ||
data: Buffer.from('buhu'), | ||
metadata: {metadata1: 'metavalue2', other: 'value'} | ||
}).then(file => { | ||
assert.strictEqual(file.uuid, lUtils.formatUuid(file.uuid)); | ||
assert.deepEqual(file.metadata.metadata1, ['metavalue2']); | ||
assert.deepEqual(file.metadata.other, ['value']); | ||
assert.strictEqual(file.uuid, lUtils.formatUuid(file.uuid)); | ||
assert.deepEqual(file.metadata.metadata1, ['metavalue2']); | ||
assert.deepEqual(file.metadata.other, ['value']); | ||
assert.deepEqual(Object.keys(file.metadata).length, 2); | ||
assert.deepEqual(file.slug, 'boll.txt'); | ||
assert.deepEqual(file.data, Buffer.from('buhu')); | ||
assert.deepEqual(file.slug, 'boll.txt'); | ||
assert.deepEqual(file.data, Buffer.from('buhu')); | ||
@@ -186,3 +184,3 @@ done(); | ||
it('List all files in storage', function (done) { | ||
filesLib.list().then(result => { | ||
fileLib.list().then(result => { | ||
assert.deepEqual(result.length, 2); | ||
@@ -203,6 +201,6 @@ | ||
it('Write yet another to db', function (done) { | ||
filesLib.save({ | ||
'slug': 'fippel.txt', | ||
'data': Buffer.from('ðđªßð'), | ||
'metadata': {'foo': ['bar', 'baz', 'buu'], 'other': ['value', 'andThis']} | ||
fileLib.save({ | ||
slug: 'fippel.txt', | ||
data: Buffer.from('ðđªßð'), | ||
metadata: {foo: ['bar', 'baz', 'buu'], other: ['value', 'andThis']} | ||
}).then(() => done()) | ||
@@ -212,12 +210,52 @@ .catch(done); | ||
it('Write to db with no metadata', done => { | ||
fileLib.save({ | ||
slug: 'bajjen.txt', | ||
data: Buffer.from('eekslfa') | ||
}).then(file => { | ||
assert.notStrictEqual(file.uuid, undefined); | ||
done(); | ||
}); | ||
}); | ||
it('Update a file', done => { | ||
fileLib.save({ | ||
slug: 'fiffel.txt', | ||
data: Buffer.from('o båg') | ||
}).then(() => { | ||
fileLib.save({ | ||
slug: 'fiffel.txt', | ||
data: Buffer.from('o fil') | ||
}).then(() => { | ||
throw new Error('Should not be able to save file with same slug'); | ||
}).catch(err => { | ||
assert.strictEqual(err.message, 'Slug "fiffel.txt" is taken by another file'); | ||
fileLib.get({slug: 'fiffel.txt'}) | ||
.then(file => { | ||
assert.strictEqual(file.data.toString(), 'o båg'); | ||
fileLib.save({ | ||
slug: 'fiffel.txt', | ||
data: Buffer.from('wakka'), | ||
updateMatchingSlug: true | ||
}).then(file => { | ||
assert.strictEqual(file.data.toString(), 'wakka'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('List files in storage filtered by exact metadata', function (done) { | ||
filesLib.list({'filter': { 'metadata': {'metadata1': 'metavalue2'}}}).then(result => { | ||
fileLib.list({filter: { metadata: {metadata1: 'metavalue2'}}}).then(result => { | ||
assert.deepEqual(result.length, 1); | ||
assert.notStrictEqual(result[0].uuid, undefined); | ||
assert.deepEqual(result[0].metadata.metadata1, ['metavalue2']); | ||
assert.deepEqual(result[0].metadata.other, ['value']); | ||
assert.notStrictEqual(result[0].uuid, undefined); | ||
assert.deepEqual(result[0].metadata.metadata1, ['metavalue2']); | ||
assert.deepEqual(result[0].metadata.other, ['value']); | ||
assert.deepEqual(Object.keys(result[0].metadata).length, 2); | ||
assert.deepEqual(result[0].slug, 'boll.txt'); | ||
assert.deepEqual(result[0].data, undefined); | ||
assert.deepEqual(result[0].slug, 'boll.txt'); | ||
assert.deepEqual(result[0].data, undefined); | ||
@@ -231,6 +269,6 @@ done(); | ||
const options = { | ||
'filter': { | ||
'metadata': { | ||
'other': 'value', | ||
'metadata1': 'metavalue2' | ||
filter: { | ||
metadata: { | ||
other: 'value', | ||
metadata1: 'metavalue2' | ||
} | ||
@@ -240,3 +278,3 @@ } | ||
filesLib.list(options).then(result => { | ||
fileLib.list(options).then(result => { | ||
assert.strictEqual(result.length, 1); | ||
@@ -251,5 +289,5 @@ | ||
const options = { | ||
'filter': { | ||
'metadata': { | ||
'other': 'value' | ||
filter: { | ||
metadata: { | ||
other: 'value' | ||
} | ||
@@ -259,3 +297,3 @@ } | ||
filesLib.list(options).then(result => { | ||
fileLib.list(options).then(result => { | ||
assert.strictEqual(result.length, 2); | ||
@@ -270,5 +308,5 @@ | ||
const options = { | ||
'filter': { | ||
'metadata': { | ||
'metadata1': true | ||
filter: { | ||
metadata: { | ||
metadata1: true | ||
} | ||
@@ -278,3 +316,3 @@ } | ||
filesLib.list(options).then(result => { | ||
fileLib.list(options).then(result => { | ||
assert.strictEqual(result.length, 2); | ||
@@ -289,6 +327,6 @@ | ||
const options = { | ||
'filter': { | ||
'metadata': { | ||
'other': 'value', | ||
'metadata1': true | ||
filter: { | ||
metadata: { | ||
other: 'value', | ||
metadata1: true | ||
} | ||
@@ -298,3 +336,3 @@ } | ||
filesLib.list(options).then(result => { | ||
fileLib.list(options).then(result => { | ||
assert.deepEqual(Object.keys(result).length, 1); | ||
@@ -309,5 +347,5 @@ | ||
const options = { | ||
'filter': { | ||
'metadata': { | ||
'other': ['value', 'andThis'] | ||
filter: { | ||
metadata: { | ||
other: ['value', 'andThis'] | ||
} | ||
@@ -317,3 +355,3 @@ } | ||
filesLib.list(options).then(result => { | ||
fileLib.list(options).then(result => { | ||
assert.deepEqual(Object.keys(result).length, 1); | ||
@@ -328,12 +366,12 @@ | ||
const options = { | ||
'filter': { | ||
'metadata': { | ||
'metadata1': 'metavalue2', | ||
'foo': 'baz' | ||
filter: { | ||
metadata: { | ||
metadata1: 'metavalue2', | ||
foo: 'baz' | ||
}, | ||
'operator': 'or' | ||
operator: 'or' | ||
} | ||
}; | ||
filesLib.list(options).then(result => { | ||
fileLib.list(options).then(result => { | ||
assert.deepEqual(Object.keys(result).length, 2); | ||
@@ -365,7 +403,7 @@ | ||
let app = new App({ | ||
'log': log, | ||
'httpOptions': port, | ||
'middlewares': [ | ||
log: log, | ||
httpOptions: port, | ||
middlewares: [ | ||
function (req, res, cb) { | ||
req.fileLib = filesLib; | ||
req.fileLib = fileLib; | ||
cb(null, req, res); | ||
@@ -390,3 +428,3 @@ }, | ||
tasks.push(function (cb) { | ||
const req = http.request({'port': port, 'path': filesLib.prefix + 'slug/foo/bar.txt'}, function (res) { | ||
const req = http.request({port: port, path: fileLib.prefix + 'slug/foo/bar.txt'}, function (res) { | ||
assert.deepEqual(res.statusCode, 200); | ||
@@ -409,4 +447,4 @@ res.on('data', function (chunk) { | ||
tasks.push(function (cb) { | ||
filesLib.uuidFromSlug('slug/foo/bar.txt').then(uuid => { | ||
filesLib.rm(uuid).then(cb) | ||
fileLib.uuidFromSlug('slug/foo/bar.txt').then(uuid => { | ||
fileLib.rm(uuid).then(cb) | ||
.catch(cb); | ||
@@ -435,11 +473,11 @@ }) | ||
tasks.push(function (cb) { | ||
filesLib.get({'slug': 'boll.txt'}).then(filen => { | ||
file = filen; | ||
fileLib.get({slug: 'boll.txt'}).then(filen => { | ||
file = filen; | ||
assert.deepEqual(file.uuid, lUtils.formatUuid(file.uuid)); | ||
assert.deepEqual(file.metadata.metadata1, ['metavalue2']); | ||
assert.deepEqual(file.metadata.other, ['value']); | ||
assert.deepEqual(file.uuid, lUtils.formatUuid(file.uuid)); | ||
assert.deepEqual(file.metadata.metadata1, ['metavalue2']); | ||
assert.deepEqual(file.metadata.other, ['value']); | ||
assert.deepEqual(Object.keys(file.metadata).length, 2); | ||
assert.deepEqual(file.slug, 'boll.txt'); | ||
assert.deepEqual(file.data, Buffer.from('buhu')); | ||
assert.deepEqual(file.slug, 'boll.txt'); | ||
assert.deepEqual(file.data, Buffer.from('buhu')); | ||
@@ -454,9 +492,9 @@ cb(); | ||
filesLib.save(file).then(result => { | ||
assert.deepEqual(result.uuid, lUtils.formatUuid(file.uuid)); | ||
assert.deepEqual(result.metadata.metadata1, ['metavalue2']); | ||
assert.deepEqual(result.metadata.other, ['value']); | ||
fileLib.save(file).then(result => { | ||
assert.deepEqual(result.uuid, lUtils.formatUuid(file.uuid)); | ||
assert.deepEqual(result.metadata.metadata1, ['metavalue2']); | ||
assert.deepEqual(result.metadata.other, ['value']); | ||
assert.deepEqual(Object.keys(result.metadata).length, 2); | ||
assert.deepEqual(result.slug, 'somethingNewAndShiny.txt'); | ||
assert.deepEqual(result.data, Buffer.from('buhu')); | ||
assert.deepEqual(result.slug, 'somethingNewAndShiny.txt'); | ||
assert.deepEqual(result.data, Buffer.from('buhu')); | ||
@@ -477,10 +515,10 @@ cb(); | ||
tasks.push(function (cb) { | ||
filesLib.get({'slug': 'somethingNewAndShiny.txt'}).then(filen => { | ||
file = filen; | ||
fileLib.get({slug: 'somethingNewAndShiny.txt'}).then(filen => { | ||
file = filen; | ||
assert.deepEqual(file.metadata.metadata1, ['metavalue2']); | ||
assert.deepEqual(file.metadata.other, ['value']); | ||
assert.deepEqual(file.metadata.metadata1, ['metavalue2']); | ||
assert.deepEqual(file.metadata.other, ['value']); | ||
assert.deepEqual(Object.keys(file.metadata).length, 2); | ||
assert.deepEqual(file.slug, 'somethingNewAndShiny.txt'); | ||
assert.deepEqual(file.data, Buffer.from('buhu')); | ||
assert.deepEqual(file.slug, 'somethingNewAndShiny.txt'); | ||
assert.deepEqual(file.data, Buffer.from('buhu')); | ||
@@ -495,7 +533,7 @@ cb(); | ||
filesLib.save(file).then(() => { | ||
fileLib.save(file).then(() => { | ||
throw new Error('This should not happen!'); | ||
}) | ||
.catch(err => { | ||
assert.strictEqual(err.message, 'Slug "fippel.txt" is take by another file'); | ||
assert.strictEqual(err.message, 'Slug "fippel.txt" is taken by another file'); | ||
cb(); | ||
@@ -502,0 +540,0 @@ }); |
@@ -7,7 +7,7 @@ 'use strict'; | ||
// Increase the timeout of the test if linting takes to long | ||
'timeout': 5000, // Defaults to the global mocha `timeout` option | ||
timeout: 5000, // Defaults to the global mocha `timeout` option | ||
// Increase the time until a test is marked as slow | ||
'slow': 1000 // Defaults to the global mocha `slow` option | ||
slow: 1000 // Defaults to the global mocha `slow` option | ||
} | ||
); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
59603
22
1448
157
8