Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions packages/flet/lib/src/utils/images.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,21 @@ bool isBase64ImageString(String value) {

Copy link

Copilot AI Nov 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function isUrlOrPath lacks documentation explaining its purpose, parameters, return value, and expected behavior. Given the complexity of the new regex patterns and the specific exclusions mentioned in the PR description (URLs, base64, invalid characters), adding a doc comment would improve maintainability. For example:

/// Checks if the given [value] is a valid URL or file path.
/// 
/// Returns `true` if [value] matches:
/// - HTTP/HTTPS URLs (e.g., "https://example.com", "http://site.org")
/// - Windows paths (e.g., "C:\path\file.txt", "\\server\share\file")
/// - Unix paths (e.g., "/absolute/path", "~/home/file", "./relative")
/// - Simple filenames (e.g., "image.png", "document.txt")
/// 
/// Invalid file path characters (*, ?, ", <, >, |) are excluded.
/// Base64-encoded strings should be checked with [isBase64ImageString].
bool isUrlOrPath(String value) {
Suggested change
/// Checks if the given [value] is a valid URL or file path.
///
/// Returns `true` if [value] matches:
/// - HTTP/HTTPS URLs (e.g., "https://example.com", "http://site.org")
/// - URLs starting with "www."
/// - Windows paths (e.g., "C:\path\file.txt", "\\server\share\file")
/// - Unix paths (e.g., "/absolute/path", "~/home/file", "./relative")
/// - Simple filenames (e.g., "image.png", "document.txt")
///
/// Invalid file path characters (*, ?, ", <, >, |) are excluded.
/// Base64-encoded strings should be checked with [isBase64ImageString].

Copilot uses AI. Check for mistakes.
bool isUrlOrPath(String value) {
// Check for URL pattern
final urlPattern = RegExp(r'^(http:\/\/|https:\/\/|www\.)');
final urlPattern = RegExp(r'^(https?:\/\/|www\.)');
Copy link

Copilot AI Nov 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The URL pattern ^(https?:\/\/|www\.) will match strings like "www.example" but also "www." or "http://" on their own. Consider adding a pattern to ensure there's something after the protocol/prefix, such as ^(https?:\/\/.+|www\..+) to prevent matching incomplete URLs.

Suggested change
final urlPattern = RegExp(r'^(https?:\/\/|www\.)');
final urlPattern = RegExp(r'^(https?:\/\/.+|www\..+)');

Copilot uses AI. Check for mistakes.
if (urlPattern.hasMatch(value)) {
return true;
}

// Check for common file path characters
final filePathPattern = RegExp(r'^[a-zA-Z0-9_\-/\\\.]+$');
final filePathPattern = RegExp(
r'^('
r'([a-zA-Z]:\\|\\\\)?([^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*' // Windows paths
r'|' // OR
r'(~?\/|\.\/|\.\.\/)?([^\/:*?"<>|\r\n]+\/)*[^\/:*?"<>|\r\n]*' // Unix paths
Copy link

Copilot AI Nov 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Trailing whitespace detected at the end of the comment. Consider removing it for cleaner code.

Suggested change
r'(~?\/|\.\/|\.\.\/)?([^\/:*?"<>|\r\n]+\/)*[^\/:*?"<>|\r\n]*' // Unix paths
r'(~?\/|\.\/|\.\.\/)?([^\/:*?"<>|\r\n]+\/)*[^\/:*?"<>|\r\n]*' // Unix paths

Copilot uses AI. Check for mistakes.
r'|' // OR
r'[^\\/:*?"<>|\r\n]+' // Just filenames without path separators
r')$'
);
Comment on lines +100 to +108
Copy link

Copilot AI Nov 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new file path regex can match empty strings, which is likely unintended. For example:

  • The Windows path pattern ([a-zA-Z]:\\|\\\\)?([^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]* has optional components that can all be empty
  • The Unix path pattern (~?\/|\.\/|\.\.\/)?([^\/:*?"<>|\r\n]+\/)*[^\/:*?"<>|\r\n]* also has optional components with * quantifiers allowing zero matches

This means isUrlOrPath("") would return true, which is probably not the desired behavior. Consider using + instead of * for the final character class in each path pattern, or adding a length check before the regex matching.

Copilot uses AI. Check for mistakes.
if (filePathPattern.hasMatch(value)) {
return true;
}
Expand Down