Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/src/http/request/request_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ class RequestFile {
/// Store the file via your Storage layer.
/// - `destPath` should include trailing slash if desired.
Future<String> 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.
Expand Down
28 changes: 21 additions & 7 deletions lib/src/storage/storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -38,18 +39,31 @@ class Storage {
return await Storage()._driver.json(file);
}

static Future<String> put(String directory, String file, dynamic content) {
static Future<String> put(
String directory,
String file,
dynamic content,
) async {
if (content == null) {
throw Exception("Content can't be null");
}

if (!(content is List<int> || 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<int>) {
return Storage()._driver.put(fullPath, content);
} else if (content is String) {
return Storage()._driver.put(fullPath, content);
} else if (content is Stream<List<int>>) {
final data = await content.fold<List<int>>([], (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<List<int>>.');
}
}

static Future<String?> mimeType(String file) async {
Expand Down