diff --git a/lib/src/http/request/request_file.dart b/lib/src/http/request/request_file.dart index c168de2..553e9bf 100644 --- a/lib/src/http/request/request_file.dart +++ b/lib/src/http/request/request_file.dart @@ -56,10 +56,10 @@ class RequestFile { /// Store the file via your Storage layer. /// - `destPath` should include trailing slash if desired. Future store({ - required String destPath, + String path = '', required String name, }) => - Storage.put(destPath, name, stream); + Storage.put(path, name, stream); /// Move the file into a local path on disk. /// Creates directories as needed. diff --git a/lib/src/storage/storage.dart b/lib/src/storage/storage.dart index 6450fb5..6142160 100644 --- a/lib/src/storage/storage.dart +++ b/lib/src/storage/storage.dart @@ -6,6 +6,7 @@ import 'package:vania/src/storage/s3_storage.dart'; import 'storage_driver.dart'; import 'package:vania/src/utils/helper.dart' show env; +import 'package:path/path.dart' as path; class Storage { static final Storage _singleton = Storage._internal(); @@ -38,18 +39,31 @@ class Storage { return await Storage()._driver.json(file); } - static Future put(String directory, String file, dynamic content) { + static Future put( + String directory, + String file, + dynamic content, + ) async { if (content == null) { throw Exception("Content can't be null"); } - if (!(content is List || content is String)) { - throw Exception('Content must be a list of int or a string.'); - } + String fullPath = path.join(directory, file); - directory = directory.endsWith("/") ? directory : "$directory/"; - String path = '$directory$file'; - return Storage()._driver.put(path, content); + if (content is List) { + return Storage()._driver.put(fullPath, content); + } else if (content is String) { + return Storage()._driver.put(fullPath, content); + } else if (content is Stream>) { + final data = await content.fold>([], (previous, element) { + previous.addAll(element); + return previous; + }); + return Storage()._driver.put(fullPath, data); + } else { + throw Exception( + 'Content must be a list of int, a string, or a Stream>.'); + } } static Future mimeType(String file) async {