Utility methods
This commit is contained in:
parent
a1188fb90c
commit
ad44495469
|
@ -428,34 +428,47 @@ 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(
|
||||||
return cb(Errors.DoesNotExist('No matches'));
|
null,
|
||||||
}
|
(fileIdRows || []).map(r => r.file_id)
|
||||||
|
);
|
||||||
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);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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'));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fileIds.length > 1) {
|
||||||
|
return cb(Errors.Invalid('SHA is ambiguous'));
|
||||||
|
}
|
||||||
|
|
||||||
|
const fileEntry = new FileEntry();
|
||||||
|
return fileEntry.load(fileIds[0], err => {
|
||||||
|
return cb(err, fileEntry);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Attempt to fine a file by an *existing* full path.
|
// Attempt to fine a file by an *existing* full path.
|
||||||
// Checkums may have changed and are not validated here.
|
// Checkums may have changed and are not validated here.
|
||||||
static findByFullPath(fullPath, cb) {
|
static findByFullPath(fullPath, cb) {
|
||||||
|
|
Loading…
Reference in New Issue