split static method
Extract the baseDirectory, directory and filename from
the provided filePath
or file
, and return this as a record
Either filePath
or file
must be provided, not both.
Throws a FileSystemException if using external storage on Android (via configuration at startup), and external storage is not available.
Implementation
static Future<
(BaseDirectory baseDirectory, String directory, String filename)>
split({String? filePath, File? file}) async {
assert((filePath != null) ^ (file != null),
'Either filePath or file must be given and not both');
final path = filePath ?? file!.absolute.path;
final absoluteDirectoryPath = p.dirname(path);
final filename = p.basename(path);
// try to match the start of the absoluteDirectory to one of the
// directories represented by the BaseDirectory enum.
// Order matters, as some may be subdirs of others
final testSequence = Platform.isAndroid || Platform.isLinux
? [
BaseDirectory.temporary,
BaseDirectory.applicationLibrary,
BaseDirectory.applicationSupport,
BaseDirectory.applicationDocuments
]
: [
BaseDirectory.temporary,
BaseDirectory.applicationSupport,
BaseDirectory.applicationLibrary,
BaseDirectory.applicationDocuments
];
for (final baseDirectoryEnum in testSequence) {
final baseDirPath = await baseDirectoryPath(baseDirectoryEnum);
final (match, directory) = _contains(baseDirPath, absoluteDirectoryPath);
if (match) {
return (baseDirectoryEnum, directory, filename);
}
}
// if no match, return a BaseDirectory.root with the absoluteDirectory
// minus the leading characters that designate the root (differs by platform)
final match =
RegExp(r'^(/|\\|([a-zA-Z]:[\\/]))').firstMatch(absoluteDirectoryPath);
return (
BaseDirectory.root,
absoluteDirectoryPath.substring(match?.end ?? 0),
filename
);
}