2021-05-23 21:11:06 +02:00
|
|
|
import 'dart:io';
|
|
|
|
import 'dart:typed_data';
|
|
|
|
|
|
|
|
import 'package:nc_photos/platform/universal_storage.dart' as itf;
|
2023-02-23 15:49:17 +01:00
|
|
|
import 'package:np_common/string_extension.dart';
|
2022-01-31 10:47:37 +01:00
|
|
|
import 'package:path/path.dart' as path_lib;
|
2021-05-23 21:11:06 +02:00
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
|
|
|
|
class UniversalStorage extends itf.UniversalStorage {
|
|
|
|
@override
|
|
|
|
putBinary(String name, Uint8List content) async {
|
|
|
|
final storageDir = await _openStorageDirForFile(name);
|
|
|
|
final file = File("${storageDir.path}/$name");
|
|
|
|
await file.writeAsBytes(content, flush: true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
getBinary(String name) async {
|
|
|
|
final storageDir = await _openStorageDirForFile(name);
|
|
|
|
final file = File("${storageDir.path}/$name");
|
|
|
|
if (await file.exists()) {
|
|
|
|
return await file.readAsBytes();
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
putString(String name, String content) async {
|
|
|
|
final storageDir = await _openStorageDirForFile(name);
|
|
|
|
final file = File("${storageDir.path}/$name");
|
|
|
|
await file.writeAsString(content, flush: true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
getString(String name) async {
|
|
|
|
final storageDir = await _openStorageDirForFile(name);
|
|
|
|
final file = File("${storageDir.path}/$name");
|
|
|
|
if (await file.exists()) {
|
|
|
|
return await file.readAsString();
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
remove(String name) async {
|
|
|
|
final storageDir = await _openStorageDirForFile(name);
|
|
|
|
final file = File("${storageDir.path}/$name");
|
|
|
|
if (await file.exists()) {
|
|
|
|
await file.delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Open and return the directory for storing a file to [relativePath]
|
|
|
|
Future<Directory> _openStorageDirForFile(String relativePath) async {
|
|
|
|
final privateDir = await getApplicationSupportDirectory();
|
|
|
|
final rootPath = "${privateDir.path}/universal_storage";
|
2022-01-31 10:47:37 +01:00
|
|
|
final dirPath = path_lib.dirname("$rootPath/${relativePath.trimAny('/')}");
|
2021-05-23 21:11:06 +02:00
|
|
|
final storageDir = Directory(dirPath);
|
|
|
|
if (!await storageDir.exists()) {
|
|
|
|
await storageDir.create(recursive: true);
|
|
|
|
}
|
|
|
|
return Directory(rootPath);
|
|
|
|
}
|
|
|
|
}
|