From edc80bc3b44f4b517d0c15f18b6e27797d1a9b3a Mon Sep 17 00:00:00 2001 From: jeffvli Date: Mon, 21 Nov 2022 11:27:39 -0800 Subject: [PATCH] Fix subsonic scanner --- server/queue/subsonic/subsonic.api.ts | 6 +-- server/queue/subsonic/subsonic.scanner.ts | 54 +++++++++++++---------- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/server/queue/subsonic/subsonic.api.ts b/server/queue/subsonic/subsonic.api.ts index 55374e755..cd81da674 100644 --- a/server/queue/subsonic/subsonic.api.ts +++ b/server/queue/subsonic/subsonic.api.ts @@ -1,7 +1,7 @@ import { Server } from '@prisma/client'; -import { randomString } from '@utils/index'; import axios from 'axios'; import md5 from 'md5'; +import { randomString } from '../../utils/random-string'; import { SSAlbumListEntry, SSAlbumListResponse, @@ -103,8 +103,8 @@ const getAlbums = async ( ) .then((res) => { if ( - !res.data.albumList2.album || - res.data.albumList2.album.length === 0 + !res.data.albumList2?.album || + res.data.albumList2?.album?.length === 0 ) { // Flatten and return once there are no more albums left return recursiveData.flatMap((album) => album); diff --git a/server/queue/subsonic/subsonic.scanner.ts b/server/queue/subsonic/subsonic.scanner.ts index cf1c3d26f..e349de101 100644 --- a/server/queue/subsonic/subsonic.scanner.ts +++ b/server/queue/subsonic/subsonic.scanner.ts @@ -1,7 +1,7 @@ /* eslint-disable no-await-in-loop */ import { ImageType, Server, ServerFolder, Task } from '@prisma/client'; -import { prisma, throttle } from '@lib/index'; -import { uniqueArray } from '@utils/index'; +import { prisma, throttle } from '../../lib/index'; +import { uniqueArray } from '../../utils/index'; import { queue } from '../queues'; import { subsonicApi } from './subsonic.api'; import { subsonicUtils } from './subsonic.utils'; @@ -73,7 +73,7 @@ export const scanAlbums = async ( }); const albums = await subsonicApi.getAlbums(server, { - musicFolderId: serverFolder.id, + musicFolderId: serverFolder.remoteId, offset: 0, size: 500, type: 'newest', @@ -103,33 +103,35 @@ export const scanAlbums = async ( await prisma.album.upsert({ create: { albumArtists: { connect: albumArtistConnect }, + deleted: false, genres: { connect: album.genre ? { name: album.genre } : undefined }, images: { connect: imagesConnect }, - name: album.title, + name: album.name, releaseDate: album?.year - ? new Date(album.year, 0).toISOString() + ? new Date(Number(String(album.year).slice(4)), 0).toISOString() : undefined, releaseYear: album.year, remoteCreatedAt: album.created, remoteId: album.id, serverFolders: { connect: { id: serverFolder.id } }, serverId: server.id, - sortName: album.title, + sortName: album.name, }, update: { albumArtists: { connect: albumArtistConnect }, + deleted: false, genres: { connect: album.genre ? { name: album.genre } : undefined }, images: { connect: imagesConnect }, - name: album.title, + name: album.name, releaseDate: album?.year - ? new Date(album.year, 0).toISOString() + ? new Date(Number(String(album.year).slice(4)), 0).toISOString() : undefined, releaseYear: album.year, remoteCreatedAt: album.created, remoteId: album.id, serverFolders: { connect: { id: serverFolder.id } }, serverId: server.id, - sortName: album.title, + sortName: album.name, }, where: { uniqueAlbumId: { @@ -170,45 +172,53 @@ const throttledAlbumFetch = throttle( return { create: { - albumArtists: { connect: albumArtistsConnect }, + // albumArtistId: song.artistId ? song.artistId : undefined, + albumArtist: { connect: albumArtistsConnect }, artistName: !song.artistId ? song.artist : undefined, - bitRate: song.bitRate, + bitRate: song.bitRate ? song.bitRate : undefined, container: song.suffix, + deleted: false, discNumber: song.discNumber, duration: song.duration, genres: { connect: genresConnect }, images: { connect: imagesConnect }, name: song.title, releaseDate: song?.year - ? new Date(song.year, 0).toISOString() + ? new Date(Number(String(song.year).slice(4)), 0).toISOString() : undefined, releaseYear: song.year, remoteCreatedAt: song.created, remoteId: song.id, + server: { connect: { id: server.id } }, serverFolders: { connect: { id: serverFolder.id } }, - serverId: server.id, + // serverId: server.id, size: song.size, sortName: song.title, trackNumber: song.track, }, update: { - albumArtists: { connect: albumArtistsConnect }, + albumArtist: { connect: albumArtistsConnect }, + // albumArtistId: song.artistId ? song.artistId : undefined, artistName: !song.artistId ? song.artist : undefined, - bitRate: song.bitRate, + + bitRate: song.bitRate ? song.bitRate : undefined, container: song.suffix, + deleted: false, discNumber: song.discNumber, duration: song.duration, genres: { connect: genresConnect }, images: { connect: imagesConnect }, name: song.title, releaseDate: song?.year - ? new Date(song.year, 0).toISOString() + ? new Date(Number(String(song.year).slice(4)), 0).toISOString() : undefined, releaseYear: song.year, remoteCreatedAt: song.created, remoteId: song.id, + server: { connect: { id: server.id } }, serverFolders: { connect: { id: serverFolder.id } }, - serverId: server.id, + + // serverId: server.id, size: song.size, sortName: song.title, trackNumber: song.track, @@ -228,7 +238,7 @@ const throttledAlbumFetch = throttle( const artistsConnect = uniqueArtistIds.map((artistId) => { return { - uniqueArtistId: { + uniqueAlbumArtistId: { remoteId: artistId!, serverId: server.id, }, @@ -237,12 +247,12 @@ const throttledAlbumFetch = throttle( await prisma.album.update({ data: { - artists: { connect: artistsConnect }, + // albumArtists: { connect: artistsConnect }, songs: { upsert: songsUpsert }, }, where: { uniqueAlbumId: { - remoteId: albumRes.album.id, + remoteId: album.remoteId, serverId: server.id, }, }, @@ -269,10 +279,8 @@ export const scanAlbumDetail = async ( }); for (let i = 0; i < dbAlbums.length; i += 1) { - promises.push(throttledAlbumFetch(server, serverFolder, dbAlbums[i])); + await throttledAlbumFetch(server, serverFolder, dbAlbums[i]); } - - await Promise.all(promises); }; const scanAll = async (