larvitfiles
Advanced tools
Comparing version 2.0.1 to 2.1.0
@@ -8,8 +8,7 @@ /** | ||
notFoundPath = lfs.getPathSync('controllers/404.js'), | ||
lFiles = require(__dirname + '/../index.js'), | ||
conf = require(lfs.getPathSync('config/larvitfiles.json')); | ||
lFiles = require(__dirname + '/../index.js'); | ||
exports.run = function (req, res, cb) { | ||
const file = new lFiles.File({ | ||
'slug': req.urlParsed.pathname.substring(conf.prefix.length) | ||
'slug': req.urlParsed.pathname.substring(lFiles.prefix.length) | ||
}, function (err) { | ||
@@ -16,0 +15,0 @@ if (err) { cb(err, req, res, {}); return; } |
281
index.js
'use strict'; | ||
const DbMigration = require('larvitdbmigration'), | ||
const dataWriter = require(__dirname + '/dataWriter.js'), | ||
logPrefix = 'larvitfiles ./index.js: ', | ||
uuidLib = require('uuid'), | ||
@@ -8,7 +9,27 @@ utils = require('larvitutils'), | ||
log = require('winston'), | ||
fs = require('fs'), | ||
db = require('larvitdb'); | ||
let dbReady = false, | ||
readyRunning = false; | ||
let config; | ||
if (fs.existsSync(process.cwd() + '/config/larvitfiles.json')) { | ||
config = require(process.cwd() + '/config/larvitfiles.json'); | ||
} else { | ||
config = {}; | ||
} | ||
if (config.storagePath !== undefined) { | ||
exports.storagePath = config.storagePath; | ||
} else { | ||
exports.storagePath = process.cwd() + '/larvitfiles'; | ||
} | ||
if (config.prefix) { | ||
exports.prefix = config.prefix; | ||
} else { | ||
exports.prefix = '/dbfiles/'; | ||
} | ||
dataWriter.ready(); | ||
function File(options, cb) { | ||
@@ -20,5 +41,4 @@ const tasks = [], | ||
const err = new Error('First parameter must be an object.'); | ||
log.warn('larvitviles: File() - ' + err.message); | ||
cb(err); | ||
return; | ||
log.info('larvitviles: File() - ' + err.message); | ||
return cb(err); | ||
} | ||
@@ -31,5 +51,5 @@ | ||
// There must always be a metadata object | ||
that.metadata = {}; | ||
that.metadata = {}; | ||
tasks.push(ready); | ||
tasks.push(dataWriter.ready); | ||
@@ -39,8 +59,7 @@ if (options.slug !== undefined && options.slug !== '') { | ||
db.query('SELECT uuid, slug FROM larvitfiles_files WHERE slug = ?', [options.slug], function (err, rows) { | ||
if (err) { cb(err); return; } | ||
if (err) return cb(err); | ||
if (rows.length === 0) { | ||
that.slug = options.slug; | ||
cb(); | ||
return; | ||
that.slug = options.slug; | ||
return cb(); | ||
} | ||
@@ -57,11 +76,9 @@ | ||
const err = new Error('Invalid uuid supplied: "' + options.uuid + '"'); | ||
log.warn('larvitviles: File() - ' + err.message); | ||
cb(err); | ||
return; | ||
log.info('larvitviles: File() - ' + err.message); | ||
return cb(err); | ||
} | ||
} else { | ||
const err = new Error('Options must contain either slug or uuid. Neither was provided.'); | ||
log.warn('larvitviles: File() - ' + err.message); | ||
cb(err); | ||
return; | ||
log.info('larvitviles: File() - ' + err.message); | ||
return cb(err); | ||
} | ||
@@ -71,4 +88,4 @@ | ||
if (that.uuid === undefined) { | ||
cb(); | ||
return; | ||
that.uuid = uuidLib.v4(); | ||
return cb(); | ||
} | ||
@@ -83,3 +100,2 @@ | ||
if (options.metadata) { that.metadata = options.metadata; } | ||
cb(); | ||
@@ -95,7 +111,19 @@ }); | ||
tasks.push(ready); | ||
if ( ! that.uuid) { | ||
const e = new Error('uuid is not defined'); | ||
log.info(logPrefix + 'loadFromDb() - ' + e.message); | ||
return cb(e); | ||
} | ||
if (exports.storagePath === null) { | ||
const e = new Error('storagePath not set'); | ||
log.info(logPrefix + 'loadFromDb() - ' + e.message); | ||
return cb(e); | ||
} | ||
tasks.push(dataWriter.ready); | ||
tasks.push(function (cb) { | ||
db.query('SELECT uuid, slug, data FROM larvitfiles_files WHERE uuid = ?', [utils.uuidToBuffer(that.uuid)], function (err, rows) { | ||
if (err) { cb(err); return; } | ||
db.query('SELECT uuid, slug FROM larvitfiles_files WHERE uuid = ?', [utils.uuidToBuffer(that.uuid)], function (err, rows) { | ||
if (err) return cb(err); | ||
@@ -105,4 +133,3 @@ if (rows.length === 0) { | ||
log.info('larvitfiles: File() - ' + err.message); | ||
cb(err); | ||
return; | ||
return cb(err); | ||
} | ||
@@ -112,3 +139,2 @@ | ||
that.slug = rows[0].slug; | ||
that.data = rows[0].data; | ||
cb(); | ||
@@ -126,3 +152,3 @@ }); | ||
db.query('SELECT name, value FROM larvitfiles_files_metadata WHERE fileUuid = ?', [utils.uuidToBuffer(that.uuid)], function (err, rows) { | ||
if (err) { cb(err); return; } | ||
if (err) return cb(err); | ||
@@ -133,3 +159,3 @@ for (let i = 0; rows[i] !== undefined; i ++) { | ||
if (that.metadata[row.name] === undefined) { | ||
that.metadata[row.name] = []; | ||
that.metadata[row.name] = []; | ||
} | ||
@@ -144,2 +170,10 @@ | ||
tasks.push(function (cb) { | ||
fs.readFile(exports.storagePath + '/' + that.uuid, function (err, data) { | ||
if (err) return cb(err); | ||
that.data = data; | ||
cb(); | ||
}); | ||
}); | ||
async.series(tasks, cb); | ||
@@ -149,21 +183,40 @@ }; | ||
File.prototype.rm = function rm(cb) { | ||
const tasks = [], | ||
const tasks = [], | ||
that = this; | ||
if (that.uuid === undefined) { | ||
const err = new Error('No uuid set, can not remove file'); | ||
log.warn('larvitfiles: File() - rm () - ' + err.message); | ||
cb(err); | ||
return; | ||
if ( ! that.uuid) { | ||
const e = new Error('uuid is not defined'); | ||
log.info(logPrefix + 'rm() - ' + e.message); | ||
return cb(e); | ||
} | ||
if (exports.storagePath === null) { | ||
const e = new Error('storagePath not set'); | ||
log.info(logPrefix + 'rm() - ' + e.message); | ||
return cb(e); | ||
} | ||
tasks.push(dataWriter.ready); | ||
tasks.push(function (cb) { | ||
db.query('DELETE FROM larvitfiles_files_metadata WHERE fileUuid = ?', [utils.uuidToBuffer(that.uuid)], cb); | ||
const options = {'exchange': dataWriter.exchangeName}, | ||
message = {}; | ||
message.action = 'rm'; | ||
message.params = {}; | ||
message.params.data = {'uuid': that.uuid}; | ||
utils.instances.intercom.send(message, options, function (err, msgUuid) { | ||
if (err) return cb(err); | ||
dataWriter.emitter.once(msgUuid, cb); | ||
}); | ||
}); | ||
tasks.push(function (cb) { | ||
db.query('DELETE FROM larvitfiles_files WHERE uuid = ?', [utils.uuidToBuffer(that.uuid)], cb); | ||
fs.unlink(exports.storagePath + '/' + that.uuid, cb); | ||
}); | ||
tasks.push(function (cb) { | ||
async.series(tasks, function (err) { | ||
if (err) return cb(err); | ||
delete that.uuid; | ||
@@ -173,6 +226,5 @@ delete that.slug; | ||
that.metadata = {}; | ||
cb(); | ||
}); | ||
async.series(tasks, cb); | ||
}; | ||
@@ -184,24 +236,25 @@ | ||
if (that.slug === undefined) { | ||
const err = new Error('Slug must be set to save to database'); | ||
log.warn('larvitfiles: File() - save() - ' + err.message); | ||
cb(err); | ||
return; | ||
if (exports.storagePath === null) { | ||
const e = new Error('storagePath not set'); | ||
log.info(logPrefix + 'save() - ' + e.message); | ||
return cb(e); | ||
} | ||
tasks.push(dataWriter.ready); | ||
tasks.push(function (cb) { | ||
getFileUuidBySlug(that.slug, function (err, result) { | ||
if (err) { cb(err); return; } | ||
const options = {'exchange': dataWriter.exchangeName}, | ||
message = {}; | ||
if ( | ||
(result !== false && result !== that.uuid) | ||
|| (result !== false && that.uuid === undefined) | ||
) { | ||
const err = new Error('Slug "' + that.slug + '" is take by another file'); | ||
log.warn('larvitfiles: File() - save() - ' + err.message); | ||
cb(err); | ||
return; | ||
} | ||
message.action = 'save'; | ||
message.params = {}; | ||
message.params.data = { | ||
'uuid': that.uuid, | ||
'slug': that.slug, | ||
'metadata': that.metadata | ||
}; | ||
cb(); | ||
utils.instances.intercom.send(message, options, function (err, msgUuid) { | ||
if (err) return cb(err); | ||
dataWriter.emitter.once(msgUuid, cb); | ||
}); | ||
@@ -211,48 +264,6 @@ }); | ||
tasks.push(function (cb) { | ||
if (that.uuid === undefined) { | ||
that.uuid = uuidLib.v4(); | ||
} | ||
cb(); | ||
fs.writeFile(exports.storagePath + '/' + that.uuid, that.data, cb); | ||
}); | ||
tasks.push(function (cb) { | ||
const dbFields = [utils.uuidToBuffer(that.uuid), that.slug, that.data, that.slug, that.data], | ||
sql = 'INSERT INTO larvitfiles_files VALUES(?,?,?) ON DUPLICATE KEY UPDATE slug = ?, data = ?;'; | ||
db.query(sql, dbFields, cb); | ||
}); | ||
tasks.push(function (cb) { | ||
db.query('DELETE FROM larvitfiles_files_metadata WHERE fileUuid = ?;', [utils.uuidToBuffer(that.uuid)], cb); | ||
}); | ||
tasks.push(function (cb) { | ||
const dbFields = []; | ||
let sql = 'INSERT INTO larvitfiles_files_metadata VALUES'; | ||
for (const name of Object.keys(that.metadata)) { | ||
if ( ! (that.metadata[name] instanceof Array)) { | ||
that.metadata[name] = [that.metadata[name]]; | ||
} | ||
for (let i = 0; that.metadata[name][i] !== undefined; i ++) { | ||
sql += '(?,?,?),'; | ||
dbFields.push(utils.uuidToBuffer(that.uuid)); | ||
dbFields.push(name); | ||
dbFields.push(that.metadata[name][i]); | ||
} | ||
} | ||
if (dbFields.length === 0) { | ||
cb(); | ||
return; | ||
} | ||
sql = sql.substring(0, sql.length - 1) + ';'; | ||
db.query(sql, dbFields, cb); | ||
}); | ||
tasks.push(function (cb) { | ||
that.loadFromDb(cb); | ||
@@ -266,3 +277,3 @@ }); | ||
this.filter = { | ||
'metadata': {} | ||
'metadata': {} | ||
}; | ||
@@ -276,3 +287,3 @@ } | ||
tasks.push(ready); | ||
tasks.push(dataWriter.ready); | ||
@@ -312,3 +323,3 @@ tasks.push(function (cb) { | ||
db.query(sql, dbFields, function (err, rows) { | ||
if (err) { cb(err); return; } | ||
if (err) return cb(err); | ||
@@ -362,3 +373,3 @@ for (let i = 0; rows[i] !== undefined; i ++) { | ||
db.query(sql, dbFields, function (err, rows) { | ||
if (err) { cb(err); return; } | ||
if (err) return cb(err); | ||
@@ -381,3 +392,3 @@ for (let i = 0; rows[i] !== undefined; i ++) { | ||
async.series(tasks, function (err) { | ||
if (err) { cb(err); return; } | ||
if (err) return cb(err); | ||
@@ -388,35 +399,2 @@ cb(null, dbFiles); | ||
// Checks if database is done migrating | ||
function ready(cb) { | ||
const options = {}; | ||
let dbMigration; | ||
if (readyRunning === true) { | ||
setTimeout(function () { | ||
ready(cb); | ||
}, 10); | ||
} | ||
if (dbReady === true) return cb(); | ||
readyRunning = true; | ||
options.dbType = 'larvitdb'; | ||
options.dbDriver = db; | ||
options.tableName = 'larvitfiles_db_version'; | ||
options.migrationScriptsPath = __dirname + '/dbmigration'; | ||
dbMigration = new DbMigration(options); | ||
dbMigration.run(function (err) { | ||
if ( ! err) { | ||
dbReady = true; | ||
} | ||
readyRunning = false; | ||
cb(err); | ||
}); | ||
} | ||
/** | ||
@@ -429,21 +407,30 @@ * Get file Uuid by slug | ||
function getFileUuidBySlug(slug, cb) { | ||
ready(function (err) { | ||
if (err) { cb(err); return; } | ||
const tasks = []; | ||
let result = false; | ||
tasks.push(dataWriter.ready); | ||
tasks.push(function (cb) { | ||
db.query('SELECT uuid FROM larvitfiles_files WHERE slug = ?', [slug], function (err, rows) { | ||
if (err) { cb(err); return; } | ||
if (err) return cb(err); | ||
if (rows.length === 0) { | ||
cb(null, false); | ||
return; | ||
return cb(null, false); | ||
} | ||
cb(null, utils.formatUuid(rows[0].uuid)); | ||
result = utils.formatUuid(rows[0].uuid); | ||
cb(); | ||
}); | ||
}); | ||
} | ||
async.series(tasks, function (err) { | ||
cb(err, result); | ||
}); | ||
}; | ||
exports.dataWriter = dataWriter; | ||
exports.File = File; | ||
exports.Files = Files; | ||
exports.getFileUuidBySlug = getFileUuidBySlug; | ||
exports.ready = ready; | ||
exports.getFileUuidBySlug = getFileUuidBySlug; |
{ | ||
"name": "larvitfiles", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "Storage of files with an API and database to use in web environments", | ||
@@ -27,3 +27,5 @@ "main": "index.js", | ||
"larvitfs": "^1.0.1", | ||
"larvitutils": "^1.0.2", | ||
"larvitutils": "^1.1.1", | ||
"larvitamsync": "^0.3.2", | ||
"larvitamintercom": "^0.2.5", | ||
"uuid": "^3.0.0", | ||
@@ -30,0 +32,0 @@ "winston": "^2.2.0" |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
12
34324
9
808
2
+ Addedlarvitamintercom@^0.2.5
+ Addedlarvitamsync@^0.3.2
+ Addedasync@3.1.1(transitive)
+ Addedbramqp@0.6.1(transitive)
+ Addedbuffer-more-ints@1.0.0(transitive)
+ Addedlarvitamintercom@0.2.14(transitive)
+ Addedlarvitamsync@0.3.2(transitive)
+ Addedpkginfo@0.4.1(transitive)
+ Addedsax@1.4.1(transitive)
+ Addedxml2js@0.4.23(transitive)
+ Addedxmlbuilder@11.0.1(transitive)
Updatedlarvitutils@^1.1.1