@@ -6,10 +6,13 @@ import {
6
6
type BaseItemDto ,
7
7
BaseItemKind ,
8
8
type BaseItemPerson ,
9
- ImageType
9
+ ImageType ,
10
+ type UserDto
10
11
} from '@jellyfin/sdk/lib/generated-client' ;
11
12
import { getImageApi } from '@jellyfin/sdk/lib/utils/api/image-api' ;
12
13
import type { ImageUrlsApi } from '@jellyfin/sdk/lib/utils/api/image-urls-api' ;
14
+ import type { ImageRequestParameters } from '@jellyfin/sdk/lib/models/api/image-request-parameters' ;
15
+ import { isNil } from '@jellyfin-vue/shared/validation' ;
13
16
import { remote } from '#/plugins/remote' ;
14
17
import { CardShapes , getShapeFromItemType , isPerson } from '#/utils/items' ;
15
18
@@ -22,13 +25,63 @@ const excludedBlurhashTypes = Object.freeze(
22
25
new Set < ImageType > ( [ ImageType . Logo ] )
23
26
) ;
24
27
28
+ const imageDefaultOptions = ( ) : ImageRequestParameters => ( { format : 'Webp' } ) ;
29
+
25
30
/**
26
31
* Gets the image URL given an item id and the image type requested
27
32
*/
28
33
export function getItemImageUrl ( ...args : Parameters < ImageUrlsApi [ 'getItemImageUrlById' ] > ) {
34
+ const argsLen = args . length ;
35
+
36
+ switch ( argsLen ) {
37
+ case 1 : {
38
+ args . push ( undefined , imageDefaultOptions ( ) ) ;
39
+ break ;
40
+ }
41
+ case 2 : {
42
+ args . push ( imageDefaultOptions ( ) ) ;
43
+ break ;
44
+ }
45
+ case 3 : {
46
+ args [ 2 ] = { ...args [ 2 ] , ...imageDefaultOptions ( ) } ;
47
+ break ;
48
+ }
49
+ }
50
+
29
51
return remote . sdk . newUserApi ( getImageApi ) . getItemImageUrlById ( ...args ) ;
30
52
}
31
53
54
+ /**
55
+ * Gets the logged's user image URL
56
+ */
57
+ export function getUserImageUrl ( user ?: UserDto ) {
58
+ return remote . sdk . newUserApi ( getImageApi ) . getUserImageUrl ( user , imageDefaultOptions ( ) ) ;
59
+ }
60
+
61
+ /**
62
+ * Gets the image url with the desired size and quality.
63
+ */
64
+ function getImageUrlWithSize (
65
+ itemId : string ,
66
+ { width,
67
+ height,
68
+ quality,
69
+ ratio = 1
70
+ } : {
71
+ width ?: number ;
72
+ height ?: number ;
73
+ quality ?: number ;
74
+ ratio ?: number ;
75
+ } = { } ,
76
+ imgType ?: ImageType
77
+ ) {
78
+ return getItemImageUrl ( itemId , imgType , {
79
+ quality,
80
+ maxWidth : isNil ( width ) ? undefined : Math . round ( width * ratio ) ,
81
+ maxHeight : isNil ( height ) ? undefined : Math . round ( height * ratio )
82
+ } ) ;
83
+ } ;
84
+
32
85
/**
33
86
* Gets the tag of the image of an specific item and type.
34
87
*
@@ -214,7 +267,6 @@ export function getImageInfo(
214
267
} = { }
215
268
) : ImageUrlInfo {
216
269
// TODO: Refactor to have separate getPosterImageInfo, getThumbImageInfo and getBackdropImageInfo.
217
- let url ;
218
270
let imgType ;
219
271
let imgTag ;
220
272
let itemId : string | null | undefined = item . Id ;
@@ -356,31 +408,13 @@ export function getImageInfo(
356
408
} ;
357
409
}
358
410
359
- const url_string = getItemImageUrl ( itemId , imgType ) ;
360
-
361
- if ( imgTag && imgType && url_string ) {
362
- url = new URL ( url_string ) ;
363
-
364
- const parameters : Record < string , string > = {
365
- imgTag,
366
- quality : quality . toString ( )
367
- } ;
368
-
369
- if ( width ) {
370
- width = Math . round ( width * ratio ) ;
371
- parameters . maxWidth = width . toString ( ) ;
372
- }
373
-
374
- if ( height ) {
375
- height = Math . round ( height * ratio ) ;
376
- parameters . maxHeight = height . toString ( ) ;
377
- }
378
-
379
- url . search = new URLSearchParams ( parameters ) . toString ( ) ;
380
- }
381
-
382
411
return {
383
- url : url ?. href ,
412
+ url : getImageUrlWithSize ( itemId , {
413
+ width,
414
+ height,
415
+ quality,
416
+ ratio
417
+ } , imgType ) ,
384
418
blurhash :
385
419
imgType && imgTag ? item . ImageBlurHashes ?. [ imgType ] ?. [ imgTag ] : undefined
386
420
} ;
@@ -411,7 +445,6 @@ export function getLogo(
411
445
tag ?: string ;
412
446
} = { }
413
447
) : ImageUrlInfo {
414
- let url ;
415
448
let imgType ;
416
449
let imgTag ;
417
450
let itemId : string | null | undefined = item . Id ;
@@ -428,24 +461,12 @@ export function getLogo(
428
461
itemId = item . ParentLogoItemId ;
429
462
}
430
463
431
- if ( imgTag && imgType && itemId ) {
432
- url = new URL ( getItemImageUrl ( itemId , imgType ) ) ;
433
-
434
- const parameters : Record < string , string > = {
435
- imgTag,
436
- quality : quality . toString ( )
437
- } ;
438
-
439
- if ( width ) {
440
- width = Math . round ( width * ratio ) ;
441
- parameters . maxWidth = width . toString ( ) ;
442
- }
443
-
444
- url . search = new URLSearchParams ( parameters ) . toString ( ) ;
445
- }
446
-
447
464
return {
448
- url : url ?. href ,
465
+ url : getImageUrlWithSize ( itemId ?? '' , {
466
+ width,
467
+ quality,
468
+ ratio
469
+ } , imgType ) ,
449
470
blurhash :
450
471
imgType && imgTag ? item . ImageBlurHashes ?. [ imgType ] ?. [ imgTag ] : undefined
451
472
} ;
0 commit comments