@@ -7,9 +7,11 @@ import { unifiedCloudManager } from '$lib/util/sync/unified-cloud-manager';
77import { generatePlaceholders } from '$lib/catalog/placeholders' ;
88import { routeParams } from '$lib/util/hash-router' ;
99import { getLegacyImageOnlyVolumeUuid } from '$lib/util/download-volume-repair' ;
10+ import type { CloudFileMetadata } from '$lib/util/sync/provider-interface' ;
1011
1112const AI_REFRESH_INTERVAL_MS = 15_000 ;
1213const AI_DOWNLOAD_ATTEMPTS = 3 ;
14+ const discoveredAiFiles = new Map < string , CloudFileMetadata > ( ) ;
1315
1416function delay ( milliseconds : number ) : Promise < void > {
1517 return new Promise ( ( resolve ) => setTimeout ( resolve , milliseconds ) ) ;
@@ -47,18 +49,28 @@ async function downloadAiSidecar(
4749 throw lastError ;
4850}
4951
50- async function loadCurrentVolumeData ( volume : VolumeMetadata ) : Promise < VolumeData | undefined > {
52+ async function loadCurrentVolumeData (
53+ volume : VolumeMetadata ,
54+ pollAi = false
55+ ) : Promise < VolumeData | undefined > {
5156 const provider = unifiedCloudManager . getActiveProvider ( ) ;
5257 if ( provider ) {
5358 const cloudPath = volume . cloudPath || `${ volume . series_title } /${ volume . volume_title } .cbz` ;
5459 const aiPath = cloudPath . replace ( / \. ( c b z | z i p | c b r | r a r | 7 z ) $ / i, '.mokuro-ai.json' ) . toLowerCase ( ) ;
55- const remoteAi = unifiedCloudManager
56- . getAllCloudVolumes ( )
57- . find ( ( file ) => file . path . toLowerCase ( ) === aiPath ) ;
60+ const discoveryKey = `${ provider . type } :${ aiPath } ` ;
61+ let remoteAi =
62+ unifiedCloudManager . getAllCloudVolumes ( ) . find ( ( file ) => file . path . toLowerCase ( ) === aiPath ) ||
63+ discoveredAiFiles . get ( discoveryKey ) ;
64+ if ( ! remoteAi && pollAi ) {
65+ remoteAi = ( await provider . listCloudVolumes ( ) ) . find (
66+ ( file ) => file . path . toLowerCase ( ) === aiPath
67+ ) ;
68+ if ( remoteAi ) discoveredAiFiles . set ( discoveryKey , remoteAi ) ;
69+ }
5870 if ( remoteAi ) {
5971 const existingAi = await db . volume_ai . get ( volume . volume_uuid ) ;
6072 const remoteModified = Date . parse ( remoteAi . modifiedTime || '' ) || 0 ;
61- if ( ! existingAi || existingAi . updated_at * 1000 < remoteModified ) {
73+ if ( pollAi || ! existingAi || existingAi . updated_at * 1000 < remoteModified ) {
6274 try {
6375 await downloadAiSidecar ( provider , remoteAi , volume , existingAi ) ;
6476 } catch ( error ) {
@@ -206,10 +218,9 @@ export const currentVolumeData: Readable<VolumeData | undefined> = derived(
206218 }
207219
208220 if ( $currentVolume ) {
209- const refresh = async ( refreshCloudFiles = false ) => {
221+ const refresh = async ( pollAi = false ) => {
210222 try {
211- if ( refreshCloudFiles ) await unifiedCloudManager . fetchAllCloudVolumes ( ) ;
212- const volumeData = await loadCurrentVolumeData ( $currentVolume ) ;
223+ const volumeData = await loadCurrentVolumeData ( $currentVolume , pollAi ) ;
213224 if ( ! cancelled && volumeData ) {
214225 set ( volumeData ) ;
215226 if (
0 commit comments