-
Notifications
You must be signed in to change notification settings - Fork 10
306 rework attachments in interaction for better dev #308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
PandaGuerrier
merged 9 commits into
main
from
306-rework-attachments-in-interaction-for-better-dev
Nov 10, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cd3c62f
feat: deleted message media, who isn't used (use Asset instead)
PandaGuerrier 4519b60
feat: Use Asset
PandaGuerrier 2568678
feat: created general asset for img uses
PandaGuerrier ab4d91b
feat: export Asset
PandaGuerrier 2df4391
feat: rework this func
PandaGuerrier 1c87502
feat: renamed to MessageFile
PandaGuerrier 74ab7dd
feat: renamed to MediaItem (like discord API)
PandaGuerrier a8a2019
feat: renamed to media instead of asset
PandaGuerrier ec5df02
feat: mixed message_file.dart and media_item.dart :)
PandaGuerrier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import 'dart:io'; | ||
| import 'dart:typed_data'; | ||
|
|
||
| final class MediaItem { | ||
| Uint8List? bytes; | ||
| final String url; | ||
| bool? spoiler; | ||
| final String? proxyUrl; | ||
| final int? height; | ||
| final int? width; | ||
| final String? contentType; | ||
| final String? description; | ||
|
|
||
| MediaItem(this.url, {this.spoiler, this.proxyUrl, this.height, this.width, this.contentType, this.description}); | ||
|
|
||
| factory MediaItem.file(File file, String name, {bool? spoiler, String? proxyUrl, int? height, int? width, String? contentType, String? description}) { | ||
| if (!file.existsSync()) { | ||
| throw ArgumentError('File ${file.path} does not exist'); | ||
| } | ||
|
|
||
| if (name.isEmpty) { | ||
| throw ArgumentError("Name can't be empty."); | ||
| } | ||
|
|
||
| final bytes = file.readAsBytesSync(); | ||
|
|
||
| return MediaItem( | ||
| 'attachment://$name', | ||
| spoiler: spoiler, | ||
| proxyUrl: proxyUrl, | ||
| height: height, | ||
| width: width, | ||
| contentType: contentType, | ||
| description: description, | ||
| )..bytes = bytes; | ||
| } | ||
|
|
||
| Map<String, dynamic> toJson() { | ||
| return { | ||
| if (description != null) 'description': description, | ||
| 'url': url, | ||
| if (bytes != null) 'bytes': bytes!, | ||
| if (spoiler != null) 'spoiler': spoiler, | ||
| 'media': { | ||
| 'url': url, | ||
| if (proxyUrl != null) 'proxy_url': proxyUrl, | ||
| if (height != null) 'height': height, | ||
| if (width != null) 'width': width, | ||
| if (contentType != null) 'content_type': contentType, | ||
| }, | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,56 +1,26 @@ | ||
| import 'dart:io'; | ||
| import 'dart:typed_data'; | ||
|
|
||
| import 'package:http/http.dart' as http; | ||
| import 'package:mineral/api.dart'; | ||
|
|
||
| final class Attachment implements Component { | ||
| final class MessageFile implements Component { | ||
| ComponentType get type => ComponentType.file; | ||
|
|
||
| static final Map<String, Uint8List> _cachedAttachments = {}; | ||
|
|
||
| Uint8List? bytes; | ||
| final String _path; | ||
| final bool? _spoiler; | ||
|
|
||
| Attachment(this._path, {bool? spoiler}) : _spoiler = spoiler; | ||
|
|
||
| factory Attachment.path(String path, {bool? spoiler, bool cache = false}) { | ||
| if (cache && _cachedAttachments.containsKey(path)) { | ||
| return Attachment(path, spoiler: spoiler) | ||
| ..bytes = _cachedAttachments[path]!; | ||
| } | ||
| MediaItem item; | ||
|
|
||
| final file = File(path); | ||
| final bytes = file.readAsBytesSync(); | ||
|
|
||
| return Attachment(path, spoiler: spoiler)..bytes = bytes; | ||
| } | ||
|
|
||
| static Future<Attachment> network(String url, | ||
| {bool? spoiler, bool cache = false}) async { | ||
| if (cache && _cachedAttachments.containsKey(url)) { | ||
| return Attachment(url, spoiler: spoiler) | ||
| ..bytes = _cachedAttachments[url]!; | ||
| } | ||
| MessageFile(this.item); | ||
|
|
||
| static Future<MessageFile> network(String url, {bool? spoiler}) async { | ||
| final uri = Uri.parse(url); | ||
| final response = await http.get(uri); | ||
| final name = uri.pathSegments.isNotEmpty ? uri.pathSegments.last : 'file.txt'; | ||
| final media = MediaItem('attachment://$name') | ||
| ..bytes = response.bodyBytes | ||
| ..spoiler = spoiler; | ||
|
|
||
| _cachedAttachments[url] = response.bodyBytes; | ||
|
|
||
| return Attachment(uri.path, spoiler: spoiler)..bytes = response.bodyBytes; | ||
| return MessageFile(media); | ||
| } | ||
|
|
||
| @override | ||
| Map<String, dynamic> toJson() { | ||
| return { | ||
| 'type': type.value, | ||
| 'file': { | ||
| 'url': _path, | ||
| 'bytes': bytes, | ||
| }, | ||
| if (_spoiler != null) 'spoiler': _spoiler, | ||
| }; | ||
| return {'type': type.value, ...item.toJson()}; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.