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;
|
2017-02-21 05:31:01 +00:00
|
|
|
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');
|
2017-02-21 05:31:01 +00:00
|
|
|
const fse = require('fs-extra');
|
2017-09-26 16:41:41 +00:00
|
|
|
const { unlink, readFile } = require('graceful-fs');
|
|
|
|
const crypto = require('crypto');
|
2018-01-12 04:17:26 +00:00
|
|
|
const moment = require('moment');
|
2016-09-29 03:54:25 +00:00
|
|
|
|
|
|
|
const FILE_TABLE_MEMBERS = [
|
2017-01-22 05:09:29 +00:00
|
|
|
'file_id', 'area_tag', 'file_sha256', 'file_name', 'storage_tag',
|
2018-01-12 04:17:26 +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,
|
2017-02-12 07:22:53 +00:00
|
|
|
upload_by_user_id : (u) => parseInt(u) || 0,
|
2016-09-29 04:26:06 +00:00
|
|
|
file_md5 : null,
|
2017-01-22 05:09:29 +00:00
|
|
|
file_sha1 : null,
|
2016-09-29 04:26:06 +00:00
|
|
|
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,
|
2016-10-13 04:07:22 +00:00
|
|
|
archive_type : null,
|
2017-03-09 05:37:02 +00:00
|
|
|
short_file_name : null, // e.g. DOS 8.3 filename, avail in some scenarios such as TIC import
|
|
|
|
tic_origin : null, // TIC "Origin"
|
|
|
|
tic_desc : null, // TIC "Desc"
|
|
|
|
tic_ldesc : null, // TIC "Ldesc" joined by '\n'
|
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
|
|
|
|
dl_count : 0,
|
|
|
|
};
|
2017-02-08 03:20:10 +00:00
|
|
|
|
2016-10-13 04:07:22 +00:00
|
|
|
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;
|
2017-03-09 05:37:02 +00:00
|
|
|
this.fileSha256 = options.fileSha256;
|
2016-09-29 03:54:25 +00:00
|
|
|
}
|
|
|
|
|
2017-02-08 03:20:10 +00:00
|
|
|
static loadBasicEntry(fileId, dest, cb) {
|
2017-03-09 05:37:02 +00:00
|
|
|
dest = dest || {};
|
2017-02-08 03:20:10 +00:00
|
|
|
|
|
|
|
fileDb.get(
|
|
|
|
`SELECT ${FILE_TABLE_MEMBERS.join(', ')}
|
|
|
|
FROM file
|
|
|
|
WHERE file_id=?
|
|
|
|
LIMIT 1;`,
|
|
|
|
[ fileId ],
|
|
|
|
(err, file) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!file) {
|
|
|
|
return cb(Errors.DoesNotExist('No file is available by that ID'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// assign props from |file|
|
|
|
|
FILE_TABLE_MEMBERS.forEach(prop => {
|
|
|
|
dest[_.camelCase(prop)] = file[prop];
|
|
|
|
});
|
|
|
|
|
2017-03-09 05:37:02 +00:00
|
|
|
return cb(null, dest);
|
2017-02-08 03:20:10 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-09-29 03:54:25 +00:00
|
|
|
load(fileId, cb) {
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function loadBasicEntry(callback) {
|
2017-02-08 03:20:10 +00:00
|
|
|
FileEntry.loadBasicEntry(fileId, self, callback);
|
2016-09-29 03:54:25 +00:00
|
|
|
},
|
|
|
|
function loadMeta(callback) {
|
|
|
|
return self.loadMeta(callback);
|
|
|
|
},
|
|
|
|
function loadHashTags(callback) {
|
|
|
|
return self.loadHashTags(callback);
|
2017-02-08 03:20:10 +00:00
|
|
|
},
|
|
|
|
function loadUserRating(callback) {
|
|
|
|
return self.loadRating(callback);
|
2016-09-29 03:54:25 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-03-09 05:37:02 +00:00
|
|
|
persist(isUpdate, cb) {
|
|
|
|
if(!cb && _.isFunction(isUpdate)) {
|
|
|
|
cb = isUpdate;
|
|
|
|
isUpdate = false;
|
|
|
|
}
|
|
|
|
|
2016-10-07 03:03:04 +00:00
|
|
|
const self = this;
|
|
|
|
|
2017-09-16 23:13:11 +00:00
|
|
|
async.waterfall(
|
2016-10-07 03:03:04 +00:00
|
|
|
[
|
2017-03-09 05:37:02 +00:00
|
|
|
function check(callback) {
|
|
|
|
if(isUpdate && !self.fileId) {
|
|
|
|
return callback(Errors.Invalid('Cannot update file entry without an existing "fileId" member'));
|
|
|
|
}
|
|
|
|
return callback(null);
|
|
|
|
},
|
2017-09-26 16:41:41 +00:00
|
|
|
function calcSha256IfNeeded(callback) {
|
|
|
|
if(self.fileSha256) {
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isUpdate) {
|
|
|
|
return callback(Errors.MissingParam('fileSha256 property must be set for updates!'));
|
|
|
|
}
|
|
|
|
|
|
|
|
readFile(self.filePath, (err, data) => {
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const sha256 = crypto.createHash('sha256');
|
|
|
|
sha256.update(data);
|
|
|
|
self.fileSha256 = sha256.digest('hex');
|
|
|
|
return callback(null);
|
|
|
|
});
|
|
|
|
},
|
2016-10-07 03:03:04 +00:00
|
|
|
function startTrans(callback) {
|
2017-09-16 23:13:11 +00:00
|
|
|
return fileDb.beginTransaction(callback);
|
2016-10-07 03:03:04 +00:00
|
|
|
},
|
2017-09-16 23:13:11 +00:00
|
|
|
function storeEntry(trans, callback) {
|
2017-03-09 05:37:02 +00:00
|
|
|
if(isUpdate) {
|
2017-09-16 23:13:11 +00:00
|
|
|
trans.run(
|
2017-03-09 05:37:02 +00:00
|
|
|
`REPLACE INTO file (file_id, area_tag, file_sha256, file_name, storage_tag, desc, desc_long, upload_timestamp)
|
|
|
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?);`,
|
|
|
|
[ self.fileId, self.areaTag, self.fileSha256, self.fileName, self.storageTag, self.desc, self.descLong, getISOTimestampString() ],
|
|
|
|
err => {
|
2017-09-16 23:13:11 +00:00
|
|
|
return callback(err, trans);
|
2016-10-07 03:03:04 +00:00
|
|
|
}
|
2017-03-09 05:37:02 +00:00
|
|
|
);
|
|
|
|
} else {
|
2017-09-16 23:13:11 +00:00
|
|
|
trans.run(
|
2017-03-09 05:37:02 +00:00
|
|
|
`REPLACE INTO file (area_tag, file_sha256, file_name, storage_tag, desc, desc_long, upload_timestamp)
|
|
|
|
VALUES(?, ?, ?, ?, ?, ?, ?);`,
|
|
|
|
[ self.areaTag, self.fileSha256, self.fileName, self.storageTag, self.desc, self.descLong, getISOTimestampString() ],
|
|
|
|
function inserted(err) { // use non-arrow func for 'this' scope / lastID
|
|
|
|
if(!err) {
|
|
|
|
self.fileId = this.lastID;
|
|
|
|
}
|
2017-09-16 23:13:11 +00:00
|
|
|
return callback(err, trans);
|
2017-03-09 05:37:02 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2016-10-07 03:03:04 +00:00
|
|
|
},
|
2017-09-16 23:13:11 +00:00
|
|
|
function storeMeta(trans, callback) {
|
2016-10-07 03:03:04 +00:00
|
|
|
async.each(Object.keys(self.meta), (n, next) => {
|
|
|
|
const v = self.meta[n];
|
2017-09-16 23:13:11 +00:00
|
|
|
return FileEntry.persistMetaValue(self.fileId, n, v, trans, next);
|
2016-10-07 03:03:04 +00:00
|
|
|
},
|
|
|
|
err => {
|
2017-09-16 23:13:11 +00:00
|
|
|
return callback(err, trans);
|
2016-10-07 03:03:04 +00:00
|
|
|
});
|
|
|
|
},
|
2017-09-16 23:13:11 +00:00
|
|
|
function storeHashTags(trans, callback) {
|
2017-01-19 05:23:53 +00:00
|
|
|
const hashTagsArray = Array.from(self.hashTags);
|
|
|
|
async.each(hashTagsArray, (hashTag, next) => {
|
2017-09-16 23:13:11 +00:00
|
|
|
return FileEntry.persistHashTag(self.fileId, hashTag, trans, next);
|
2017-01-19 05:23:53 +00:00
|
|
|
},
|
|
|
|
err => {
|
2017-09-16 23:13:11 +00:00
|
|
|
return callback(err, trans);
|
2017-01-19 05:23:53 +00:00
|
|
|
});
|
2016-10-07 03:03:04 +00:00
|
|
|
}
|
|
|
|
],
|
2017-09-16 23:13:11 +00:00
|
|
|
(err, trans) => {
|
2016-10-07 03:03:04 +00:00
|
|
|
// :TODO: Log orig err
|
2017-09-16 23:13:11 +00:00
|
|
|
if(trans) {
|
2017-09-26 16:41:41 +00:00
|
|
|
trans[err ? 'rollback' : 'commit'](transErr => {
|
|
|
|
return cb(transErr ? transErr : err);
|
2017-03-09 05:37:02 +00:00
|
|
|
});
|
|
|
|
} else {
|
2016-10-07 03:03:04 +00:00
|
|
|
return cb(err);
|
2017-03-09 05:37:02 +00:00
|
|
|
}
|
2016-10-07 03:03:04 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-02-08 03:20:10 +00:00
|
|
|
static persistUserRating(fileId, userId, rating, cb) {
|
|
|
|
return fileDb.run(
|
|
|
|
`REPLACE INTO file_user_rating (file_id, user_id, rating)
|
|
|
|
VALUES (?, ?, ?);`,
|
|
|
|
[ fileId, userId, rating ],
|
|
|
|
cb
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-09-16 23:13:11 +00:00
|
|
|
static persistMetaValue(fileId, name, value, transOrDb, cb) {
|
|
|
|
if(!_.isFunction(cb) && _.isFunction(transOrDb)) {
|
|
|
|
cb = transOrDb;
|
|
|
|
transOrDb = fileDb;
|
|
|
|
}
|
|
|
|
|
|
|
|
return transOrDb.run(
|
2016-10-07 03:03:04 +00:00
|
|
|
`REPLACE INTO file_meta (file_id, meta_name, meta_value)
|
2017-02-08 03:20:10 +00:00
|
|
|
VALUES (?, ?, ?);`,
|
2016-10-07 03:03:04 +00:00
|
|
|
[ 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);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-09-16 23:13:11 +00:00
|
|
|
static persistHashTag(fileId, hashTag, transOrDb, cb) {
|
|
|
|
if(!_.isFunction(cb) && _.isFunction(transOrDb)) {
|
|
|
|
cb = transOrDb;
|
|
|
|
transOrDb = fileDb;
|
|
|
|
}
|
|
|
|
|
|
|
|
transOrDb.serialize( () => {
|
2017-09-29 03:34:46 +00:00
|
|
|
transOrDb.run(
|
2017-01-19 05:23:53 +00:00
|
|
|
`INSERT OR IGNORE INTO hash_tag (hash_tag)
|
|
|
|
VALUES (?);`,
|
|
|
|
[ hashTag ]
|
|
|
|
);
|
|
|
|
|
2017-09-16 23:13:11 +00:00
|
|
|
transOrDb.run(
|
2017-01-19 05:23:53 +00:00
|
|
|
`REPLACE INTO file_hash_tag (hash_tag_id, file_id)
|
|
|
|
VALUES (
|
|
|
|
(SELECT hash_tag_id
|
|
|
|
FROM hash_tag
|
|
|
|
WHERE hash_tag = ?),
|
|
|
|
?
|
|
|
|
);`,
|
|
|
|
[ hashTag, fileId ],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-29 03:54:25 +00:00
|
|
|
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-02-08 03:20:10 +00:00
|
|
|
loadRating(cb) {
|
|
|
|
fileDb.get(
|
|
|
|
`SELECT AVG(fur.rating) AS avg_rating
|
|
|
|
FROM file_user_rating fur
|
|
|
|
INNER JOIN file f
|
|
|
|
ON f.file_id = fur.file_id
|
|
|
|
AND f.file_id = ?`,
|
|
|
|
[ this.fileId ],
|
|
|
|
(err, result) => {
|
|
|
|
if(result) {
|
|
|
|
this.userRating = result.avg_rating;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-25 06:39:31 +00:00
|
|
|
static get WellKnownMetaValues() {
|
|
|
|
return Object.keys(FILE_WELL_KNOWN_META);
|
|
|
|
}
|
2016-09-29 04:26:06 +00:00
|
|
|
|
2017-02-19 02:00:09 +00:00
|
|
|
static findFileBySha(sha, cb) {
|
|
|
|
// full or partial SHA-256
|
|
|
|
fileDb.all(
|
|
|
|
`SELECT file_id
|
|
|
|
FROM file
|
|
|
|
WHERE file_sha256 LIKE "${sha}%"
|
|
|
|
LIMIT 2;`, // limit 2 such that we can find if there are dupes
|
|
|
|
(err, fileIdRows) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!fileIdRows || 0 === fileIdRows.length) {
|
|
|
|
return cb(Errors.DoesNotExist('No matches'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(fileIdRows.length > 1) {
|
|
|
|
return cb(Errors.Invalid('SHA is ambiguous'));
|
|
|
|
}
|
|
|
|
|
|
|
|
const fileEntry = new FileEntry();
|
|
|
|
return fileEntry.load(fileIdRows[0].file_id, err => {
|
|
|
|
return cb(err, fileEntry);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-05-24 03:55:22 +00:00
|
|
|
static findByFileNameWildcard(wc, cb) {
|
|
|
|
// convert any * -> % and ? -> _ for SQLite syntax - see https://www.sqlite.org/lang_expr.html
|
|
|
|
wc = wc.replace(/\*/g, '%').replace(/\?/g, '_');
|
|
|
|
|
|
|
|
fileDb.all(
|
|
|
|
`SELECT file_id
|
|
|
|
FROM file
|
|
|
|
WHERE file_name LIKE "${wc}"
|
|
|
|
`,
|
|
|
|
(err, fileIdRows) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!fileIdRows || 0 === fileIdRows.length) {
|
|
|
|
return cb(Errors.DoesNotExist('No matches'));
|
|
|
|
}
|
|
|
|
|
|
|
|
const entries = [];
|
|
|
|
async.each(fileIdRows, (row, nextRow) => {
|
|
|
|
const fileEntry = new FileEntry();
|
|
|
|
fileEntry.load(row.file_id, err => {
|
|
|
|
if(!err) {
|
|
|
|
entries.push(fileEntry);
|
|
|
|
}
|
|
|
|
return nextRow(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return cb(err, entries);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-12-07 01:58:56 +00:00
|
|
|
static findFiles(filter, cb) {
|
|
|
|
filter = filter || {};
|
|
|
|
|
2017-01-19 05:23:53 +00:00
|
|
|
let sql;
|
2016-09-29 03:54:25 +00:00
|
|
|
let sqlWhere = '';
|
2017-01-19 05:23:53 +00:00
|
|
|
let sqlOrderBy;
|
|
|
|
const sqlOrderDir = 'ascending' === filter.order ? 'ASC' : 'DESC';
|
2018-01-12 04:17:26 +00:00
|
|
|
|
|
|
|
if(moment.isMoment(filter.newerThanTimestamp)) {
|
|
|
|
filter.newerThanTimestamp = getISOTimestampString(filter.newerThanTimestamp);
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:23:53 +00:00
|
|
|
function getOrderByWithCast(ob) {
|
2017-02-08 03:20:10 +00:00
|
|
|
if( [ 'dl_count', 'est_release_year', 'byte_size' ].indexOf(filter.sort) > -1 ) {
|
2017-01-19 05:23:53 +00:00
|
|
|
return `ORDER BY CAST(${ob} AS INTEGER)`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return `ORDER BY ${ob}`;
|
|
|
|
}
|
2016-09-29 03:54:25 +00:00
|
|
|
|
|
|
|
function appendWhereClause(clause) {
|
|
|
|
if(sqlWhere) {
|
|
|
|
sqlWhere += ' AND ';
|
|
|
|
} else {
|
|
|
|
sqlWhere += ' WHERE ';
|
|
|
|
}
|
|
|
|
sqlWhere += clause;
|
|
|
|
}
|
|
|
|
|
2017-01-23 04:30:49 +00:00
|
|
|
if(filter.sort && filter.sort.length > 0) {
|
2017-01-19 05:23:53 +00:00
|
|
|
if(Object.keys(FILE_WELL_KNOWN_META).indexOf(filter.sort) > -1) { // sorting via a meta value?
|
2018-01-12 04:17:26 +00:00
|
|
|
sql =
|
2017-03-09 05:37:02 +00:00
|
|
|
`SELECT DISTINCT f.file_id
|
2017-01-19 05:23:53 +00:00
|
|
|
FROM file f, file_meta m`;
|
|
|
|
|
2017-03-09 05:37:02 +00:00
|
|
|
appendWhereClause(`f.file_id = m.file_id AND m.meta_name = "${filter.sort}"`);
|
2017-01-19 05:23:53 +00:00
|
|
|
|
|
|
|
sqlOrderBy = `${getOrderByWithCast('m.meta_value')} ${sqlOrderDir}`;
|
|
|
|
} else {
|
2017-02-08 03:20:10 +00:00
|
|
|
// additional special treatment for user ratings: we need to average them
|
|
|
|
if('user_rating' === filter.sort) {
|
|
|
|
sql =
|
2017-03-09 05:37:02 +00:00
|
|
|
`SELECT DISTINCT f.file_id,
|
2017-02-08 03:20:10 +00:00
|
|
|
(SELECT IFNULL(AVG(rating), 0) rating
|
|
|
|
FROM file_user_rating
|
|
|
|
WHERE file_id = f.file_id)
|
|
|
|
AS avg_rating
|
2017-05-20 00:41:13 +00:00
|
|
|
FROM file f`;
|
2018-01-12 04:17:26 +00:00
|
|
|
|
2017-02-08 03:20:10 +00:00
|
|
|
sqlOrderBy = `ORDER BY avg_rating ${sqlOrderDir}`;
|
|
|
|
} else {
|
|
|
|
sql =
|
2017-03-09 05:37:02 +00:00
|
|
|
`SELECT DISTINCT f.file_id, f.${filter.sort}
|
2017-05-20 00:41:13 +00:00
|
|
|
FROM file f`;
|
2017-02-08 03:20:10 +00:00
|
|
|
|
|
|
|
sqlOrderBy = getOrderByWithCast(`f.${filter.sort}`) + ' ' + sqlOrderDir;
|
|
|
|
}
|
2017-01-19 05:23:53 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-01-12 04:17:26 +00:00
|
|
|
sql =
|
2017-03-09 05:37:02 +00:00
|
|
|
`SELECT DISTINCT f.file_id
|
2017-05-20 00:41:13 +00:00
|
|
|
FROM file f`;
|
2017-01-19 05:23:53 +00:00
|
|
|
|
|
|
|
sqlOrderBy = `${getOrderByWithCast('f.file_id')} ${sqlOrderDir}`;
|
|
|
|
}
|
|
|
|
|
2017-01-23 04:30:49 +00:00
|
|
|
if(filter.areaTag && filter.areaTag.length > 0) {
|
2017-09-26 16:41:41 +00:00
|
|
|
if(Array.isArray(filter.areaTag)) {
|
|
|
|
const areaList = filter.areaTag.map(t => `"${t}"`).join(', ');
|
|
|
|
appendWhereClause(`f.area_tag IN(${areaList})`);
|
|
|
|
} else {
|
|
|
|
appendWhereClause(`f.area_tag = "${filter.areaTag}"`);
|
|
|
|
}
|
2017-03-09 05:37:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(filter.metaPairs && filter.metaPairs.length > 0) {
|
|
|
|
|
|
|
|
filter.metaPairs.forEach(mp => {
|
|
|
|
if(mp.wcValue) {
|
|
|
|
// convert any * -> % and ? -> _ for SQLite syntax - see https://www.sqlite.org/lang_expr.html
|
|
|
|
mp.value = mp.value.replace(/\*/g, '%').replace(/\?/g, '_');
|
|
|
|
appendWhereClause(
|
|
|
|
`f.file_id IN (
|
|
|
|
SELECT file_id
|
|
|
|
FROM file_meta
|
|
|
|
WHERE meta_name = "${mp.name}" AND meta_value LIKE "${mp.value}"
|
|
|
|
)`
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
appendWhereClause(
|
|
|
|
`f.file_id IN (
|
|
|
|
SELECT file_id
|
|
|
|
FROM file_meta
|
|
|
|
WHERE meta_name = "${mp.name}" AND meta_value = "${mp.value}"
|
|
|
|
)`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2016-09-29 03:54:25 +00:00
|
|
|
}
|
|
|
|
|
2017-02-21 05:31:01 +00:00
|
|
|
if(filter.storageTag && filter.storageTag.length > 0) {
|
|
|
|
appendWhereClause(`f.storage_tag="${filter.storageTag}"`);
|
|
|
|
}
|
|
|
|
|
2017-01-23 04:30:49 +00:00
|
|
|
if(filter.terms && filter.terms.length > 0) {
|
2016-09-29 03:54:25 +00:00
|
|
|
appendWhereClause(
|
2017-01-19 05:23:53 +00:00
|
|
|
`f.file_id IN (
|
2016-09-29 03:54:25 +00:00
|
|
|
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
|
|
|
)`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-23 04:30:49 +00:00
|
|
|
if(filter.tags && filter.tags.length > 0) {
|
2017-12-13 04:32:01 +00:00
|
|
|
// build list of quoted tags; filter.tags comes in as a space and/or comma separated values
|
|
|
|
const tags = filter.tags.replace(/,/g, ' ').replace(/\s{2,}/g, ' ').split(' ').map( tag => `"${tag}"` ).join(',');
|
2016-12-07 01:58:56 +00:00
|
|
|
|
2016-09-29 03:54:25 +00:00
|
|
|
appendWhereClause(
|
2017-01-19 05:23:53 +00:00
|
|
|
`f.file_id IN (
|
2016-09-29 03:54:25 +00:00
|
|
|
SELECT file_id
|
|
|
|
FROM file_hash_tag
|
|
|
|
WHERE hash_tag_id IN (
|
|
|
|
SELECT hash_tag_id
|
|
|
|
FROM hash_tag
|
2017-01-19 05:23:53 +00:00
|
|
|
WHERE hash_tag IN (${tags})
|
2016-09-29 03:54:25 +00:00
|
|
|
)
|
|
|
|
)`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-07-18 01:46:38 +00:00
|
|
|
if(_.isString(filter.newerThanTimestamp) && filter.newerThanTimestamp.length > 0) {
|
|
|
|
appendWhereClause(`DATETIME(f.upload_timestamp) > DATETIME("${filter.newerThanTimestamp}", "+1 seconds")`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(_.isNumber(filter.newerThanFileId)) {
|
2017-07-24 00:11:33 +00:00
|
|
|
appendWhereClause(`f.file_id > ${filter.newerThanFileId}`);
|
2017-07-18 01:46:38 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 04:17:26 +00:00
|
|
|
sql += `${sqlWhere} ${sqlOrderBy}`;
|
2016-09-29 03:54:25 +00:00
|
|
|
|
2017-09-11 02:51:43 +00:00
|
|
|
if(_.isNumber(filter.limit)) {
|
2018-01-12 04:17:26 +00:00
|
|
|
sql += ` LIMIT ${filter.limit}`;
|
2017-09-11 02:51:43 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 04:17:26 +00:00
|
|
|
sql += ';';
|
|
|
|
|
2016-09-29 03:54:25 +00:00
|
|
|
const matchingFileIds = [];
|
|
|
|
fileDb.each(sql, (err, fileId) => {
|
|
|
|
if(fileId) {
|
|
|
|
matchingFileIds.push(fileId.file_id);
|
|
|
|
}
|
|
|
|
}, err => {
|
|
|
|
return cb(err, matchingFileIds);
|
|
|
|
});
|
|
|
|
}
|
2017-02-21 05:31:01 +00:00
|
|
|
|
2017-09-24 05:03:21 +00:00
|
|
|
static removeEntry(srcFileEntry, options, cb) {
|
|
|
|
if(!_.isFunction(cb) && _.isFunction(options)) {
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function removeFromDatabase(callback) {
|
|
|
|
fileDb.run(
|
|
|
|
`DELETE FROM file
|
|
|
|
WHERE file_id = ?;`,
|
|
|
|
[ srcFileEntry.fileId ],
|
|
|
|
err => {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function optionallyRemovePhysicalFile(callback) {
|
|
|
|
if(true !== options.removePhysFile) {
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
unlink(srcFileEntry.filePath, err => {
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-02-21 05:31:01 +00:00
|
|
|
static moveEntry(srcFileEntry, destAreaTag, destStorageTag, destFileName, cb) {
|
|
|
|
if(!cb && _.isFunction(destFileName)) {
|
|
|
|
cb = destFileName;
|
|
|
|
destFileName = srcFileEntry.fileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
const srcPath = srcFileEntry.filePath;
|
|
|
|
const dstDir = FileEntry.getAreaStorageDirectoryByTag(destStorageTag);
|
|
|
|
|
|
|
|
if(!dstDir) {
|
|
|
|
return cb(Errors.Invalid('Invalid storage tag'));
|
|
|
|
}
|
|
|
|
|
|
|
|
const dstPath = paths.join(dstDir, destFileName);
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function movePhysFile(callback) {
|
|
|
|
if(srcPath === dstPath) {
|
|
|
|
return callback(null); // don't need to move file, but may change areas
|
|
|
|
}
|
|
|
|
|
|
|
|
fse.move(srcPath, dstPath, err => {
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function updateDatabase(callback) {
|
|
|
|
fileDb.run(
|
|
|
|
`UPDATE file
|
|
|
|
SET area_tag = ?, file_name = ?, storage_tag = ?
|
|
|
|
WHERE file_id = ?;`,
|
|
|
|
[ destAreaTag, destFileName, destStorageTag, srcFileEntry.fileId ],
|
|
|
|
err => {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2016-09-29 03:54:25 +00:00
|
|
|
};
|