mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-23 05:21:15 +00:00
c7c9ba24a3
The photo picker's Select all took only the pages already loaded, 50 at a time, and replaced whatever had been picked on another tab. It now loads the rest of a day, trip or date-range search before selecting (cancelled by closing the picker or switching tab), adds to earlier picks, and Deselect all only clears what is in view. Add stays disabled until that loading is done. The whole-library tab keeps selecting what is loaded and shows the count as "N+". A date-filtered Immich search stopped at 1,000 photos without a word, because its page budget was absolute; it is now relative to the page asked for, with an absolute ceiling of 270 raw pages sized so the picker's deepest page still gets a full answer. Adds go out in batches of 500 ids, under the JSON body limit, and each batch is sent oldest first, so photos land in an entry in the order they were taken. When a later batch fails, the photos already saved are counted and the journey reloads while the error still shows, through one hook for desktop and phone. After a provider add, the capture-time lookup runs at most four at a time per user, shared by all of that user's batches. When it fills a row, a journey:photos:updated event reloads open journeys once per batch, from the REST routes and the MCP add tool alike. Device uploads, which arrive one file per request, get the same lookup without the event. Addresses discussion #1587.
85 lines
3.1 KiB
TypeScript
85 lines
3.1 KiB
TypeScript
/**
|
|
* Most provider asset ids one add request carries (#1587).
|
|
*
|
|
* The server parses JSON bodies up to 100 kB, and an Immich id with its media
|
|
* type costs about 47 bytes, so a single request stopped at roughly 2,180
|
|
* photos with a 413 the moment "Select all" could reach a whole trip. 500 keeps
|
|
* every request far under that for either provider.
|
|
*/
|
|
export const PROVIDER_PHOTO_BATCH = 500
|
|
|
|
/** What the two provider-photo add routes answer, summed over every batch. */
|
|
export interface ProviderPhotosAdded {
|
|
photos: unknown[]
|
|
added: number
|
|
}
|
|
|
|
/**
|
|
* A batched add that failed after earlier batches were stored.
|
|
*
|
|
* Those batches are on the journey whatever happens next, so the error carries
|
|
* what they added: a caller can count them and reload, and still report the
|
|
* failure for the rest. `cause` is what the failing batch rejected with.
|
|
*/
|
|
export class ProviderPhotoBatchError extends Error implements ProviderPhotosAdded {
|
|
readonly photos: unknown[]
|
|
readonly added: number
|
|
readonly cause: unknown
|
|
|
|
constructor(saved: ProviderPhotosAdded, cause: unknown) {
|
|
super(cause instanceof Error ? cause.message : 'Adding the photos failed part way')
|
|
this.name = 'ProviderPhotoBatchError'
|
|
this.photos = saved.photos
|
|
this.added = saved.added
|
|
this.cause = cause
|
|
}
|
|
}
|
|
|
|
/**
|
|
* How many photos a failed add stored all the same: the count a
|
|
* ProviderPhotoBatchError carries, 0 for any other failure.
|
|
*/
|
|
export function addedBeforeFailure(err: unknown): number {
|
|
return err instanceof ProviderPhotoBatchError ? err.added : 0
|
|
}
|
|
|
|
/**
|
|
* Send a provider-photo add in batches the body limit lets through, and answer
|
|
* as one request would have.
|
|
*
|
|
* `mediaTypes` is parallel to `assetIds` and is cut at the same positions, so
|
|
* every id keeps its own type. The batches go one after the other: the server
|
|
* numbers photos in the order it receives them, and the picker sends them in
|
|
* capture order, which concurrent requests could interleave. A failing batch
|
|
* stops the run. When it is the first, the request's own error comes through
|
|
* unchanged; after that it is a ProviderPhotoBatchError carrying what the
|
|
* earlier batches stored. Adding those again is harmless because the server
|
|
* skips what is already there.
|
|
*
|
|
* An empty list still makes the one request it always made.
|
|
*/
|
|
export async function postProviderPhotosInBatches(
|
|
assetIds: string[],
|
|
mediaTypes: string[] | undefined,
|
|
post: (assetIds: string[], mediaTypes: string[] | undefined) => Promise<Partial<ProviderPhotosAdded> | undefined>,
|
|
batchSize: number = PROVIDER_PHOTO_BATCH,
|
|
): Promise<ProviderPhotosAdded> {
|
|
const photos: unknown[] = []
|
|
let added = 0
|
|
let start = 0
|
|
do {
|
|
const end = start + batchSize
|
|
let result: Partial<ProviderPhotosAdded> | undefined
|
|
try {
|
|
result = await post(assetIds.slice(start, end), mediaTypes?.slice(start, end))
|
|
} catch (err) {
|
|
if (start === 0) throw err
|
|
throw new ProviderPhotoBatchError({ photos, added }, err)
|
|
}
|
|
photos.push(...(result?.photos ?? []))
|
|
added += result?.added ?? 0
|
|
start = end
|
|
} while (start < assetIds.length)
|
|
return { photos, added }
|
|
}
|