-
Notifications
You must be signed in to change notification settings - Fork 1
Tony/153 refactor file browsing code #198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tonypioneer
wants to merge
11
commits into
dev
Choose a base branch
from
tony/153_refactor_file_browsing_code
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6ef5cad
Fix the issue that uploaded file appears in the unexpected directory
tonypioneer 9583f73
Refactor file-browsing code built upon basePath
tonypioneer 2ccc3bc
Lint
tonypioneer 4aca698
Merge branch 'dev' of https://github.com/anusii/solidui into tony/153…
tonypioneer b6e5689
Use `pathType: PathType.relativeToPod` instead of `pathType: pathType…
tonypioneer 3c43e1c
Remove basePath in utils folder
tonypioneer fe24889
Remove basePath in widgets folder
tonypioneer 00a4e53
Remove basePath in widgets folder
tonypioneer b96c7bc
Lint
tonypioneer 33dba75
Ensure the security key is available before writing encrypted data
tonypioneer f6ba52f
Merge branch 'dev' of https://github.com/anusii/solidui into tony/153…
tonypioneer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| /// Path utilities for SolidUI. | ||
| /// | ||
| /// Copyright (C) 2026, Software Innovation Institute, ANU. | ||
| /// | ||
| /// Licensed under the MIT License (the "License"). | ||
| /// | ||
| /// License: https://choosealicense.com/licenses/mit/. | ||
| // | ||
| // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| // of this software and associated documentation files (the "Software"), to deal | ||
| // in the Software without restriction, including without limitation the rights | ||
| // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| // copies of the Software, and to permit persons to whom the Software is | ||
| // furnished to do so, subject to the following conditions: | ||
| // | ||
| // The above copyright notice and this permission notice shall be included in | ||
| // all copies or substantial portions of the Software. | ||
| // | ||
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| // SOFTWARE. | ||
| /// | ||
| /// Authors: Tony Chen | ||
|
|
||
| library; | ||
|
|
||
| /// Utility class for path operations in SolidUI. | ||
| /// | ||
| /// This class provides methods to normalise and manipulate paths used in | ||
| /// file browsing operations. All paths are treated as relative to the Pod | ||
| /// root and should not have leading forward slashes. | ||
|
|
||
| class PathUtils { | ||
| const PathUtils._(); | ||
|
|
||
| /// Normalises a relative path by removing leading and trailing slashes. | ||
| /// | ||
| /// This ensures paths are consistently formatted for use with solidpod's | ||
| /// `PathType.relativeToPod` option, which expects paths without leading | ||
| /// slashes. | ||
| /// | ||
| /// Examples: | ||
| /// - `/myapp/data` becomes `myapp/data` | ||
| /// - `myapp/data/` becomes `myapp/data` | ||
| /// - `//myapp//data//` becomes `myapp/data` | ||
| /// - `` remains `` | ||
| /// - `/` becomes `` | ||
|
|
||
| static String normalise(String path) { | ||
| if (path.isEmpty) return ''; | ||
|
|
||
| // Remove leading slashes. | ||
|
|
||
| String normalised = path; | ||
| while (normalised.startsWith('/')) { | ||
| normalised = normalised.substring(1); | ||
| } | ||
|
|
||
| // Remove trailing slashes. | ||
|
|
||
| while (normalised.endsWith('/')) { | ||
| normalised = normalised.substring(0, normalised.length - 1); | ||
| } | ||
|
|
||
| // Remove consecutive slashes. | ||
|
|
||
| normalised = normalised.replaceAll(RegExp(r'/+'), '/'); | ||
|
|
||
| return normalised; | ||
| } | ||
|
|
||
| /// Joins path segments into a normalised path. | ||
| /// | ||
| /// All segments are normalised and empty segments are filtered out. | ||
| /// | ||
| /// Examples: | ||
| /// - join(['myapp', 'data', 'file.ttl']) returns `myapp/data/file.ttl` | ||
| /// - join(['/myapp/', '/data/', 'file.ttl']) returns `myapp/data/file.ttl` | ||
| /// - join(['', 'myapp', '', 'data']) returns `myapp/data` | ||
|
|
||
| static String join(List<String> segments) { | ||
| final normalisedSegments = | ||
| segments.map(normalise).where((s) => s.isNotEmpty).toList(); | ||
|
|
||
| return normalisedSegments.join('/'); | ||
| } | ||
|
|
||
| /// Extracts the relative path from a full path given a root path. | ||
| /// | ||
| /// Both paths are normalised before comparison. If the full path does not | ||
| /// start with the root path, the full normalised path is returned. | ||
| /// | ||
| /// Examples: | ||
| /// - relativeTo('myapp/data/subfolder', 'myapp/data') returns `subfolder` | ||
| /// - relativeTo('/myapp/data/subfolder', '/myapp/data') returns `subfolder` | ||
| /// - relativeTo('myapp/data', 'myapp/data') returns `` | ||
| /// - relativeTo('other/path', 'myapp/data') returns `other/path` | ||
|
|
||
| static String relativeTo(String fullPath, String rootPath) { | ||
| final normalisedFull = normalise(fullPath); | ||
| final normalisedRoot = normalise(rootPath); | ||
|
|
||
| if (normalisedRoot.isEmpty) { | ||
| return normalisedFull; | ||
| } | ||
|
|
||
| if (normalisedFull == normalisedRoot) { | ||
| return ''; | ||
| } | ||
|
|
||
| if (normalisedFull.startsWith('$normalisedRoot/')) { | ||
| return normalisedFull.substring(normalisedRoot.length + 1); | ||
| } | ||
|
|
||
| return normalisedFull; | ||
| } | ||
|
|
||
| /// Combines a directory path and a file name into a full path. | ||
| /// | ||
| /// Both are normalised before joining. | ||
| /// | ||
| /// Examples: | ||
| /// - combine('myapp/data', 'file.ttl') returns `myapp/data/file.ttl` | ||
| /// - combine('/myapp/data/', '/file.ttl') returns `myapp/data/file.ttl` | ||
| /// - combine('', 'file.ttl') returns `file.ttl` | ||
|
|
||
| static String combine(String directoryPath, String fileName) { | ||
| return join([directoryPath, fileName]); | ||
| } | ||
|
|
||
| /// Checks if a path is the root (empty or just slashes). | ||
| /// | ||
| /// Examples: | ||
| /// - isRoot('') returns true | ||
| /// - isRoot('/') returns true | ||
| /// - isRoot('//') returns true | ||
| /// - isRoot('myapp') returns false | ||
|
|
||
| static bool isRoot(String path) { | ||
| return normalise(path).isEmpty; | ||
| } | ||
|
|
||
| /// Gets the parent directory of a path. | ||
| /// | ||
| /// Returns empty string if the path has no parent (is root or single | ||
| /// segment). | ||
| /// | ||
| /// Examples: | ||
| /// - parent('myapp/data/subfolder') returns `myapp/data` | ||
| /// - parent('myapp') returns `` | ||
| /// - parent('') returns `` | ||
|
|
||
| static String parent(String path) { | ||
| final normalised = normalise(path); | ||
| final lastSlash = normalised.lastIndexOf('/'); | ||
| if (lastSlash == -1) { | ||
| return ''; | ||
| } | ||
| return normalised.substring(0, lastSlash); | ||
| } | ||
|
|
||
| /// Gets the last segment (file or directory name) of a path. | ||
| /// | ||
| /// Examples: | ||
| /// - basename('myapp/data/file.ttl') returns `file.ttl` | ||
| /// - basename('myapp') returns `myapp` | ||
| /// - basename('') returns `` | ||
|
|
||
| static String basename(String path) { | ||
| final normalised = normalise(path); | ||
| final lastSlash = normalised.lastIndexOf('/'); | ||
| if (lastSlash == -1) { | ||
| return normalised; | ||
| } | ||
| return normalised.substring(lastSlash + 1); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.