mirror of
https://github.com/jellyfin/jellyfin-web
synced 2025-03-30 19:56:21 +00:00
Fix too long URLs on getItems with many ids
This commit is contained in:
parent
5e51616e18
commit
d5e9541010
6 changed files with 80 additions and 4 deletions
63
src/utils/jellyfin-apiclient/getItemsHelper.ts
Normal file
63
src/utils/jellyfin-apiclient/getItemsHelper.ts
Normal file
|
@ -0,0 +1,63 @@
|
|||
import type {BaseItemDtoQueryResult} from '@jellyfin/sdk/lib/generated-client';
|
||||
import {ApiClient} from 'jellyfin-apiclient';
|
||||
|
||||
const idsPerItemRequestLimit = 25;
|
||||
|
||||
function getItemsSplit(apiClient: ApiClient, userId: string, options: any) {
|
||||
const optionsTemplate = {...options};
|
||||
const ids = options.Ids.split(',');
|
||||
const results = [];
|
||||
|
||||
let nextI;
|
||||
for (let i = 0; i < ids.length && i < options.Limit; i = nextI) {
|
||||
nextI = i + idsPerItemRequestLimit;
|
||||
if (nextI > options.Limit) {
|
||||
nextI = options.Limit;
|
||||
}
|
||||
const idsSlice = ids.slice(i, nextI);
|
||||
console.log(idsSlice);
|
||||
optionsTemplate.Ids = idsSlice.join(',');
|
||||
results.push(apiClient.getItems(userId, optionsTemplate));
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
function mergeResults(results: BaseItemDtoQueryResult[]) {
|
||||
const merged: BaseItemDtoQueryResult = {};
|
||||
merged.Items = [];
|
||||
merged.TotalRecordCount = 0;
|
||||
merged.StartIndex = 0;
|
||||
|
||||
for (const result of results) {
|
||||
if (result.Items == null) {
|
||||
console.log(`Retrieved Items array is invalid: ${result.Items}`);
|
||||
continue;
|
||||
}
|
||||
if (result.TotalRecordCount == null) {
|
||||
console.log(`Retrieved TotalRecordCount is invalid: ${
|
||||
result.TotalRecordCount}`);
|
||||
continue;
|
||||
}
|
||||
if (result.StartIndex == null) {
|
||||
console.log(
|
||||
`Retrieved StartIndex is invalid: ${result.StartIndex}`);
|
||||
continue;
|
||||
}
|
||||
merged.Items = merged.Items.concat(result.Items);
|
||||
merged.TotalRecordCount += result.TotalRecordCount;
|
||||
merged.StartIndex = Math.min(merged.StartIndex, result.StartIndex);
|
||||
}
|
||||
return merged;
|
||||
}
|
||||
|
||||
export function getItems(apiClient: ApiClient, userId: string, options?: any):
|
||||
Promise<BaseItemDtoQueryResult> {
|
||||
if (options.Ids === undefined ||
|
||||
options.Ids.split(',').ength <= idsPerItemRequestLimit) {
|
||||
return apiClient.getItems(apiClient.getCurrentUserId(), options);
|
||||
}
|
||||
const results = getItemsSplit(apiClient, userId, options);
|
||||
|
||||
return Promise.all(results).then(mergeResults);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue