extended object oriented approach
This commit is contained in:
parent
1279676470
commit
73c225ac6d
9 changed files with 86 additions and 93 deletions
114
classes/Queue.js
114
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -10,6 +10,7 @@ class AlbumToArtist extends Base {
|
|||
constructor(album, artist) {
|
||||
super();
|
||||
this.tableName = tableName;
|
||||
this.relationTable = true;
|
||||
this.album = album;
|
||||
this.artist = artist;
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ class Artist extends Base {
|
|||
|
||||
constructor(name) {
|
||||
super();
|
||||
this.tableName = tableName;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
|
@ -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() {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -9,6 +9,8 @@ class TrackToArtist extends Base {
|
|||
|
||||
constructor(track, artist) {
|
||||
super();
|
||||
this.tableName = tableName;
|
||||
this.relationTable = true;
|
||||
this.track = track;
|
||||
this.artist = artist;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue