Enhance image handling in FileViewerDispatch for web and mobile, adding error handling and CORS support

This commit is contained in:
Leon Bösche
2026-01-25 19:51:18 +01:00
parent 38bf8013cd
commit 7c86be2098

View File

@@ -104,12 +104,28 @@ class FileViewerDispatch {
),
);
} else if (mimeType.startsWith('image/')) {
return Container(
color: AppTheme.primaryBackground,
child: InteractiveViewer(
minScale: 0.5,
maxScale: 4.0,
child: Image.network(
Widget child;
if (kIsWeb) {
// Use HTML img element for web to handle CORS
ui_web.platformViewRegistry.registerViewFactory(viewerId, (int viewId) {
final imgElement = web.HTMLImageElement()
..src = url
..style.width = '100%'
..style.height = '100%'
..style.objectFit = 'contain'
..crossOrigin = 'anonymous';
imgElement.onError.listen((event) {
// Handle error
});
return imgElement;
});
child = HtmlElementView(viewType: viewerId);
} else {
// For mobile, use Image.network
child = Image.network(
url,
headers: headers,
fit: BoxFit.contain,
@@ -121,11 +137,14 @@ class FileViewerDispatch {
),
);
},
),
),
);
}
return Container(
color: AppTheme.primaryBackground,
child: InteractiveViewer(minScale: 0.5, maxScale: 4.0, child: child),
);
} else {
// Fallback
return Container(
color: AppTheme.primaryBackground,
child: Center(