implemented md5 hash sum for files
This commit is contained in:
parent
027da080da
commit
e8e79e58f2
6 changed files with 78 additions and 12 deletions
|
@ -1,5 +1,6 @@
|
||||||
const metadata = require('../libs/metadata.js');
|
const metadata = require('../libs/metadata.js');
|
||||||
const ext = require('path').extname;
|
const { md5 } = require('../libs/util.js');
|
||||||
|
const { extname } = require('path');
|
||||||
|
|
||||||
class Queue {
|
class Queue {
|
||||||
|
|
||||||
|
@ -10,7 +11,7 @@ class Queue {
|
||||||
}
|
}
|
||||||
|
|
||||||
add(event, file, stats) {
|
add(event, file, stats) {
|
||||||
if (file === undefined || !this.filter.includes(ext(file))) {
|
if (file === undefined || !this.filter.includes(extname(file))) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const element = { file: file };
|
const element = { file: file };
|
||||||
|
@ -73,10 +74,14 @@ class Queue {
|
||||||
if (file === undefined) {
|
if (file === undefined) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const hash = await md5(file);
|
||||||
|
if (! await this.fileChanged(file, hash)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
const tags = await metadata.parseFile(file);
|
const tags = await metadata.parseFile(file);
|
||||||
const artist = await this.addArtist(tags);
|
const artist = await this.addArtist(tags);
|
||||||
const album = await this.addAlbum(tags);
|
const album = await this.addAlbum(tags);
|
||||||
const track = await this.addTrack(tags, file);
|
const track = await this.addTrack(tags, file, hash);
|
||||||
this.linkTrackToArtist(track, artist);
|
this.linkTrackToArtist(track, artist);
|
||||||
this.linkTrackToAlbum(track, album);
|
this.linkTrackToAlbum(track, album);
|
||||||
this.linkAlbumToArtist(album, artist);
|
this.linkAlbumToArtist(album, artist);
|
||||||
|
@ -101,6 +106,29 @@ class Queue {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fileChanged(file, hash) {
|
||||||
|
if (file === undefined) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const where = {
|
||||||
|
file: file
|
||||||
|
};
|
||||||
|
if (hash === undefined) {
|
||||||
|
hash = await md5(file);
|
||||||
|
}
|
||||||
|
if (hash !== undefined) {
|
||||||
|
where.hash = hash;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const element = await database.models.Track.findOne({
|
||||||
|
where: where
|
||||||
|
});
|
||||||
|
return element?.id === undefined;
|
||||||
|
} catch (err) {
|
||||||
|
logger.error('error finding database entry for file \'' + file + '\' with md5 sum \'' + hash + '\' > ' + err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async addArtist(tags) {
|
async addArtist(tags) {
|
||||||
if (tags?.common?.artist === undefined && tags?.common?.artists === undefined) {
|
if (tags?.common?.artist === undefined && tags?.common?.artists === undefined) {
|
||||||
return;
|
return;
|
||||||
|
@ -145,14 +173,23 @@ class Queue {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async addTrack(tags, file) {
|
async addTrack(tags, file, hash) {
|
||||||
if (tags?.common?.title === undefined || file === undefined) {
|
if (tags?.common?.title === undefined || file === undefined) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
let timestamp = new Date();
|
||||||
const where = {
|
const where = {
|
||||||
file: file,
|
file: file
|
||||||
title: tags.common.title
|
|
||||||
};
|
};
|
||||||
|
if (hash === undefined) {
|
||||||
|
hash = await md5(file);
|
||||||
|
}
|
||||||
|
if (hash !== undefined) {
|
||||||
|
where.hash = hash;
|
||||||
|
}
|
||||||
|
if (tags?.common?.title !== undefined) {
|
||||||
|
where.title = tags.common.title;
|
||||||
|
}
|
||||||
if (tags?.common?.year !== undefined) {
|
if (tags?.common?.year !== undefined) {
|
||||||
where.year = tags.common.year;
|
where.year = tags.common.year;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
docker-compose.yml
|
docker-compose.yml
|
||||||
|
*.json
|
|
@ -18,4 +18,4 @@ services:
|
||||||
- 3001:3001
|
- 3001:3001
|
||||||
- 3002:3002
|
- 3002:3002
|
||||||
volumes:
|
volumes:
|
||||||
- /path/to/library:/mnt/library
|
- /mnt/samsung/music:/mnt/library
|
|
@ -17,7 +17,7 @@
|
||||||
"library": {
|
"library": {
|
||||||
"enabled": true,
|
"enabled": true,
|
||||||
"sources": [
|
"sources": [
|
||||||
"/mnt/library"
|
"/mnt/samsung/music"
|
||||||
],
|
],
|
||||||
"formats": [
|
"formats": [
|
||||||
"mp3",
|
"mp3",
|
||||||
|
@ -38,4 +38,4 @@
|
||||||
"nodelay": false,
|
"nodelay": false,
|
||||||
"threshold": 10
|
"threshold": 10
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
29
libs/util.js
29
libs/util.js
|
@ -1,3 +1,6 @@
|
||||||
|
const crypto = require('crypto');
|
||||||
|
const { open } = require('fs/promises');
|
||||||
|
|
||||||
function isEnabled(parameter) {
|
function isEnabled(parameter) {
|
||||||
return isSet(parameter?.enabled) && parameter.enabled === true;
|
return isSet(parameter?.enabled) && parameter.enabled === true;
|
||||||
}
|
}
|
||||||
|
@ -23,10 +26,34 @@ async function sleep(ms) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function md5(file) {
|
||||||
|
if (file === undefined) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let buffer;
|
||||||
|
let filehandle;
|
||||||
|
try {
|
||||||
|
filehandle = await open(file);
|
||||||
|
buffer = await filehandle.readFile();
|
||||||
|
} catch (error) {
|
||||||
|
logger.error('encountered an error generating the md5 hash sum for file \'' + file + '\' > ' + error);
|
||||||
|
} finally {
|
||||||
|
await filehandle?.close();
|
||||||
|
}
|
||||||
|
if (buffer === undefined) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let md5 = crypto.createHash('md5');
|
||||||
|
md5.update(buffer);
|
||||||
|
md5 = md5.digest('hex');
|
||||||
|
return md5;
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
isEnabled,
|
isEnabled,
|
||||||
isDisabled,
|
isDisabled,
|
||||||
isSet,
|
isSet,
|
||||||
isUnset,
|
isUnset,
|
||||||
sleep
|
sleep,
|
||||||
|
md5
|
||||||
}
|
}
|
|
@ -9,7 +9,8 @@ const Track = database.connection.define("track", {
|
||||||
diskof: DataTypes.INTEGER,
|
diskof: DataTypes.INTEGER,
|
||||||
trackno: DataTypes.INTEGER,
|
trackno: DataTypes.INTEGER,
|
||||||
trackof: DataTypes.INTEGER,
|
trackof: DataTypes.INTEGER,
|
||||||
file: DataTypes.TEXT
|
file: DataTypes.TEXT,
|
||||||
|
hash: DataTypes.TEXT
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = Track;
|
module.exports = Track;
|
Loading…
Reference in a new issue