From 8a6c5e4c5d5146b38de6c44a519bd0892e1f6e1f Mon Sep 17 00:00:00 2001 From: jld3103 Date: Fri, 18 Aug 2023 20:04:34 +0200 Subject: [PATCH] feat(neon): Complete URL and add headers for cached images Signed-off-by: jld3103 --- .../neon/lib/src/widgets/cached_image.dart | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/neon/neon/lib/src/widgets/cached_image.dart b/packages/neon/neon/lib/src/widgets/cached_image.dart index 4b02d603..dd865cea 100644 --- a/packages/neon/neon/lib/src/widgets/cached_image.dart +++ b/packages/neon/neon/lib/src/widgets/cached_image.dart @@ -5,6 +5,7 @@ import 'dart:typed_data'; import 'package:flutter/material.dart'; import 'package:flutter_cache_manager/flutter_cache_manager.dart'; import 'package:flutter_svg/flutter_svg.dart'; +import 'package:neon/src/models/account.dart'; import 'package:neon/src/widgets/exception.dart'; import 'package:neon/src/widgets/linear_progress_indicator.dart'; @@ -27,6 +28,7 @@ class NeonCachedImage extends StatefulWidget { NeonCachedImage.url({ required final String url, + final Account? account, final Key? key, this.isSvgHint = false, this.size, @@ -34,7 +36,7 @@ class NeonCachedImage extends StatefulWidget { this.svgColor, this.iconColor, this.errorBuilder, - }) : image = _getImageFromUrl(url), + }) : image = _getImageFromUrl(url, account), super(key: key ?? Key(url)); NeonCachedImage.custom({ @@ -67,8 +69,18 @@ class NeonCachedImage extends StatefulWidget { final ErrorWidgetBuilder? errorBuilder; - static Future _getImageFromUrl(final String url) async { - final file = await _cacheManager.getSingleFile(url); + static Future _getImageFromUrl(final String url, final Account? account) async { + var uri = Uri.parse(url); + if (account != null) { + uri = account.completeUri(uri); + } + final file = await _cacheManager.getSingleFile( + uri.toString(), + headers: + account != null && uri.host == Uri.parse(account.serverURL).host && account.client.authentications.isNotEmpty + ? account.client.authentications.first.headers + : null, + ); return file.readAsBytes(); }