Utility methods
This commit is contained in:
parent
a1188fb90c
commit
ad44495469
|
@ -428,32 +428,45 @@ module.exports = class FileEntry {
|
||||||
return Object.keys(FILE_WELL_KNOWN_META);
|
return Object.keys(FILE_WELL_KNOWN_META);
|
||||||
}
|
}
|
||||||
|
|
||||||
static findBySha(sha, cb) {
|
static getFileIdsBySha(sha, options = {}, cb) {
|
||||||
// full or partial SHA-256
|
// full or partial SHA-256
|
||||||
|
const limit = _.isNumber(options.limit) ? `LIMIT ${options.limit}` : '';
|
||||||
fileDb.all(
|
fileDb.all(
|
||||||
`SELECT file_id
|
`SELECT file_id
|
||||||
FROM file
|
FROM file
|
||||||
WHERE file_sha256 LIKE "${sha}%"
|
WHERE file_sha256 LIKE "${sha}%" ${limit};`,
|
||||||
LIMIT 2;`, // limit 2 such that we can find if there are dupes
|
|
||||||
(err, fileIdRows) => {
|
(err, fileIdRows) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return cb(err);
|
return cb(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!fileIdRows || 0 === fileIdRows.length) {
|
return cb(
|
||||||
|
null,
|
||||||
|
(fileIdRows || []).map(r => r.file_id)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
static findBySha(sha, cb) {
|
||||||
|
FileEntry.getFileIdsBySha(sha, { limit: 2 }, (err, fileIds) => {
|
||||||
|
if (err) {
|
||||||
|
return cb(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!fileIds || 0 === fileIds.length) {
|
||||||
return cb(Errors.DoesNotExist('No matches'));
|
return cb(Errors.DoesNotExist('No matches'));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fileIdRows.length > 1) {
|
if (fileIds.length > 1) {
|
||||||
return cb(Errors.Invalid('SHA is ambiguous'));
|
return cb(Errors.Invalid('SHA is ambiguous'));
|
||||||
}
|
}
|
||||||
|
|
||||||
const fileEntry = new FileEntry();
|
const fileEntry = new FileEntry();
|
||||||
return fileEntry.load(fileIdRows[0].file_id, err => {
|
return fileEntry.load(fileIds[0], err => {
|
||||||
return cb(err, fileEntry);
|
return cb(err, fileEntry);
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attempt to fine a file by an *existing* full path.
|
// Attempt to fine a file by an *existing* full path.
|
||||||
|
|
Loading…
Reference in New Issue