url-validator.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // GrabZilla 2.1 - URL Validation Utilities
  2. // Comprehensive URL validation for video platforms
  3. class URLValidator {
  4. // Check if URL is a valid video URL from supported platforms
  5. static isValidVideoUrl(url) {
  6. if (!url || typeof url !== 'string') {
  7. return false;
  8. }
  9. const trimmedUrl = url.trim();
  10. if (trimmedUrl.length === 0) {
  11. return false;
  12. }
  13. // Check against supported platforms
  14. return this.isYouTubeUrl(trimmedUrl) ||
  15. this.isVimeoUrl(trimmedUrl) ||
  16. this.isGenericVideoUrl(trimmedUrl);
  17. }
  18. // Validate YouTube URLs
  19. static isYouTubeUrl(url) {
  20. // Match YouTube URLs with any query parameters
  21. const videoPattern = /^(https?:\/\/)?(www\.)?(youtube\.com\/(watch\?v=|embed\/|v\/)|youtu\.be\/)[\w\-_]{11}([?&].*)?$/i;
  22. const playlistPattern = /^(https?:\/\/)?(www\.)?youtube\.com\/playlist\?list=[\w\-]+/i;
  23. return videoPattern.test(url) || playlistPattern.test(url);
  24. }
  25. // Validate Vimeo URLs
  26. static isVimeoUrl(url) {
  27. const patterns = window.AppConfig?.VALIDATION_PATTERNS || {
  28. VIMEO_URL: /^(https?:\/\/)?(www\.)?(vimeo\.com\/\d+|player\.vimeo\.com\/video\/\d+)/i
  29. };
  30. return patterns.VIMEO_URL.test(url);
  31. }
  32. // Check if URL is a YouTube playlist
  33. static isYouTubePlaylist(url) {
  34. if (!url || typeof url !== 'string') {
  35. return false;
  36. }
  37. return /[?&]list=[\w\-]+/.test(url);
  38. }
  39. // Validate generic video URLs
  40. static isGenericVideoUrl(url) {
  41. // Disable generic video URL validation to be more strict
  42. // Only allow explicitly supported platforms (YouTube, Vimeo)
  43. return false;
  44. }
  45. // Extract video ID from YouTube URL
  46. static extractYouTubeId(url) {
  47. if (!this.isYouTubeUrl(url)) {
  48. return null;
  49. }
  50. const patterns = [
  51. /[?&]v=([^&#]*)/, // youtube.com/watch?v=ID
  52. /\/embed\/([^\/\?]*)/, // youtube.com/embed/ID
  53. /\/v\/([^\/\?]*)/, // youtube.com/v/ID
  54. /youtu\.be\/([^\/\?]*)/ // youtu.be/ID
  55. ];
  56. for (const pattern of patterns) {
  57. const match = url.match(pattern);
  58. if (match && match[1]) {
  59. return match[1];
  60. }
  61. }
  62. return null;
  63. }
  64. // Extract video ID from Vimeo URL
  65. static extractVimeoId(url) {
  66. if (!this.isVimeoUrl(url)) {
  67. return null;
  68. }
  69. const match = url.match(/vimeo\.com\/(\d+)/);
  70. return match ? match[1] : null;
  71. }
  72. // Normalize URL to standard format
  73. static normalizeUrl(url) {
  74. if (!url || typeof url !== 'string') {
  75. return url;
  76. }
  77. let normalizedUrl = url.trim();
  78. // Add protocol if missing
  79. if (!/^https?:\/\//i.test(normalizedUrl)) {
  80. normalizedUrl = 'https://' + normalizedUrl;
  81. }
  82. // Normalize YouTube URLs
  83. if (this.isYouTubeUrl(normalizedUrl)) {
  84. const videoId = this.extractYouTubeId(normalizedUrl);
  85. if (videoId) {
  86. return `https://www.youtube.com/watch?v=${videoId}`;
  87. }
  88. }
  89. // Normalize Vimeo URLs
  90. if (this.isVimeoUrl(normalizedUrl)) {
  91. const videoId = this.extractVimeoId(normalizedUrl);
  92. if (videoId) {
  93. return `https://vimeo.com/${videoId}`;
  94. }
  95. }
  96. return normalizedUrl;
  97. }
  98. // Get platform name from URL
  99. static getPlatform(url) {
  100. if (this.isYouTubeUrl(url)) {
  101. return 'YouTube';
  102. }
  103. if (this.isVimeoUrl(url)) {
  104. return 'Vimeo';
  105. }
  106. return 'Unknown';
  107. }
  108. // Validate multiple URLs (one per line)
  109. static validateMultipleUrls(urlText) {
  110. if (!urlText || typeof urlText !== 'string') {
  111. return { valid: [], invalid: [] };
  112. }
  113. // Extract all URLs from text using regex patterns
  114. // Match entire YouTube URLs including all query parameters
  115. const youtubePattern = /(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:watch\?v=|embed\/|v\/)|youtu\.be\/)[\w\-_]{11}(?:[?&][^\s]*)*/gi;
  116. const vimeoPattern = /(?:https?:\/\/)?(?:www\.)?(?:vimeo\.com\/|player\.vimeo\.com\/video\/)\d+/gi;
  117. const youtubeMatches = urlText.match(youtubePattern) || [];
  118. const vimeoMatches = urlText.match(vimeoPattern) || [];
  119. const allUrls = [...youtubeMatches, ...vimeoMatches];
  120. const valid = [];
  121. const invalid = [];
  122. const seen = new Set();
  123. allUrls.forEach(url => {
  124. // Fully normalize URLs to canonical format for deduplication
  125. const normalizedUrl = this.normalizeUrl(url);
  126. // Deduplicate based on normalized canonical URL
  127. if (!seen.has(normalizedUrl)) {
  128. seen.add(normalizedUrl);
  129. if (this.isValidVideoUrl(normalizedUrl)) {
  130. valid.push(normalizedUrl);
  131. } else {
  132. invalid.push(url);
  133. }
  134. }
  135. });
  136. return { valid, invalid };
  137. }
  138. // Check for duplicate URLs in a list
  139. static findDuplicates(urls) {
  140. const normalized = urls.map(url => this.normalizeUrl(url));
  141. const duplicates = [];
  142. const seen = new Set();
  143. normalized.forEach((url, index) => {
  144. if (seen.has(url)) {
  145. duplicates.push({ url: urls[index], index });
  146. } else {
  147. seen.add(url);
  148. }
  149. });
  150. return duplicates;
  151. }
  152. // Get validation error message
  153. static getValidationError(url) {
  154. if (url === null || url === undefined) {
  155. return 'URL is required';
  156. }
  157. if (typeof url !== 'string' || url.trim().length === 0) {
  158. return 'URL cannot be empty';
  159. }
  160. const trimmedUrl = url.trim();
  161. if (!/^https?:\/\//i.test(trimmedUrl) && !/^www\./i.test(trimmedUrl) && !trimmedUrl.includes('.')) {
  162. return 'Invalid URL format - must include domain';
  163. }
  164. if (!this.isValidVideoUrl(trimmedUrl)) {
  165. return 'Unsupported video platform - currently supports YouTube and Vimeo';
  166. }
  167. return null; // Valid URL
  168. }
  169. }
  170. // Export for use in other modules
  171. if (typeof module !== 'undefined' && module.exports) {
  172. // Node.js environment
  173. module.exports = URLValidator;
  174. } else {
  175. // Browser environment - attach to window
  176. window.URLValidator = URLValidator;
  177. }