Skip to content

Commit

Permalink
Various bugfixes (see below)
Browse files Browse the repository at this point in the history
* Deprecated decoding paths from legacy versions and added tool to fix paths.
* Fixed null hash on corrupted media.
* Made a verbose getStatus that will show new files.
* Fixed unable to delete rated media.
  • Loading branch information
SimplyBoo committed Oct 6, 2018
1 parent 1429605 commit 0aedb04
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 15 deletions.
54 changes: 42 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ const configCheckConnector = async function (req, res, next) {
next();
};

function deleteMedia(media) {
const hash = media.hash;
fs.unlink(media.absolutePath, function() {
console.log(`${media.absolutePath} removed`);
});
fs.unlink(`${utils.config.cachePath}/thumbnails/${hash}.png`, function() {
console.log(`${utils.config.cachePath}/thumbnails/${hash}.png removed`);
});
rimraf(`${utils.config.cachePath}/${hash}/`, function () {
console.log(`${utils.config.cachePath}/${hash}/ removed`);
});
}

class Scanner {
constructor(callback) {
this.state = "IDLE";
Expand Down Expand Up @@ -121,7 +134,7 @@ class Scanner {
};
}

getStatus() {
getStatus(verbose) {
const status = {
state: this.state,
libraryPath: utils.config.libraryPath,
Expand All @@ -131,6 +144,10 @@ class Scanner {
}
if (this.scanStatus) {
status.scanStatus = utils.slimScannerStatus(this.scanStatus);
if (verbose) {
status.scanStatus.newFiles = this.scanStatus.newFiles;
status.scanStatus.missingFiles = this.scanStatus.missingFiles;
}
}
return status;
}
Expand All @@ -154,6 +171,22 @@ class Scanner {
}
}
}

async deleteCorrupted() {
const map = global.db.getDefaultMap();
for (let i = 0; i < map.length; i++) {
const media = global.db.getMedia(map[i]);
if (media.corrupted) {
console.log(`Deleting corrupted media ${media.path}`);
if (await global.db.removeMedia(media.hash)) {
deleteMedia(media);
console.log(`${media.hash} deleted.`);
} else {
console.log(`Failed to delete ${media.hash}`);
}
}
}
}
}

const scanner = new Scanner(function(status) {
Expand Down Expand Up @@ -204,7 +237,7 @@ function wrapReq(func) {
}

app.get('/api/scanner/status', configCheckConnector, function (req, res) {
res.json(scanner.getStatus());
res.json(scanner.getStatus(req.query.verbose));
});

app.get('/api/scanner/index', configCheckConnector, function (req, res) {
Expand Down Expand Up @@ -233,7 +266,7 @@ app.get('/api/scanner/import', configCheckConnector, function (req, res) {
}
(async function() {
await scanner.scan();
await scanner.index();
await scanner.index(deleteClones);
await scanner.cache();
await global.db.search.rebuildIndex();
})();
Expand All @@ -246,6 +279,11 @@ app.get('/api/scanner/deleteMissing', configCheckConnector, wrapReq(async functi
res.json(scanner.getStatus());
}));

app.get('/api/scanner/deleteCorrupted', configCheckConnector, wrapReq(async function (req, res) {
await scanner.deleteCorrupted();
res.json(scanner.getStatus());
}));

app.get('/api/tags', configCheckConnector, function (req, res) {
res.json(global.db.getTags());
});
Expand Down Expand Up @@ -318,15 +356,7 @@ app.get('/api/images/:hash/delete', configCheckConnector, wrapReq(async function
const media = global.db.getMedia(hash);
if (media) {
if (await global.db.removeMedia(hash)) {
fs.unlink(media.absolutePath, function() {
console.log(`${media.absolutePath} removed`);
});
fs.unlink(`${utils.config.cachePath}/thumbnails/${hash}.png`, function() {
console.log(`${utils.config.cachePath}/thumbnails/${hash}.png removed`);
});
rimraf(`${utils.config.cachePath}/${hash}/`, function () {
console.log(`${utils.config.cachePath}/${hash}/ removed`);
});
deleteMedia(media);
console.log(`${hash} deleted.`);
res.json({message: `${hash} deleted.`});
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/database/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class MediaManager {

addMedia(hash, path, rotation, type, hashDate) {
if (!hash) {
console.log('Skipping file. Hash not set');
console.log(`Skipping file. Hash not set: ${path}`);
return false;
}
if (!path) {
Expand All @@ -42,7 +42,7 @@ class MediaManager {
// Hash date can be null. It's not crucial.
const media = {
hash: hash,
path: decodeURIComponent((path + '').replace(/\+/g, '%20')).replace(/\\/g, '/'),
path: path,
rotation: rotation,
type: type,
tags: [],
Expand Down
4 changes: 3 additions & 1 deletion src/database/mysql/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class MySQLDatabase extends MediaManager {
IF(corrupted.hash IS NULL, FALSE, TRUE) AS corrupted,
IF(priority_transcode.hash IS NULL, FALSE, TRUE) AS priority_transcode,
IF(cached.hash IS NULL, FALSE, TRUE) AS cached,
cached.*,
cached.width, cached.height, cached.length, cached.artist, cached.album, cached.title,
ratings.rating
from images
LEFT JOIN corrupted ON (corrupted.hash = images.hash)
Expand Down Expand Up @@ -272,6 +272,8 @@ class MySQLDatabase extends MediaManager {
await this.query("DELETE FROM priority_transcode WHERE hash=?", [hash]);
await this.query("DELETE FROM corrupted WHERE hash=?", [hash]);
await this.query("DELETE FROM collection_data WHERE hash=?", [hash]);
await this.query("DELETE FROM ratings WHERE hash=?", [hash]);
await this.query("DELETE FROM media_actors WHERE hash=?", [hash]);
await this.query('DELETE FROM images WHERE hash=?', [hash]);
return true;
}
Expand Down
2 changes: 2 additions & 0 deletions src/database/sqlite3/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ class SQLiteDatabase extends MediaManager {
await this.query("DELETE FROM priority_transcode WHERE hash=?", [hash]);
await this.query("DELETE FROM corrupted WHERE hash=?", [hash]);
await this.query("DELETE FROM collection_data WHERE hash=?", [hash]);
await this.query("DELETE FROM ratings WHERE hash=?", [hash]);
await this.query("DELETE FROM media_actors WHERE hash=?", [hash]);
await this.query('DELETE FROM images WHERE hash=?', [hash]);
return true;
}
Expand Down
24 changes: 24 additions & 0 deletions utils/fix_paths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const Database = require('../src/database');
const utils = require('../src/utils.js');
const path = require('path');

(async function() {
await utils.setup();
global.db = await Database.setup(utils.config);
console.log('Decoding all paths');
const map = global.db.getDefaultMap();
// Convert from hashmap to array.
let updated = 0;
for (let i = 0; i < map.length; i++) {
const media = global.db.getMedia(map[i]);
const newPath = decodeURIComponent((media.path + '').replace(/\+/g, '%20')).replace(/\\/g, '/');
if (media.path !== newPath) {
console.log(`Updated ${media.path} to ${newPath}`);
await global.db.updateMedia(media.hash, {path: newPath});
updated++;
}
}
console.log(`Updated ${updated} paths`);

await global.db.close();
})();

0 comments on commit 0aedb04

Please sign in to comment.