🐛 Fix attachment displaying according the latest server

This commit is contained in:
LittleSheep 2024-07-29 17:56:36 +08:00
parent 3ca98fa58c
commit 3db6850d89
6 changed files with 65 additions and 32 deletions

View File

@ -12,7 +12,8 @@ class Attachment {
String usage; String usage;
String mimetype; String mimetype;
String hash; String hash;
String destination; int destination;
bool isAnalyzed;
Map<String, dynamic>? metadata; Map<String, dynamic>? metadata;
bool isMature; bool isMature;
Account? account; Account? account;
@ -31,6 +32,7 @@ class Attachment {
required this.mimetype, required this.mimetype,
required this.hash, required this.hash,
required this.destination, required this.destination,
required this.isAnalyzed,
required this.metadata, required this.metadata,
required this.isMature, required this.isMature,
required this.account, required this.account,
@ -50,6 +52,7 @@ class Attachment {
mimetype: json['mimetype'], mimetype: json['mimetype'],
hash: json['hash'], hash: json['hash'],
destination: json['destination'], destination: json['destination'],
isAnalyzed: json['is_analyzed'],
metadata: json['metadata'], metadata: json['metadata'],
isMature: json['is_mature'], isMature: json['is_mature'],
account: json['account'] != null ? Account.fromJson(json['account']) : null, account: json['account'] != null ? Account.fromJson(json['account']) : null,
@ -69,6 +72,7 @@ class Attachment {
'mimetype': mimetype, 'mimetype': mimetype,
'hash': hash, 'hash': hash,
'destination': destination, 'destination': destination,
'is_analyzed': isAnalyzed,
'metadata': metadata, 'metadata': metadata,
'is_mature': isMature, 'is_mature': isMature,
'account': account?.toJson(), 'account': account?.toJson(),

View File

@ -6,6 +6,7 @@ import 'dart:typed_data';
import 'package:crypto/crypto.dart'; import 'package:crypto/crypto.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:path/path.dart'; import 'package:path/path.dart';
import 'package:solian/models/attachment.dart';
import 'package:solian/platform.dart'; import 'package:solian/platform.dart';
import 'package:solian/providers/auth.dart'; import 'package:solian/providers/auth.dart';
import 'package:solian/services.dart'; import 'package:solian/services.dart';
@ -65,17 +66,24 @@ class AttachmentProvider extends GetConnect {
httpClient.baseUrl = ServiceFinder.buildUrl('files', null); httpClient.baseUrl = ServiceFinder.buildUrl('files', null);
} }
final Map<int, Response> _cachedResponses = {}; final Map<int, Attachment> _cachedResponses = {};
Future<Response> getMetadata(int id, {noCache = false}) async { Future<Attachment?> getMetadata(int id, {noCache = false}) async {
if (!noCache && _cachedResponses.containsKey(id)) { if (!noCache && _cachedResponses.containsKey(id)) {
return _cachedResponses[id]!; return _cachedResponses[id]!;
} }
final resp = await get('/attachments/$id/meta'); final resp = await get('/attachments/$id/meta');
_cachedResponses[id] = resp; if (resp.statusCode == 200) {
final result = Attachment.fromJson(resp.body);
if (result.destination != 0 && result.isAnalyzed) {
_cachedResponses[id] = result;
}
print(result);
return result;
}
return resp; return null;
} }
Future<Response> createAttachment( Future<Response> createAttachment(

View File

@ -1,5 +1,6 @@
import 'package:cached_network_image/cached_network_image.dart'; import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:media_kit/media_kit.dart'; import 'package:media_kit/media_kit.dart';
import 'package:media_kit_video/media_kit_video.dart'; import 'package:media_kit_video/media_kit_video.dart';
@ -83,7 +84,9 @@ class _AttachmentItemState extends State<AttachmentItem> {
onPressed: () { onPressed: () {
launchUrlString( launchUrlString(
ServiceFinder.buildUrl( ServiceFinder.buildUrl(
'files', '/attachments/${widget.item.id}'), 'files',
'/attachments/${widget.item.id}',
),
); );
}, },
), ),
@ -125,14 +128,27 @@ class _AttachmentItemImage extends StatelessWidget {
if (PlatformInfo.canCacheImage) if (PlatformInfo.canCacheImage)
CachedNetworkImage( CachedNetworkImage(
fit: fit, fit: fit,
imageUrl: imageUrl: ServiceFinder.buildUrl(
ServiceFinder.buildUrl('files', '/attachments/${item.id}'), 'files',
progressIndicatorBuilder: (context, url, downloadProgress) => '/attachments/${item.id}',
Center(
child: CircularProgressIndicator(
value: downloadProgress.progress,
),
), ),
progressIndicatorBuilder: (context, url, downloadProgress) {
return Center(
child: CircularProgressIndicator(
value: downloadProgress.progress,
),
);
},
errorWidget: (context, url, error) {
return Material(
color: Theme.of(context).colorScheme.surface,
child: Center(
child: const Icon(Icons.close, size: 32)
.animate(onPlay: (e) => e.repeat(reverse: true))
.fade(duration: 500.ms),
),
);
},
) )
else else
Image.network( Image.network(
@ -150,6 +166,16 @@ class _AttachmentItemImage extends StatelessWidget {
), ),
); );
}, },
errorBuilder: (context, error, stackTrace) {
return Material(
color: Theme.of(context).colorScheme.surface,
child: Center(
child: const Icon(Icons.close, size: 32)
.animate(onPlay: (e) => e.repeat(reverse: true))
.fade(duration: 500.ms),
),
);
},
), ),
if (showBadge && badge != null) if (showBadge && badge != null)
Positioned( Positioned(

View File

@ -4,6 +4,7 @@ import 'dart:ui';
import 'package:carousel_slider/carousel_slider.dart'; import 'package:carousel_slider/carousel_slider.dart';
import 'package:dismissible_page/dismissible_page.dart'; import 'package:dismissible_page/dismissible_page.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:solian/models/attachment.dart'; import 'package:solian/models/attachment.dart';
import 'package:solian/widgets/attachments/attachment_item.dart'; import 'package:solian/widgets/attachments/attachment_item.dart';
@ -43,7 +44,7 @@ class _AttachmentListState extends State<AttachmentList> {
List<Attachment?> _attachmentsMeta = List.empty(); List<Attachment?> _attachmentsMeta = List.empty();
void getMetadataList() { void _getMetadataList() {
final AttachmentProvider provider = Get.find(); final AttachmentProvider provider = Get.find();
if (widget.attachmentsId.isEmpty) { if (widget.attachmentsId.isEmpty) {
@ -56,8 +57,8 @@ class _AttachmentListState extends State<AttachmentList> {
for (var idx = 0; idx < widget.attachmentsId.length; idx++) { for (var idx = 0; idx < widget.attachmentsId.length; idx++) {
provider.getMetadata(widget.attachmentsId[idx]).then((resp) { provider.getMetadata(widget.attachmentsId[idx]).then((resp) {
progress++; progress++;
if (resp.body != null) { if (resp != null) {
_attachmentsMeta[idx] = Attachment.fromJson(resp.body); _attachmentsMeta[idx] = resp;
} }
if (progress == widget.attachmentsId.length) { if (progress == widget.attachmentsId.length) {
calculateAspectRatio(); calculateAspectRatio();
@ -125,7 +126,7 @@ class _AttachmentListState extends State<AttachmentList> {
@override @override
void initState() { void initState() {
super.initState(); super.initState();
getMetadataList(); _getMetadataList();
} }
@override @override
@ -234,19 +235,13 @@ class AttachmentListEntry extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
if (item == null) { if (item == null) {
return Center( return Center(
child: Container( child: Icon(
constraints: const BoxConstraints(maxWidth: 280), Icons.close,
child: Column( size: 32,
mainAxisAlignment: MainAxisAlignment.center, color: Theme.of(context).colorScheme.onSurface,
children: [ )
Icon( .animate(onPlay: (e) => e.repeat(reverse: true))
Icons.close, .fade(duration: 500.ms),
size: 32,
color: Theme.of(context).colorScheme.onSurface,
),
],
),
),
); );
} }

View File

@ -226,7 +226,7 @@ class _AttachmentPublishPopupState extends State<AttachmentPublishPopup> {
for (var idx = 0; idx < widget.current.length; idx++) { for (var idx = 0; idx < widget.current.length; idx++) {
provider.getMetadata(widget.current[idx]).then((resp) { provider.getMetadata(widget.current[idx]).then((resp) {
progress++; progress++;
_attachments[idx] = Attachment.fromJson(resp.body); _attachments[idx] = resp;
if (progress == widget.current.length) { if (progress == widget.current.length) {
setState(() { setState(() {
_isBusy = false; _isBusy = false;

View File

@ -2,7 +2,7 @@ name: solian
description: "The Solar Network App" description: "The Solar Network App"
publish_to: "none" publish_to: "none"
version: 1.1.0+50 version: 1.2.0
environment: environment:
sdk: ">=3.3.4 <4.0.0" sdk: ">=3.3.4 <4.0.0"