Line data Source code
1 : import 'dart:io';
2 : import 'package:cross_file/cross_file.dart';
3 : import 'package:flutter/foundation.dart';
4 : import 'package:media_source/src/utils/platform_utils.dart';
5 :
6 : /// IO implementation of the [PlatformUtilsFacade].
7 : ///
8 : /// Provides concrete file system operations for native platforms (mobile,
9 : /// desktop). All methods are written to be resilient: they catch exceptions
10 : /// and return boolean success indicators where appropriate.
11 : class PlatformUtilsFacadeImpl implements PlatformUtilsFacade {
12 : /// Deletes the provided [XFile] from disk.
13 : ///
14 : /// Returns `true` when deletion is successful, `false` otherwise.
15 0 : @override
16 : Future<bool> deleteFile(XFile file) async {
17 : try {
18 0 : if (!await fileExists(file)) return false;
19 :
20 0 : final fileToDelete = File(file.path);
21 0 : await fileToDelete.delete();
22 : return true;
23 : } catch (e) {
24 : return false;
25 : }
26 : }
27 :
28 : /// Ensures the parent directory of [directoryPath] exists; creates it if missing.
29 1 : @override
30 : Future<void> createDirectoryIfNotExists(String directoryPath) async {
31 2 : final directory = Directory(directoryPath).parent;
32 1 : if (!await directory.exists()) {
33 1 : await directory.create(recursive: true);
34 : }
35 : }
36 :
37 : /// Returns `true` when the directory exists on disk.
38 : ///
39 : /// Visible for testing to allow unit tests to verify environment state.
40 0 : @override
41 : @visibleForTesting
42 : Future<bool> directoryExists(String directoryPath) async {
43 0 : final directory = Directory(directoryPath);
44 0 : return directory.exists();
45 : }
46 :
47 : /// Deletes the directory recursively; returns `true` on success.
48 : ///
49 : /// Visible for testing to allow test teardown of created resources.
50 1 : @override
51 : @visibleForTesting
52 : Future<bool> deleteDirectory(String directoryPath) async {
53 : try {
54 1 : final directory = Directory(directoryPath);
55 1 : if (!directory.existsSync()) return false;
56 :
57 1 : directory.deleteSync(recursive: true);
58 : return true;
59 : } catch (e) {
60 : return false;
61 : }
62 : }
63 :
64 : /// Returns `true` if the file exists on disk.
65 1 : @override
66 : Future<bool> fileExists(XFile file) async {
67 : try {
68 1 : final path = file.path;
69 1 : if (path.isEmpty) return false; // If path is empty, the file cannot exist
70 2 : return File(path).exists();
71 : } catch (e) {
72 : return false;
73 : }
74 : }
75 : }
|