mirror of
https://github.com/foss42/apidash.git
synced 2025-06-28 11:27:06 +08:00
Add new file and path utilities
This commit is contained in:
48
lib/utils/file_utils.dart
Normal file
48
lib/utils/file_utils.dart
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
import 'dart:io';
|
||||||
|
import 'dart:typed_data';
|
||||||
|
import 'package:path/path.dart' as p;
|
||||||
|
import 'package:mime_dart/mime_dart.dart';
|
||||||
|
import 'package:uuid/uuid.dart';
|
||||||
|
import 'package:path_provider/path_provider.dart';
|
||||||
|
|
||||||
|
const uuid = Uuid();
|
||||||
|
|
||||||
|
String? getFileExtension(String? mimeType) {
|
||||||
|
if (mimeType == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return Mime.getExtensionsFromType(mimeType)?[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<String?> getFileDownloadpath(String? name, String? ext) async {
|
||||||
|
final Directory? downloadsDir = await getDownloadsDirectory();
|
||||||
|
if (downloadsDir != null) {
|
||||||
|
name = name ?? getTempFileName();
|
||||||
|
ext = (ext != null) ? ".$ext" : "";
|
||||||
|
String path = '${downloadsDir.path}/$name$ext';
|
||||||
|
int num = 1;
|
||||||
|
while (await File(path).exists()) {
|
||||||
|
path = '${downloadsDir.path}/$name (${num++})$ext';
|
||||||
|
}
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> saveFile(String path, Uint8List content) async {
|
||||||
|
final file = File(path);
|
||||||
|
await file.writeAsBytes(content);
|
||||||
|
}
|
||||||
|
|
||||||
|
String getShortPath(String path) {
|
||||||
|
var f = p.split(path);
|
||||||
|
if (f.length > 2) {
|
||||||
|
f = f.sublist(f.length - 2);
|
||||||
|
return ".../${p.joinAll(f)}";
|
||||||
|
}
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
String getTempFileName() {
|
||||||
|
return uuid.v1();
|
||||||
|
}
|
@ -1,3 +1,4 @@
|
|||||||
export 'ui_utils.dart';
|
export 'ui_utils.dart';
|
||||||
export 'convert_utils.dart';
|
export 'convert_utils.dart';
|
||||||
export 'http_utils.dart';
|
export 'http_utils.dart';
|
||||||
|
export 'file_utils.dart';
|
||||||
|
Reference in New Issue
Block a user