Enhance image path validation to support URLs

Updated isValidImagePath to recognize and validate image URLs in addition to local file paths. The function now checks for valid URL formats and logs debug information for both URLs and local paths.
This commit is contained in:
Dariusz L
2025-07-01 08:01:37 +02:00
parent 94ffc64f6e
commit 0f05e36333

View File

@@ -73,9 +73,9 @@ export class ClipboardManager {
} }
/** /**
* Validates if a text string is a valid image file path * Validates if a text string is a valid image file path or URL
* @param {string} text - The text to validate * @param {string} text - The text to validate
* @returns {boolean} - True if the text appears to be a valid image file path * @returns {boolean} - True if the text appears to be a valid image file path or URL
*/ */
isValidImagePath(text) { isValidImagePath(text) {
if (!text || typeof text !== 'string') { if (!text || typeof text !== 'string') {
@@ -90,7 +90,20 @@ export class ClipboardManager {
return false; return false;
} }
// Common image file extensions // Check if it's a URL first (URLs have priority and don't need file extensions)
if (text.startsWith('http://') || text.startsWith('https://') || text.startsWith('file://')) {
// For URLs, we're more permissive - any valid URL could potentially be an image
try {
new URL(text);
log.debug("Detected valid URL:", text);
return true;
} catch (e) {
log.debug("Invalid URL format:", text);
return false;
}
}
// For local file paths, check for image extensions
const imageExtensions = [ const imageExtensions = [
'.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp', '.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp',
'.svg', '.tiff', '.tif', '.ico', '.avif' '.svg', '.tiff', '.tif', '.ico', '.avif'
@@ -102,23 +115,28 @@ export class ClipboardManager {
); );
if (!hasImageExtension) { if (!hasImageExtension) {
log.debug("No valid image extension found in:", text);
return false; return false;
} }
// Basic path validation - should look like a file path // Basic path validation for local files - should look like a file path
// Accept both Windows and Unix style paths, and URLs // Accept both Windows and Unix style paths
const pathPatterns = [ const pathPatterns = [
/^[a-zA-Z]:[\\\/]/, // Windows absolute path (C:\... or C:/...) /^[a-zA-Z]:[\\\/]/, // Windows absolute path (C:\... or C:/...)
/^[\\\/]/, // Unix absolute path (/...) /^[\\\/]/, // Unix absolute path (/...)
/^\.{1,2}[\\\/]/, // Relative path (./... or ../...) /^\.{1,2}[\\\/]/, // Relative path (./... or ../...)
/^https?:\/\//, // HTTP/HTTPS URL
/^file:\/\//, // File URL
/^[^\\\/]*[\\\/]/ // Contains path separators /^[^\\\/]*[\\\/]/ // Contains path separators
]; ];
const isValidPath = pathPatterns.some(pattern => pattern.test(text)) || const isValidPath = pathPatterns.some(pattern => pattern.test(text)) ||
(!text.includes('/') && !text.includes('\\') && text.includes('.')); // Simple filename (!text.includes('/') && !text.includes('\\') && text.includes('.')); // Simple filename
if (isValidPath) {
log.debug("Detected valid local file path:", text);
} else {
log.debug("Invalid local file path format:", text);
}
return isValidPath; return isValidPath;
} }