Skip to content

Commit

Permalink
Make attachment implement CdnAsset
Browse files Browse the repository at this point in the history
  • Loading branch information
abitofevrything committed Sep 16, 2023
1 parent cca3200 commit bd4e68f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/src/http/managers/message_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class MessageManager extends Manager<Message> {
Attachment parseAttachment(Map<String, Object?> raw) {
return Attachment(
id: Snowflake.parse(raw['id']!),
manager: this,
fileName: raw['filename'] as String,
description: raw['description'] as String?,
contentType: raw['content_type'] as String?,
Expand Down
53 changes: 51 additions & 2 deletions lib/src/models/message/attachment.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import 'package:nyxx/nyxx.dart';
import 'dart:typed_data';

import 'package:http/http.dart';
import 'package:nyxx/src/client.dart';
import 'package:nyxx/src/http/cdn/cdn_asset.dart';
import 'package:nyxx/src/http/managers/message_manager.dart';
import 'package:nyxx/src/http/route.dart';
import 'package:nyxx/src/models/snowflake.dart';
import 'package:nyxx/src/utils/to_string_helper/to_string_helper.dart';

/// {@template attachment}
/// An attachment in a [Message].
///
/// Note that although this class implements [CdnAsset], not all operations are supported. Notably, [CdnFormat]s and sizes are not supported.
///
/// External references:
/// * Discord API Reference: https://discord.com/developers/docs/resources/channel#attachment-object
/// {@endtemplate}
class Attachment with ToStringHelper {
class Attachment with ToStringHelper implements CdnAsset {
/// The manager for this [Attachment].
final MessageManager manager;

/// This attachment's ID.
final Snowflake id;

Expand All @@ -24,6 +36,7 @@ class Attachment with ToStringHelper {
final int size;

/// A URL from which the attached file can be downloaded.
@override
final Uri url;

/// A proxied URL from which the attached file can be downloaded.
Expand All @@ -38,9 +51,25 @@ class Attachment with ToStringHelper {
/// Whether this attachment is ephemeral.
final bool isEphemeral;

@override
Nyxx get client => manager.client;

@override
String get hash => fileName;

@override
HttpRoute get base => HttpRoute()..parts.addAll(proxiedUrl.pathSegments.take(proxiedUrl.pathSegments.length - 1).map((part) => HttpRoutePart(part)));

@override
CdnFormat get defaultFormat => throw UnsupportedError('Cannot get attachment format');

@override
bool get isAnimated => false;

/// {@macro attachment}
Attachment({
required this.id,
required this.manager,
required this.fileName,
required this.description,
required this.contentType,
Expand All @@ -51,4 +80,24 @@ class Attachment with ToStringHelper {
required this.width,
required this.isEphemeral,
});

@override
Future<Uint8List> fetch({CdnFormat? format, int? size}) async {
if (format != null || size != null) {
throw UnsupportedError('Cannot specify attachment format or size');
}

final response = await client.httpHandler.httpClient.get(url);
return response.bodyBytes;
}

@override
Stream<List<int>> fetchStreamed({CdnFormat? format, int? size}) async* {
if (format != null || size != null) {
throw UnsupportedError('Cannot specify attachment format or size');
}

final response = await client.httpHandler.httpClient.send(Request('GET', url));
yield* response.stream;
}
}
3 changes: 3 additions & 0 deletions test/integration/rest_integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ void main() {
expect(message.attachments, hasLength(1));
expect(message.attachments.first.fileName, equals('1.png'));

await expectLater(message.attachments.first.fetch(), completes);
await expectLater(message.attachments.first.fetchStreamed().drain(), completes);

await expectLater(message.delete(), completes);

await expectLater(
Expand Down

0 comments on commit bd4e68f

Please sign in to comment.