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 { extname } = require('path');
|
||||||
|
|
||||||
const Artist = require('../models/Artist.js');
|
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 {
|
class Queue {
|
||||||
|
|
||||||
|
@ -76,12 +79,12 @@ class Queue {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const tags = await metadata.parseFile(file);
|
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 album = await this.addAlbum(tags);
|
||||||
const track = await this.addTrack(tags, file);
|
const track = await this.addTrack(tags, file);
|
||||||
this.linkTrackToArtist(track, artist);
|
// this.linkTrackToArtist(track, artist);
|
||||||
this.linkTrackToAlbum(track, album);
|
// this.linkTrackToAlbum(track, album);
|
||||||
this.linkAlbumToArtist(album, artist);
|
this.linkAlbumToArtists(album, artists);
|
||||||
}
|
}
|
||||||
|
|
||||||
async eventUnlink(file) {
|
async eventUnlink(file) {
|
||||||
|
@ -101,6 +104,17 @@ class Queue {
|
||||||
if (file === undefined) {
|
if (file === undefined) {
|
||||||
return;
|
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) {
|
async addArtist(tags) {
|
||||||
|
@ -114,83 +128,28 @@ class Queue {
|
||||||
}
|
}
|
||||||
for (let index = 0; index < artists.length; index++) {
|
for (let index = 0; index < artists.length; index++) {
|
||||||
artist = new Artist(artists[index]);
|
artist = new Artist(artists[index]);
|
||||||
artist = await artist.save();
|
await artist.save();
|
||||||
return artist;
|
artists[index] = artist;
|
||||||
}
|
}
|
||||||
|
return artists;
|
||||||
}
|
}
|
||||||
|
|
||||||
async addAlbum(tags) {
|
async addAlbum(tags) {
|
||||||
if (tags?.common?.album === undefined) {
|
if (tags?.common?.album === undefined) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
let album = new Album(tags.common.album);
|
||||||
const [element, created] = await database.models.Album.findOrCreate({
|
await album.save();
|
||||||
where: {
|
return album;
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async addTrack(tags, file) {
|
async addTrack(tags, file) {
|
||||||
if (tags?.common?.title === undefined || file === undefined) {
|
if (tags?.common?.title === undefined || file === undefined) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const where = {
|
let track = new Track(tags, file);
|
||||||
file: file
|
await track.save();
|
||||||
};
|
return track;
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async linkTrackToArtist(track, artist) {
|
async linkTrackToArtist(track, artist) {
|
||||||
|
@ -233,23 +192,12 @@ class Queue {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async linkAlbumToArtist(album, artist) {
|
async linkAlbumToArtists(album, artists) {
|
||||||
if (album === undefined || artist === undefined) {
|
if (album === undefined || artists === undefined || artists.length === 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
for (let index = 0; index < artists.length; index++) {
|
||||||
const [element, created] = await database.models.AlbumToArtist.findOrCreate({
|
await new AlbumToArtist(album, artists[index]).save();
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,10 @@ class Watcher {
|
||||||
if (initialScan === undefined) {
|
if (initialScan === undefined) {
|
||||||
initialScan = true;
|
initialScan = true;
|
||||||
}
|
}
|
||||||
|
let polling = config?.library?.polling;
|
||||||
|
if (polling === undefined) {
|
||||||
|
polling = false;
|
||||||
|
}
|
||||||
for (let index = 0; index < config.library.sources.length; index++) {
|
for (let index = 0; index < config.library.sources.length; index++) {
|
||||||
const directory = path.resolve(config.library.sources[index]);
|
const directory = path.resolve(config.library.sources[index]);
|
||||||
let ignoreInitial = !initialScan;
|
let ignoreInitial = !initialScan;
|
||||||
|
@ -33,7 +37,8 @@ class Watcher {
|
||||||
}
|
}
|
||||||
logger.debug('watching directory \'' + directory + '\'...');
|
logger.debug('watching directory \'' + directory + '\'...');
|
||||||
this.#handleEvents(chokidar.watch(directory, {
|
this.#handleEvents(chokidar.watch(directory, {
|
||||||
ignoreInitial: ignoreInitial
|
ignoreInitial: ignoreInitial,
|
||||||
|
usePolling: polling
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
"library": {
|
"library": {
|
||||||
"enabled": true,
|
"enabled": true,
|
||||||
"sources": [
|
"sources": [
|
||||||
"/home/velvettear/downloads"
|
"/run/media/velvettear/ext1tb_elements"
|
||||||
],
|
],
|
||||||
"formats": [
|
"formats": [
|
||||||
"mp3",
|
"mp3",
|
||||||
|
@ -26,7 +26,8 @@
|
||||||
"initialscan": {
|
"initialscan": {
|
||||||
"enabled": true,
|
"enabled": true,
|
||||||
"maxage": 3600000
|
"maxage": 3600000
|
||||||
}
|
},
|
||||||
|
"polling": true
|
||||||
},
|
},
|
||||||
"database": {
|
"database": {
|
||||||
"dialect": "postgres",
|
"dialect": "postgres",
|
||||||
|
|
|
@ -10,6 +10,7 @@ class AlbumToArtist extends Base {
|
||||||
constructor(album, artist) {
|
constructor(album, artist) {
|
||||||
super();
|
super();
|
||||||
this.tableName = tableName;
|
this.tableName = tableName;
|
||||||
|
this.relationTable = true;
|
||||||
this.album = album;
|
this.album = album;
|
||||||
this.artist = artist;
|
this.artist = artist;
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ class Artist extends Base {
|
||||||
|
|
||||||
constructor(name) {
|
constructor(name) {
|
||||||
super();
|
super();
|
||||||
|
this.tableName = tableName;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,7 @@ class Base {
|
||||||
if (element?.id === undefined) {
|
if (element?.id === undefined) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
return element;
|
this.#fillThis(element);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error('error finding or creating \'' + this.tableName + '\' entry: \'' + JSON.stringify(this) + '\' > ' + error);
|
logger.error('error finding or creating \'' + this.tableName + '\' entry: \'' + JSON.stringify(this) + '\' > ' + error);
|
||||||
}
|
}
|
||||||
|
@ -61,6 +61,10 @@ class Base {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
isRelationTable() {
|
||||||
|
return this.relationTable === true;
|
||||||
|
}
|
||||||
|
|
||||||
#fillThis(data) {
|
#fillThis(data) {
|
||||||
if (data === undefined || data.dataValues === undefined || data.dataValues.id === undefined || data.dataValues.id < 0) {
|
if (data === undefined || data.dataValues === undefined || data.dataValues.id === undefined || data.dataValues.id < 0) {
|
||||||
return;
|
return;
|
||||||
|
@ -84,15 +88,19 @@ class Base {
|
||||||
if (value === undefined) {
|
if (value === undefined) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
where[key] = value;
|
if (this.isRelationTable()) {
|
||||||
|
const id = value.id;
|
||||||
|
if (id === undefined) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
where[key] = id;
|
||||||
|
} else {
|
||||||
|
where[key] = value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return where;
|
return where;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
module.exports = Base;
|
module.exports = Base;
|
|
@ -7,8 +7,32 @@ const tableName = path.basename(__filename, '.js').toLowerCase();
|
||||||
|
|
||||||
class Track extends Base {
|
class Track extends Base {
|
||||||
|
|
||||||
constructor() {
|
constructor(tags, file) {
|
||||||
super();
|
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() {
|
isValid() {
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
const { table } = require('console');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const { DataTypes } = require('sequelize');
|
const { DataTypes } = require('sequelize');
|
||||||
|
|
||||||
|
@ -9,6 +10,8 @@ class TrackToAlbum extends Base {
|
||||||
|
|
||||||
constructor(track, album) {
|
constructor(track, album) {
|
||||||
super();
|
super();
|
||||||
|
this.tableName = tableName;
|
||||||
|
this.relationTable = true;
|
||||||
this.track = track;
|
this.track = track;
|
||||||
this.album = album;
|
this.album = album;
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,8 @@ class TrackToArtist extends Base {
|
||||||
|
|
||||||
constructor(track, artist) {
|
constructor(track, artist) {
|
||||||
super();
|
super();
|
||||||
|
this.tableName = tableName;
|
||||||
|
this.relationTable = true;
|
||||||
this.track = track;
|
this.track = track;
|
||||||
this.artist = artist;
|
this.artist = artist;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue