Skip to content

Commit b8a0603

Browse files
committed
Add toRelativePath method to file system
1 parent 7a5ca7e commit b8a0603

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

packages/lw_file_system/lib/src/api/file_system_base.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@ abstract class GeneralFileSystem {
104104
return variant;
105105
}
106106

107+
Future<String?> toRelativePath(String path) async {
108+
final root = await getDirectory();
109+
if (universalPathContext.isRelative(path)) {
110+
return normalizePath(path);
111+
}
112+
if (universalPathContext.isWithin(root, path)) {
113+
return universalPathContext.relative(path, from: root);
114+
}
115+
return null;
116+
}
117+
107118
Future<Uint8List?> loadAbsolute(String path) => Future.value(null);
108119

109120
Future<void> saveAbsolute(String path, Uint8List bytes) => Future.value();

packages/lw_file_system/lib/src/api/file_system_html.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ class WebDirectoryFileSystem extends DirectoryFileSystem with WebFileSystem {
188188
return doc != null;
189189
}
190190

191+
@override
192+
Future<String?> toRelativePath(String path) => Future.value(null);
193+
191194
@override
192195
Future<bool> updateFile(
193196
String path,

packages/lw_file_system/lib/src/api/file_system_io.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import 'dart:io';
33

44
import 'package:flutter/foundation.dart';
55
import 'package:lw_file_system/lw_file_system.dart';
6-
import 'package:path/path.dart' as p;
76
import 'package:synchronized/synchronized.dart';
87

98
Future<void> _updateFile((String, Uint8List) e) async {
@@ -134,7 +133,7 @@ class IODirectoryFileSystem extends DirectoryFileSystem {
134133
(await directory.list(followLinks: false).toList()).map((e) async {
135134
final current = universalPathContext.join(
136135
path,
137-
p.relative(e.path, from: absolutePath),
136+
universalPathContext.relative(e.path, from: absolutePath),
138137
);
139138
if (e is File) {
140139
return RawFileSystemFile(

0 commit comments

Comments
 (0)