33// MIT License
44
55using ImageWizard . Caches ;
6+ using ImageWizard . Core . Loaders ;
67using ImageWizard . Loaders ;
78using ImageWizard . Processing ;
89using ImageWizard . Processing . Results ;
1314using Microsoft . Extensions . Logging ;
1415using Microsoft . Extensions . Options ;
1516using Microsoft . Net . Http . Headers ;
17+ using System . Buffers ;
1618using System . Globalization ;
1719using System . Text ;
1820
@@ -35,6 +37,7 @@ public static async Task ExecuteAsync(
3537 ICacheKey cacheKey ,
3638 ICacheHash cacheHash ,
3739 ICacheLock cacheLock ,
40+ ILoaderCacheKey loaderCacheKey ,
3841 InterceptorInvoker interceptor ,
3942 ImageWizardBuilder builder ,
4043 string signature ,
@@ -383,11 +386,11 @@ public static async Task ExecuteAsync(
383386 {
384387 responseHeaders . AppendList (
385388 HeaderNames . Vary ,
386- new [ ] {
389+ [
387390 ClientHints . DPRHeader ,
388391 ClientHints . WidthHeader ,
389392 ClientHints . ViewportWidthHeader
390- } ) ;
393+ ] ) ;
391394
392395 //is image?
393396 if ( cachedData . Metadata . Width != null )
@@ -443,4 +446,54 @@ public static async Task ExecuteAsync(
443446
444447 logger . LogTrace ( "Operation completed" ) ;
445448 }
449+
450+ private static async Task < LoaderResult > GetLoaderDataAsync (
451+ ILoaderCacheKey loaderCacheKey ,
452+ ILoader loader ,
453+ ICache cache ,
454+ ImageWizardUrl url ,
455+ CachedData ? cachedData )
456+ {
457+ string loaderKey = loaderCacheKey . Create ( url . LoaderType , url . LoaderSource ) ;
458+
459+ CachedData ? cacheResult = await cache . ReadAsync ( loaderKey ) ;
460+
461+ if ( cacheResult != null )
462+ {
463+ //original data modified?
464+ if ( cachedData != null && cachedData . Metadata . Cache . ETag == cachedData . Metadata . Cache . ETag )
465+ {
466+ return LoaderResult . NotModified ( ) ;
467+ }
468+
469+ Stream stream = await cacheResult . OpenReadAsync ( ) ;
470+
471+ return LoaderResult . Success ( new OriginalData ( cacheResult . Metadata . MimeType , stream ) ) ;
472+ }
473+
474+ LoaderResult result = await loader . GetAsync ( url . LoaderSource , cachedData ) ;
475+
476+ if ( result . State == LoaderResultState . Success && result . Result != null )
477+ {
478+ DateTime now = DateTime . UtcNow ;
479+
480+ Metadata metadata = new Metadata ( )
481+ {
482+ Cache = result . Result . Cache ,
483+ Created = now ,
484+ LastAccess = now ,
485+ Key = loaderKey ,
486+ //Hash = hash,
487+ //Filters = url.Filters.Select(x => x.Fullname).ToArray(),
488+ LoaderSource = url . LoaderSource ,
489+ LoaderType = url . LoaderType ,
490+ MimeType = result . Result . MimeType ,
491+ //FileLength = processingContext.Result.Data.Length
492+ } ;
493+
494+ await cache . WriteAsync ( metadata , result . Result . Data ) ;
495+ }
496+
497+ return result ;
498+ }
446499}
0 commit comments