Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/no more sharp #193

Merged
merged 3 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/music_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ export const albumToAlbumSummary = (it: Album): AlbumSummary => ({

export const playlistToPlaylistSummary = (it: Playlist): PlaylistSummary => ({
id: it.id,
name: it.name
name: it.name,
coverArt: it.coverArt
})

export type StreamingHeader = "content-type" | "content-length" | "content-range" | "accept-ranges";
Expand All @@ -131,7 +132,8 @@ export type CoverArt = {

export type PlaylistSummary = {
id: string,
name: string
name: string,
coverArt?: BUrn | undefined
}

export type Playlist = PlaylistSummary & {
Expand Down
72 changes: 14 additions & 58 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@
import { URLBuilder } from "./url_builder";
import makeI8N, { asLANGs, KEY, keys as i8nKeys, LANG } from "./i8n";
import { Icon, ICONS, festivals, features } from "./icon";
import _, { shuffle } from "underscore";
import _ from "underscore";
import morgan from "morgan";
import { takeWithRepeats } from "./utils";
import { parse } from "./burn";
import { axiosImageFetcher, ImageFetcher } from "./subsonic";
import {
Expand Down Expand Up @@ -558,87 +557,44 @@
});
});

const GRAVITY_9 = [
"north",
"northeast",
"east",
"southeast",
"south",
"southwest",
"west",
"northwest",
"centre",
];

app.get("/art/:burns/size/:size", (req, res) => {
app.get("/art/:burn/size/:size", (req, res) => {
const serviceToken = apiTokens.authTokenFor(
req.query[BONOB_ACCESS_TOKEN_HEADER] as string
);
const urns = req.params["burns"]!.split("&").map(parse);
const urn = parse(req.params["burn"]!);
const size = Number.parseInt(req.params["size"]!);

if (!serviceToken) {
return res.status(401).send();
} else if (!(size > 0)) {
return res.status(400).send();
}

return musicService
.login(serviceToken)
.then((musicLibrary) =>
Promise.all(
urns.map((it) => {
if (it.system == "external") {
return serverOpts.externalImageResolver(it.resource);
} else {
return musicLibrary.coverArt(it, size);
}
})
)
)
.then((coverArts) => coverArts.filter((it) => it))
.then(shuffle)
.then((coverArts) => {
if (coverArts.length == 1) {
const coverArt = coverArts[0]!;
.then((musicLibrary) => {
if (urn.system == "external") {
return serverOpts.externalImageResolver(urn.resource);
} else {
return musicLibrary.coverArt(urn, size);
}
})
.then((coverArt) => {
if(coverArt) {
res.status(200);
res.setHeader("content-type", coverArt.contentType);
return res.send(coverArt.data);
} else if (coverArts.length > 1) {
const gravity = [...GRAVITY_9];
return sharp({
create: {
width: size * 3,
height: size * 3,
channels: 3,
background: { r: 255, g: 255, b: 255 },
},
})
.composite(
takeWithRepeats(coverArts, 9).map((art) => ({
input: art?.data,
gravity: gravity.pop(),
}))
)
.png()
.toBuffer()
.then((image) => sharp(image).resize(size).png().toBuffer())
.then((image) => {
res.status(200);
res.setHeader("content-type", "image/png");
return res.send(image);
});
} else {
return res.status(404).send();
}
})
})
.catch((e: Error) => {
logger.error(`Failed fetching image ${urns.join("&")}/size/${size}`, {
logger.error(`Failed fetching image ${urn}/size/${size}`, {
cause: e,
});
return res.status(500).send();
});
});

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
authorization
, but is not rate-limited.
This route handler performs
authorization
, but is not rate-limited.

bindSmapiSoapServiceToExpress(
app,
Expand Down
55 changes: 10 additions & 45 deletions src/smapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { Clock } from "./clock";
import { URLBuilder } from "./url_builder";
import { asLANGs, I8N } from "./i8n";
import { ICON, iconForGenre } from "./icon";
import _, { uniq } from "underscore";
import _ from "underscore";
import { BUrn, formatForURL } from "./burn";
import {
isExpiredTokenError,
Expand Down Expand Up @@ -253,7 +253,7 @@ const playlist = (bonobUrl: URLBuilder, playlist: Playlist) => ({
itemType: "playlist",
id: `playlist:${playlist.id}`,
title: playlist.name,
albumArtURI: playlistAlbumArtURL(bonobUrl, playlist).href(),
albumArtURI: coverArtURI(bonobUrl, playlist).href(),
canPlay: true,
attributes: {
readOnly: false,
Expand All @@ -262,32 +262,9 @@ const playlist = (bonobUrl: URLBuilder, playlist: Playlist) => ({
},
});

export const playlistAlbumArtURL = (
export const coverArtURI = (
bonobUrl: URLBuilder,
playlist: Playlist
) => {
// todo: this should be put into config, or even just removed for the ND music source
if(process.env["BNB_DISABLE_PLAYLIST_ART"]) return iconArtURI(bonobUrl, "music");

const burns: BUrn[] = uniq(
playlist.entries.filter((it) => it.coverArt != undefined),
(it) => it.album.id
).map((it) => it.coverArt!);
if (burns.length == 0) {
return iconArtURI(bonobUrl, "error");
} else {
return bonobUrl.append({
pathname: `/art/${burns
.slice(0, 9)
.map((it) => encodeURIComponent(formatForURL(it)))
.join("&")}/size/180`,
});
}
};

export const defaultAlbumArtURI = (
bonobUrl: URLBuilder,
{ coverArt }: { coverArt: BUrn | undefined }
{ coverArt }: { coverArt?: BUrn | undefined }
) =>
pipe(
coverArt,
Expand All @@ -305,21 +282,6 @@ export const iconArtURI = (bonobUrl: URLBuilder, icon: ICON) =>
pathname: `/icon/${icon}/size/legacy`,
});

export const defaultArtistArtURI = (
bonobUrl: URLBuilder,
artist: ArtistSummary
) =>
pipe(
artist.image,
O.fromNullable,
O.map((it) =>
bonobUrl.append({
pathname: `/art/${encodeURIComponent(formatForURL(it))}/size/180`,
})
),
O.getOrElseW(() => iconArtURI(bonobUrl, "vinyl"))
);

export const sonosifyMimeType = (mimeType: string) =>
mimeType == "audio/x-flac" ? "audio/flac" : mimeType;

Expand All @@ -329,7 +291,7 @@ export const album = (bonobUrl: URLBuilder, album: AlbumSummary) => ({
artist: album.artistName,
artistId: `artist:${album.artistId}`,
title: album.name,
albumArtURI: defaultAlbumArtURI(bonobUrl, album).href(),
albumArtURI: coverArtURI(bonobUrl, album).href(),
canPlay: true,
// defaults
// canScroll: false,
Expand All @@ -348,7 +310,7 @@ export const track = (bonobUrl: URLBuilder, track: Track) => ({
albumId: `album:${track.album.id}`,
albumArtist: track.artist.name,
albumArtistId: track.artist.id ? `artist:${track.artist.id}` : undefined,
albumArtURI: defaultAlbumArtURI(bonobUrl, track).href(),
albumArtURI: coverArtURI(bonobUrl, track).href(),
artist: track.artist.name,
artistId: track.artist.id ? `artist:${track.artist.id}` : undefined,
duration: track.duration,
Expand All @@ -366,7 +328,7 @@ export const artist = (bonobUrl: URLBuilder, artist: ArtistSummary) => ({
id: `artist:${artist.id}`,
artistId: artist.id,
title: artist.name,
albumArtURI: defaultArtistArtURI(bonobUrl, artist).href(),
albumArtURI: coverArtURI(bonobUrl, { coverArt: artist.image }).href(),
});

function splitId<T>(id: string) {
Expand Down Expand Up @@ -872,9 +834,12 @@ function bindSmapiSoapServiceToExpress(
.then((it) =>
Promise.all(
it.map((playlist) => {
// todo: whats this odd copy all about, can we just delete it?
return {
id: playlist.id,
name: playlist.name,
coverArt: playlist.coverArt,
// todo: are these every important?
entries: []
};
}
Expand Down
17 changes: 15 additions & 2 deletions src/subsonic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
AlbumQueryType,
Artist,
AuthFailure,
PlaylistSummary
} from "./music_service";
import sharp from "sharp";
import _ from "underscore";
Expand Down Expand Up @@ -178,12 +179,15 @@ type GetAlbumResponse = {
type playlist = {
id: string;
name: string;
coverArt: string | undefined;
};

type GetPlaylistResponse = {
// todo: isnt the type here a composite? playlistSummary && { entry: song[]; }
playlist: {
id: string;
name: string;
coverArt: string | undefined;
entry: song[];
};
};
Expand Down Expand Up @@ -306,6 +310,13 @@ const asAlbum = (album: album): Album => ({
coverArt: coverArtURN(album.coverArt),
});

// coverArtURN
const asPlayListSummary = (playlist: playlist): PlaylistSummary => ({
id: playlist.id,
name: playlist.name,
coverArt: coverArtURN(playlist.coverArt)
})

export const asGenre = (genreName: string) => ({
id: b64Encode(genreName),
name: genreName,
Expand Down Expand Up @@ -862,7 +873,7 @@ export class Subsonic implements MusicService {
.getJSON<GetPlaylistsResponse>(credentials, "/rest/getPlaylists")
.then((it) => it.playlists.playlist || [])
.then((playlists) =>
playlists.map((it) => ({ id: it.id, name: it.name }))
playlists.map(asPlayListSummary)
),
playlist: async (id: string) =>
subsonic
Expand All @@ -875,6 +886,7 @@ export class Subsonic implements MusicService {
return {
id: playlist.id,
name: playlist.name,
coverArt: coverArtURN(playlist.coverArt),
entries: (playlist.entry || []).map((entry) => ({
...asTrack(
{
Expand All @@ -898,7 +910,8 @@ export class Subsonic implements MusicService {
name,
})
.then((it) => it.playlist)
.then((it) => ({ id: it.id, name: it.name })),
// todo: why is this line so similar to other playlist lines??
.then((it) => ({ id: it.id, name: it.name, coverArt: coverArtURN(it.coverArt) })),
deletePlaylist: async (id: string) =>
subsonic
.getJSON<GetPlaylistResponse>(credentials, "/rest/deletePlaylist", {
Expand Down
Loading
Loading