fileExists method
Returns a File with the provided filename
and size
, if any exist in
the downloadsDirectory.
If size
is null
, then an attempt to get the size from the given url
will be performed.
Implementation
Future<File?> fileExists(
String filename, {
int? size,
String? url,
bool temporary = false,
}) async {
if ((size != null || url != null) && !PlatformUtils.isWeb) {
size =
size ??
int.parse(
((await (await dio).head(url!)).headers['content-length']
as List<String>)[0],
);
final Directory directory =
temporary ? await temporaryDirectory : await downloadsDirectory;
String name = p.basenameWithoutExtension(filename);
String ext = p.extension(filename);
File file = File('${directory.path}/$filename');
// TODO: Compare hashes instead of sizes.
for (int i = 1; await file.exists() && await file.length() != size; ++i) {
file = File('${directory.path}/$name ($i)$ext');
}
if (await file.exists()) {
return file;
}
}
return null;
}