saveToPublicDir method
Saves the file into a subdirectory of the public storage path.
When autoName is true, a UUID-based filename is generated while
preserving the original extension when possible.
Returns the written File.
Example:
final saved = await file.saveToPublicDir('avatars');
print(saved.path);
Implementation
Future<File> saveToPublicDir(String subDir, {bool autoName = true}) async {
final String fileName;
if (autoName) {
final ext = extension.isNotEmpty ? '.$extension' : '';
final uuid = Uuid().v4();
fileName = '$uuid$ext';
} else {
fileName = filename;
}
final path = p.join('lib/src/http/public', subDir, fileName);
final file = File(path);
if (!await file.parent.exists()) {
await file.parent.create(recursive: true);
}
final bytes = await _cachedBytes;
await file.writeAsBytes(bytes);
return file;
}