diff --git a/classes/Queue.js b/classes/Queue.js index 9ed2fc1..037c12f 100644 --- a/classes/Queue.js +++ b/classes/Queue.js @@ -2,6 +2,9 @@ const metadata = require('../libs/metadata.js'); const { extname } = require('path'); const Artist = require('../models/Artist.js'); +const Album = require('../models/Album.js'); +const Track = require('../models/Track.js'); +const AlbumToArtist = require('../models/AlbumToArtist.js'); class Queue { @@ -76,12 +79,12 @@ class Queue { return; } const tags = await metadata.parseFile(file); - const artist = await this.addArtist(tags); + const artists = await this.addArtist(tags); const album = await this.addAlbum(tags); const track = await this.addTrack(tags, file); - this.linkTrackToArtist(track, artist); - this.linkTrackToAlbum(track, album); - this.linkAlbumToArtist(album, artist); + // this.linkTrackToArtist(track, artist); + // this.linkTrackToAlbum(track, album); + this.linkAlbumToArtists(album, artists); } async eventUnlink(file) { @@ -101,6 +104,17 @@ class Queue { if (file === undefined) { return; } + let artist = tags.common.artist; + const artists = tags.common.artists || []; + if (artist !== undefined && !artists.includes(artist)) { + artists.push(artist); + } + for (let index = 0; index < artists.length; index++) { + artist = new Artist(artists[index]); + await artist.save(); + artists[index] = artist; + } + return artists; } async addArtist(tags) { @@ -114,83 +128,28 @@ class Queue { } for (let index = 0; index < artists.length; index++) { artist = new Artist(artists[index]); - artist = await artist.save(); - return artist; + await artist.save(); + artists[index] = artist; } + return artists; } async addAlbum(tags) { if (tags?.common?.album === undefined) { return; } - try { - const [element, created] = await database.models.Album.findOrCreate({ - where: { - name: tags.common.album - } - }); - if (created) { - logger.debug('created album: ' + JSON.stringify(element)); - } - return element; - } catch (err) { - logger.error('error finding or creating album \'' + JSON.stringify(tags) + '\' > ' + err); - } + let album = new Album(tags.common.album); + await album.save(); + return album; } async addTrack(tags, file) { if (tags?.common?.title === undefined || file === undefined) { return; } - const where = { - file: file - }; - if (tags?.common?.title !== undefined) { - where.title = tags.common.title; - } - if (tags?.common?.year !== undefined) { - where.year = tags.common.year; - } - if (tags?.common?.duration !== undefined) { - where.duration = tags.common.duration; - } - if (tags?.common?.comment !== undefined) { - let comment = ''; - if (Array.isArray(tags.common.comment)) { - for (let index = 0; index < tags.common.comment.length; index++) { - if (comment.length > 0) { - comment += '\n'; - } - comment += tags.common.comment[index]; - } - } else { - comment = tags.common.comment; - } - where.comment = comment; - } - if (tags?.common?.disk?.no !== undefined) { - where.diskno = tags.common.disk.no; - } - if (tags?.common?.disk?.of !== undefined) { - where.diskof = tags.common.disk.of; - } - if (tags?.common?.track?.no !== undefined) { - where.trackno = tags.common.track.no; - } - if (tags?.common?.track?.of !== undefined) { - where.trackof = tags.common.track.of; - } - try { - const [element, created] = await database.models.Track.findOrCreate({ - where: where - }); - if (created) { - logger.debug('created track: ' + JSON.stringify(element)); - } - return element; - } catch (err) { - logger.error('error finding or creating track \'' + JSON.stringify(tags) + '\' > ' + err); - } + let track = new Track(tags, file); + await track.save(); + return track; } async linkTrackToArtist(track, artist) { @@ -233,23 +192,12 @@ class Queue { } } - async linkAlbumToArtist(album, artist) { - if (album === undefined || artist === undefined) { + async linkAlbumToArtists(album, artists) { + if (album === undefined || artists === undefined || artists.length === 0) { return; } - try { - const [element, created] = await database.models.AlbumToArtist.findOrCreate({ - where: { - album: album.id, - artist: artist.id - } - }); - if (created) { - logger.debug('linked album \'' + album.id + '\' to artist \'' + artist.id + '\': ' + JSON.stringify(element)); - } - return element; - } catch (err) { - logger.error('error finding or creating albumtoartist entry for album \'' + album.id + '\' and artist \'' + artist.id + '\' > ' + err); + for (let index = 0; index < artists.length; index++) { + await new AlbumToArtist(album, artists[index]).save(); } } diff --git a/classes/Watcher.js b/classes/Watcher.js index eb07ea0..16a6a05 100644 --- a/classes/Watcher.js +++ b/classes/Watcher.js @@ -20,6 +20,10 @@ class Watcher { if (initialScan === undefined) { initialScan = true; } + let polling = config?.library?.polling; + if (polling === undefined) { + polling = false; + } for (let index = 0; index < config.library.sources.length; index++) { const directory = path.resolve(config.library.sources[index]); let ignoreInitial = !initialScan; @@ -33,7 +37,8 @@ class Watcher { } logger.debug('watching directory \'' + directory + '\'...'); this.#handleEvents(chokidar.watch(directory, { - ignoreInitial: ignoreInitial + ignoreInitial: ignoreInitial, + usePolling: polling })); } } diff --git a/example_config.json b/example_config.json index 0ed3b36..4f4380b 100644 --- a/example_config.json +++ b/example_config.json @@ -17,7 +17,7 @@ "library": { "enabled": true, "sources": [ - "/home/velvettear/downloads" + "/run/media/velvettear/ext1tb_elements" ], "formats": [ "mp3", @@ -26,7 +26,8 @@ "initialscan": { "enabled": true, "maxage": 3600000 - } + }, + "polling": true }, "database": { "dialect": "postgres", diff --git a/models/AlbumToArtist.js b/models/AlbumToArtist.js index 37e2c19..8788908 100644 --- a/models/AlbumToArtist.js +++ b/models/AlbumToArtist.js @@ -10,6 +10,7 @@ class AlbumToArtist extends Base { constructor(album, artist) { super(); this.tableName = tableName; + this.relationTable = true; this.album = album; this.artist = artist; } diff --git a/models/Artist.js b/models/Artist.js index 36145af..8fb90a8 100644 --- a/models/Artist.js +++ b/models/Artist.js @@ -9,6 +9,7 @@ class Artist extends Base { constructor(name) { super(); + this.tableName = tableName; this.name = name; } diff --git a/models/Base.js b/models/Base.js index b935c84..eb2fd8c 100644 --- a/models/Base.js +++ b/models/Base.js @@ -18,7 +18,7 @@ class Base { if (element?.id === undefined) { return undefined; } - return element; + this.#fillThis(element); } catch (error) { logger.error('error finding or creating \'' + this.tableName + '\' entry: \'' + JSON.stringify(this) + '\' > ' + error); } @@ -61,6 +61,10 @@ class Base { return false; } + isRelationTable() { + return this.relationTable === true; + } + #fillThis(data) { if (data === undefined || data.dataValues === undefined || data.dataValues.id === undefined || data.dataValues.id < 0) { return; @@ -84,15 +88,19 @@ class Base { if (value === undefined) { continue; } - where[key] = value; + if (this.isRelationTable()) { + const id = value.id; + if (id === undefined) { + continue; + } + where[key] = id; + } else { + where[key] = value; + } } return where; } } - - - - module.exports = Base; \ No newline at end of file diff --git a/models/Track.js b/models/Track.js index 7207417..ec7b1d8 100644 --- a/models/Track.js +++ b/models/Track.js @@ -7,8 +7,32 @@ const tableName = path.basename(__filename, '.js').toLowerCase(); class Track extends Base { - constructor() { + constructor(tags, file) { super(); + this.tableName = tableName; + this.title = tags?.common?.title; + this.year = tags?.common?.year; + this.duration = tags?.common?.duration; + let comment = undefined; + if (tags?.common?.comment !== undefined) { + let comment = ''; + if (Array.isArray(tags.common.comment)) { + for (let index = 0; index < tags.common.comment.length; index++) { + if (comment.length > 0) { + comment += '\n'; + } + comment += tags.common.comment[index]; + } + } else { + comment = tags.common.comment; + } + } + this.comment = comment; + this.diskno = tags?.common?.disk?.no; + this.diskof = tags?.common?.disk?.of; + this.trackno = tags?.common?.track?.no; + this.trackof = tags?.common?.track?.of; + this.file = file; } isValid() { diff --git a/models/TrackToAlbum.js b/models/TrackToAlbum.js index 52c4840..c9f9ffb 100644 --- a/models/TrackToAlbum.js +++ b/models/TrackToAlbum.js @@ -1,3 +1,4 @@ +const { table } = require('console'); const path = require('path'); const { DataTypes } = require('sequelize'); @@ -9,6 +10,8 @@ class TrackToAlbum extends Base { constructor(track, album) { super(); + this.tableName = tableName; + this.relationTable = true; this.track = track; this.album = album; } diff --git a/models/TrackToArtist.js b/models/TrackToArtist.js index 5263e7a..3a65726 100644 --- a/models/TrackToArtist.js +++ b/models/TrackToArtist.js @@ -9,6 +9,8 @@ class TrackToArtist extends Base { constructor(track, artist) { super(); + this.tableName = tableName; + this.relationTable = true; this.track = track; this.artist = artist; }