2016-09-29 03:54:25 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2016-10-07 03:03:04 +00:00
|
|
|
const fileDb = require('./database.js').dbs.file;
|
|
|
|
const Errors = require('./enig_error.js').Errors;
|
2016-10-25 03:49:45 +00:00
|
|
|
const getISOTimestampString = require('./database.js').getISOTimestampString;
|
|
|
|
const Config = require('./config.js').config;
|
2016-09-29 03:54:25 +00:00
|
|
|
|
|
|
|
// deps
|
2016-10-07 03:03:04 +00:00
|
|
|
const async = require('async');
|
|
|
|
const _ = require('lodash');
|
2016-10-25 03:49:45 +00:00
|
|
|
const paths = require('path');
|
2016-09-29 03:54:25 +00:00
|
|
|
|
|
|
|
const FILE_TABLE_MEMBERS = [
|
2016-12-07 01:58:56 +00:00
|
|
|
'file_id', 'area_tag', 'file_sha1', 'file_name', 'storage_tag',
|
2016-10-07 03:03:04 +00:00
|
|
|
'desc', 'desc_long', 'upload_timestamp'
|
2016-09-29 03:54:25 +00:00
|
|
|
];
|
|
|
|
|
2016-09-29 04:26:06 +00:00
|
|
|
const FILE_WELL_KNOWN_META = {
|
|
|
|
// name -> *read* converter, if any
|
|
|
|
upload_by_username : null,
|
|
|
|
upload_by_user_id : null,
|
|
|
|
file_md5 : null,
|
|
|
|
file_sha256 : null,
|
|
|
|
file_crc32 : null,
|
2016-10-01 19:25:32 +00:00
|
|
|
est_release_year : (y) => parseInt(y) || new Date().getFullYear(),
|
|
|
|
dl_count : (d) => parseInt(d) || 0,
|
|
|
|
byte_size : (b) => parseInt(b) || 0,
|
|
|
|
user_rating : (r) => Math.min(parseInt(r) || 0, 5),
|
2016-10-13 04:07:22 +00:00
|
|
|
archive_type : null,
|
2016-09-29 04:26:06 +00:00
|
|
|
};
|
|
|
|
|
2016-09-29 03:54:25 +00:00
|
|
|
module.exports = class FileEntry {
|
|
|
|
constructor(options) {
|
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
this.fileId = options.fileId || 0;
|
|
|
|
this.areaTag = options.areaTag || '';
|
2016-10-13 04:07:22 +00:00
|
|
|
this.meta = options.meta || {
|
|
|
|
// values we always want
|
|
|
|
user_rating : 0,
|
|
|
|
dl_count : 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.hashTags = options.hashTags || new Set();
|
2016-10-07 03:03:04 +00:00
|
|
|
this.fileName = options.fileName;
|
2016-12-07 01:58:56 +00:00
|
|
|
this.storageTag = options.storageTag;
|
2016-09-29 03:54:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
load(fileId, cb) {
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function loadBasicEntry(callback) {
|
|
|
|
fileDb.get(
|
|
|
|
`SELECT ${FILE_TABLE_MEMBERS.join(', ')}
|
|
|
|
FROM file
|
|
|
|
WHERE file_id=?
|
|
|
|
LIMIT 1;`,
|
|
|
|
[ fileId ],
|
|
|
|
(err, file) => {
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!file) {
|
|
|
|
return callback(Errors.DoesNotExist('No file is available by that ID'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// assign props from |file|
|
|
|
|
FILE_TABLE_MEMBERS.forEach(prop => {
|
|
|
|
self[_.camelCase(prop)] = file[prop];
|
|
|
|
});
|
|
|
|
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function loadMeta(callback) {
|
|
|
|
return self.loadMeta(callback);
|
|
|
|
},
|
|
|
|
function loadHashTags(callback) {
|
|
|
|
return self.loadHashTags(callback);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-10-07 03:03:04 +00:00
|
|
|
persist(cb) {
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function startTrans(callback) {
|
|
|
|
return fileDb.run('BEGIN;', callback);
|
|
|
|
},
|
|
|
|
function storeEntry(callback) {
|
|
|
|
fileDb.run(
|
2016-12-07 01:58:56 +00:00
|
|
|
`REPLACE INTO file (area_tag, file_sha1, file_name, storage_tag, desc, desc_long, upload_timestamp)
|
|
|
|
VALUES(?, ?, ?, ?, ?, ?, ?);`,
|
|
|
|
[ self.areaTag, self.fileSha1, self.fileName, self.storageTag, self.desc, self.descLong, getISOTimestampString() ],
|
2016-10-07 03:03:04 +00:00
|
|
|
function inserted(err) { // use non-arrow func for 'this' scope / lastID
|
|
|
|
if(!err) {
|
|
|
|
self.fileId = this.lastID;
|
|
|
|
}
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function storeMeta(callback) {
|
|
|
|
async.each(Object.keys(self.meta), (n, next) => {
|
|
|
|
const v = self.meta[n];
|
|
|
|
return FileEntry.persistMetaValue(self.fileId, n, v, next);
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function storeHashTags(callback) {
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
// :TODO: Log orig err
|
|
|
|
fileDb.run(err ? 'ROLLBACK;' : 'COMMIT;', err => {
|
|
|
|
return cb(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-12-07 01:58:56 +00:00
|
|
|
static getAreaStorageDirectoryByTag(storageTag) {
|
|
|
|
const storageLocation = (storageTag && Config.fileBase.storageTags[storageTag]);
|
|
|
|
|
|
|
|
// absolute paths as-is
|
|
|
|
if(storageLocation && '/' === storageLocation.charAt(0)) {
|
|
|
|
return storageLocation;
|
2016-10-25 03:49:45 +00:00
|
|
|
}
|
2016-12-07 01:58:56 +00:00
|
|
|
|
|
|
|
// relative to |areaStoragePrefix|
|
|
|
|
return paths.join(Config.fileBase.areaStoragePrefix, storageLocation || '');
|
|
|
|
}
|
|
|
|
|
|
|
|
get filePath() {
|
|
|
|
const storageDir = FileEntry.getAreaStorageDirectoryByTag(this.storageTag);
|
|
|
|
return paths.join(storageDir, this.fileName);
|
2016-10-25 03:49:45 +00:00
|
|
|
}
|
|
|
|
|
2016-10-07 03:03:04 +00:00
|
|
|
static persistMetaValue(fileId, name, value, cb) {
|
|
|
|
fileDb.run(
|
|
|
|
`REPLACE INTO file_meta (file_id, meta_name, meta_value)
|
|
|
|
VALUES(?, ?, ?);`,
|
|
|
|
[ fileId, name, value ],
|
|
|
|
cb
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-12-31 21:50:29 +00:00
|
|
|
static incrementAndPersistMetaValue(fileId, name, incrementBy, cb) {
|
|
|
|
incrementBy = incrementBy || 1;
|
|
|
|
fileDb.run(
|
|
|
|
`UPDATE file_meta
|
|
|
|
SET meta_value = meta_value + ?
|
|
|
|
WHERE file_id = ? AND meta_name = ?;`,
|
|
|
|
[ incrementBy, fileId, name ],
|
|
|
|
err => {
|
|
|
|
if(cb) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-09-29 03:54:25 +00:00
|
|
|
loadMeta(cb) {
|
|
|
|
fileDb.each(
|
|
|
|
`SELECT meta_name, meta_value
|
|
|
|
FROM file_meta
|
|
|
|
WHERE file_id=?;`,
|
|
|
|
[ this.fileId ],
|
|
|
|
(err, meta) => {
|
|
|
|
if(meta) {
|
2016-09-29 04:26:06 +00:00
|
|
|
const conv = FILE_WELL_KNOWN_META[meta.meta_name];
|
|
|
|
this.meta[meta.meta_name] = conv ? conv(meta.meta_value) : meta.meta_value;
|
2016-09-29 03:54:25 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
loadHashTags(cb) {
|
|
|
|
fileDb.each(
|
|
|
|
`SELECT ht.hash_tag_id, ht.hash_tag
|
|
|
|
FROM hash_tag ht
|
|
|
|
WHERE ht.hash_tag_id IN (
|
|
|
|
SELECT hash_tag_id
|
|
|
|
FROM file_hash_tag
|
|
|
|
WHERE file_id=?
|
|
|
|
);`,
|
|
|
|
[ this.fileId ],
|
|
|
|
(err, hashTag) => {
|
|
|
|
if(hashTag) {
|
|
|
|
this.hashTags.add(hashTag.hash_tag);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-12 05:51:00 +00:00
|
|
|
setHashTags(hashTags) {
|
|
|
|
if(_.isString(hashTags)) {
|
|
|
|
this.hashTags = new Set(hashTags.split(/[\s,]+/));
|
|
|
|
} else if(Array.isArray(hashTags)) {
|
|
|
|
this.hashTags = new Set(hashTags);
|
|
|
|
} else if(hashTags instanceof Set) {
|
|
|
|
this.hashTags = hashTags;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-29 04:26:06 +00:00
|
|
|
static getWellKnownMetaValues() { return Object.keys(FILE_WELL_KNOWN_META); }
|
|
|
|
|
2016-12-07 01:58:56 +00:00
|
|
|
static findFiles(filter, cb) {
|
2016-09-29 03:54:25 +00:00
|
|
|
// :TODO: build search here - return [ fileid1, fileid2, ... ]
|
|
|
|
// free form
|
|
|
|
// areaTag
|
|
|
|
// tags
|
|
|
|
// order by
|
|
|
|
// sort
|
|
|
|
|
2016-12-07 01:58:56 +00:00
|
|
|
filter = filter || {};
|
|
|
|
|
2016-09-29 03:54:25 +00:00
|
|
|
let sql =
|
|
|
|
`SELECT file_id
|
|
|
|
FROM file`;
|
|
|
|
|
|
|
|
let sqlWhere = '';
|
|
|
|
|
|
|
|
function appendWhereClause(clause) {
|
|
|
|
if(sqlWhere) {
|
|
|
|
sqlWhere += ' AND ';
|
|
|
|
} else {
|
|
|
|
sqlWhere += ' WHERE ';
|
|
|
|
}
|
|
|
|
sqlWhere += clause;
|
|
|
|
}
|
|
|
|
|
2016-12-07 01:58:56 +00:00
|
|
|
if(filter.areaTag) {
|
|
|
|
appendWhereClause(`area_tag="${filter.areaTag}"`);
|
2016-09-29 03:54:25 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 01:58:56 +00:00
|
|
|
if(filter.terms) {
|
2016-09-29 03:54:25 +00:00
|
|
|
appendWhereClause(
|
|
|
|
`file_id IN (
|
|
|
|
SELECT rowid
|
|
|
|
FROM file_fts
|
2016-12-07 01:58:56 +00:00
|
|
|
WHERE file_fts MATCH "${filter.terms.replace(/"/g,'""')}"
|
2016-09-29 03:54:25 +00:00
|
|
|
)`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-12-07 01:58:56 +00:00
|
|
|
if(filter.tags) {
|
|
|
|
const tags = filter.tags.split(' '); // filter stores as sep separated values
|
|
|
|
|
2016-09-29 03:54:25 +00:00
|
|
|
appendWhereClause(
|
|
|
|
`file_id IN (
|
|
|
|
SELECT file_id
|
|
|
|
FROM file_hash_tag
|
|
|
|
WHERE hash_tag_id IN (
|
|
|
|
SELECT hash_tag_id
|
|
|
|
FROM hash_tag
|
2016-12-07 01:58:56 +00:00
|
|
|
WHERE hash_tag IN (${tags.join(',')})
|
2016-09-29 03:54:25 +00:00
|
|
|
)
|
|
|
|
)`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-12-07 01:58:56 +00:00
|
|
|
// :TODO: filter.orderBy
|
|
|
|
// :TODO: filter.sort
|
2016-09-29 03:54:25 +00:00
|
|
|
|
|
|
|
sql += sqlWhere + ';';
|
|
|
|
const matchingFileIds = [];
|
|
|
|
fileDb.each(sql, (err, fileId) => {
|
|
|
|
if(fileId) {
|
|
|
|
matchingFileIds.push(fileId.file_id);
|
|
|
|
}
|
|
|
|
}, err => {
|
|
|
|
return cb(err, matchingFileIds);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|