File size: 2,018 Bytes
b225a21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import 'package:http/http.dart' as http;
import 'dart:convert';

class UriUtility {
  static bool isURL(String url) {
    // Validate if the URL string is empty, or contains spaces or invalid characters
    if (url.isEmpty || RegExp(r'[\s<>]').hasMatch(url)) {
      print('URL is either empty or contains spaces/invalid characters.');
      return false;
    }

    // Check for 'mailto:' at the start of the URL
    if (url.startsWith('mailto:')) {
      print('URL starts with "mailto:".');
      return false;
    }

    // Try to parse the URL, return false if parsing fails
    Uri? uri;
    try {
      uri = Uri.parse(url);
    } catch (e) {
      print('URL parsing failed: $e');
      return false;
    }

    // Validate the URL has a scheme (protocol) and host
    if (uri.scheme.isEmpty || uri.host.isEmpty) {
      print('URL is missing a scheme (protocol) or host.');
      return false;
    }

    // Check if the URI has user info, which is not a common case for a valid HTTP/HTTPS URL
    if (uri.hasAuthority &&
        uri.userInfo.contains(':') &&
        uri.userInfo.split(':').length > 2) {
      print('URL contains invalid user info.');
      return false;
    }

    // Validate the port number if exists
    if (uri.hasPort && (uri.port <= 0 || uri.port > 65535)) {
      print('URL contains an invalid port number.');
      return false;
    }

    print('URL is valid.');
    return true;
  }

  Future<bool> isValidGitHubRepo(String repoUrl) async {
    var uri = Uri.parse(repoUrl);
    if (uri.host != 'github.com') {
      return false;
    }

    var segments = uri.pathSegments;
    if (segments.length < 2) {
      return false;
    }

    var user = segments[0];
    var repo = segments[1];

    var apiUri = Uri.https('api.github.com', '/repos/$user/$repo');

    var response = await http.get(apiUri);
    if (response.statusCode != 200) {
      return false;
    }

    var data = json.decode(response.body);
    return data is Map && data['full_name'] == '$user/$repo';
  }
}