Skip to content

Commit

Permalink
🚀 Merge #299: Televerse v1.26.4
Browse files Browse the repository at this point in the history
📁 Multipart File to Local File
  • Loading branch information
HeySreelal authored Oct 4, 2024
2 parents db6753a + 7cc4497 commit 86b183e
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 13 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 1.26.4

- Added `LocalFile` class.
- Redefined `Payload.files` parameter with the `LocalFile` objects instead of `MultipartFile` from Dio.

# 1.26.3

- 🐞 Fix: Webhook Bots crashed when an invalid body was present in the request.
Expand Down
10 changes: 5 additions & 5 deletions lib/src/televerse/api/raw_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,14 @@ class RawAPI {
///
/// A new list of maps representing multipart files. The list will be empty if
/// no matching elements are found in the input list.
List<Map<String, MultipartFile>> _getFiles(List<_MultipartHelper> list) {
List<Map<String, MultipartFile>> files = list.where((el) {
List<Map<String, LocalFile>> _getFiles(List<_MultipartHelper> list) {
List<Map<String, LocalFile>> files = list.where((el) {
return el.type == InputFileType.bytes;
}).map((e) {
return {
e.field: MultipartFile.fromBytes(
e.field: LocalFile(
e.file.getBytes(),
filename: e.name,
fileName: e.name,
),
};
}).toList();
Expand Down Expand Up @@ -3034,7 +3034,7 @@ class RawAPI {
"format": format.value,
};

List<Map<String, MultipartFile>>? files;
List<Map<String, LocalFile>>? files;

if (thumbnail != null) {
files = _getFiles([
Expand Down
24 changes: 24 additions & 0 deletions lib/src/televerse/models/local_file.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
part of 'models.dart';

/// The File to be uploaded.
class LocalFile {
/// The file name of the file.
final String? fileName;

/// The actual bytes of the file.
final Uint8List bytes;

/// MIME content type for the file
final String? contentType;

/// Additional headers
final Map<String, List<String>>? headers;

/// Constructs the Local File with the given bytes, and file name.
const LocalFile(
this.bytes, {
this.fileName,
this.contentType,
this.headers,
});
}
3 changes: 2 additions & 1 deletion lib/src/televerse/models/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'dart:async';
import 'dart:io' as io;
import 'dart:typed_data';

import 'package:dio/dio.dart' show DioException, MultipartFile;
import 'package:dio/dio.dart' show DioException;

import 'package:televerse/telegram.dart';
import 'package:televerse/televerse.dart' hide HandlerScope;
Expand All @@ -24,3 +24,4 @@ part 'payload.dart';
part 'telegram_exception.dart';
part 'televerse_exception.dart';
part 'webhook_exception.dart';
part 'local_file.dart';
10 changes: 5 additions & 5 deletions lib/src/televerse/models/payload.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ part of 'models.dart';
/// Represents a list of maps containing key-value pairs for multipart files.
///
/// Each map associates a field key (a `String`) with its corresponding
/// `MultipartFile` object. This type is used to represent a collection
/// `LocalFile` object. This type is used to represent a collection
/// of files to be uploaded in a multipart request.
typedef PayloadFiles = List<Map<String, MultipartFile>>;
typedef PayloadFiles = List<Map<String, LocalFile>>;

/// Represents a payload object used for making API calls.
///
Expand All @@ -20,7 +20,7 @@ typedef PayloadFiles = List<Map<String, MultipartFile>>;
/// request to the API.
/// * [files]: A list of maps containing key-value pairs for multipart files.
/// Each map associates a field key (a `String`) with its corresponding
/// `MultipartFile` object. This type is typically used to represent a collection
/// `LocalFile` object. This type is typically used to represent a collection
/// of files to be uploaded in a multipart request.
///
class Payload {
Expand All @@ -34,7 +34,7 @@ class Payload {
/// Represents a list of maps containing key-value pairs for multipart files.
///
/// Each map associates a field key (a `String`) with its corresponding
/// `MultipartFile` object. This type is used to represent a collection
/// `LocalFile` object. This type is used to represent a collection
/// of files to be uploaded in a multipart request.
PayloadFiles? files;

Expand All @@ -51,7 +51,7 @@ class Payload {
/// request to the API. (See [Payload.params] for details)
/// * [files]: A list of maps containing key-value pairs for multipart files.
/// Each map associates a field key (a `String`) with its corresponding
/// `MultipartFile` object. This type is typically used to represent a collection
/// `LocalFile` object. This type is typically used to represent a collection
/// of files to be uploaded in a multipart request. (See [Payload.files] for details)
Payload([Map<String, dynamic>? params, this.files]) : params = params ?? {};

Expand Down
19 changes: 18 additions & 1 deletion lib/src/utils/http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ class _HttpClient {
final filesMap = payload.files!.expand((element) => element.entries);
final formData = FormData()
..fields.addAll(parameters)
..files.addAll(filesMap);
..files.addAll(
filesMap.map((e) => MapEntry(e.key, _toMultipartFile(e.value))),
);

try {
final req = await _dio.postUri(
Expand Down Expand Up @@ -138,4 +140,19 @@ class _HttpClient {
void close() {
_dio.close();
}

/// Converts the [LocalFile] into a MultipartFile for sending along with the request.
/// Converts the file into a `MultipartFile` instance
MultipartFile _toMultipartFile(LocalFile file) {
return MultipartFile.fromBytes(
file.bytes,
filename: file.fileName,
contentType: file.contentType != null
? DioMediaType.parse(
file.contentType!,
)
: null,
headers: file.headers,
);
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: televerse
description: Televerse lets you create your own efficient Telegram bots with ease in Dart. Supports latest Telegram Bot API - 7.10!
version: 1.26.3
version: 1.26.4
homepage: https://televerse.xooniverse.com
repository: https://github.com/xooniverse/televerse
topics:
Expand Down

0 comments on commit 86b183e

Please sign in to comment.