srvpath/Util.pm untested

filter: Statementstmt 0.0% 0 / 131 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 236 CC 11 SCAR 37.6
CC
11
Cov
0%
path2uri
132.0
epoch
2.0
uri_split
2.0
CRAP 42.9
1 use Object::Pad ':experimental(:all)';
2  
3 package WWW::srvpath::Util;
4 role WWW::srvpath::Util; #: does(WWW::srvpath::Config);
5  
6 use utf8;
7 use v5.44;
8  
9 no warnings 'experimental::re_strict';
10 use re 'strict';
11  
12 use Path::Try;
13 use Const::Fast;
14 use Time::HiRes;
15 use Syntax::Keyword::Dynamically;
16 use IO::Handle::Common;
17 use HTML::Escape;
18 use URI::Escape;
19 use Net::Domain 'hostfqdn';
20 use List::Util 'none';
21  
22 use Exporter;
23 use parent 'Exporter';
24  
25 our @EXPORT_OK = qw'path2uri epoch file_unique path_uri_encode';
26 our @EXPORT = @EXPORT_OK;
27  
28 sub epoch ( $join = '' ) {
29 join $join, Time::HiRes::gettimeofday;
30 }
31  
32 my class FileUnique {
33 use utf8;
34 use v5.44;
35  
36 use IO::Handle::Common;
37 use Path::Tiny;
38 use List::Util 'none';
39  
40 field $cksum : reader = {};
41 field $digest : param;
42  
43 method $file_unique($in) {
44 my $digest = $in->digest;
45 $$cksum{$digest} //= [];
46 push $$cksum{$digest}->@*, $in->absolute
47 if none { dmsg $in, $_; $in->absolute eq $_ } $$cksum{digest}->@*;
48  
49 dmsg $in, $digest;
50  
51 my $ret;
52  
53 if ( scalar $$cksum{$digest}->@* > 1 ) {
54 $ret = undef;
55 say STDERR "Duplicate file '$in' detected. Other paths:";
56 my $dupestr = " " . ( join ', ', $$cksum{$digest}->@* ) . "\n";
57 say STDERR $dupestr;
58 }
59 else { $ret = 1 }
60  
61 $ret;
62 }
63  
64 method file_unique : common (@in) {
65 my $self = $class->new();
66 $self->$file_unique( path($_) ) for @in;
67 }
68 }
69  
70 sub file_unique(@in) {
71 FileUnique->file_unique(@in);
72 }
73  
74 sub path_uri_encode( $path, %opt ) {
75 my $return_joined;
76 $opt{join} //= "\n";
77  
78 ( $path, $return_joined ) = [ split qr!/!, $path ]
79 if ( ref $path && ref $path eq 'ARRAY' );
80  
81 my @pathencoded = (
82 map {
83 $opt{charset} && $opt{charset} ne 'utf8'
84 ? uri_escape($_)
85 : uri_escape_utf8($_)
86 } @$path
87 );
88  
89 $return_joined ? join( $opt{join}, @pathencoded ) : @pathencoded;
90 }
91  
92 sub wrap_anchor ( $href, $text, %opt ) {
93 qq'<a href="$href" '
94 . (
95 $opt{title}
96 ? qq'"title="' . escape_html( $opt{title} ) . '"'
97 : ''
98 )
99 . '>'
100 . escape_html($text) . "</a>";
101 }
102  
103 const our $urisplit => qr|
104 (?:([^:/?\#]+):)?
105 (?://([^/?\#]*))?
106 ([^?\#]*)
107 (?:\?([^\#]*))?(?:\#(.*))?|x;
108  
109 const our $pathsplit => qr|/|;
110  
111 sub uri_split( $uristr, %opt ) {
112 ( $uristr =~ $urisplit )
113 }
114  
115 sub path_split( $pathstr, %opt ) {
116 ( split /$pathsplit/, $pathstr )
117 }
118  
119 const our %urifield_default => (
120 scheme => 'https',
121 host => hostfqdn,
122 ( map { ( $_ => undef ) } qw'port path query fragment' )
123 );
124  
125 sub path2uri ( $path_aref, %opt ) {
126 my @out;
127  
128 $opt{host} //= hostfqdn;
129 $opt{join} //= "\n";
130  
131 my %urifield;
132  
133 foreach my ($pathstr) (@$path_aref) {
134  
135 @urifield{ keys %urifield_default } = $pathstr =~ $urisplit;
136  
137 foreach my ( $k, $v ) (
138 map { ( $_ => ( $opt{$_} // $urifield_default{$_} ) ) }
139 keys %urifield_default
140 )
141 {
142 next unless $v;
143 if ( $opt{"replace_$k"} ) {
144 $urifield{$k} = $v;
145 }
146 else {
147 $urifield{$k} //= $v;
148 }
149 }
150  
151 if ( $opt{uniq} || $opt{readpath} ) {
152 $urifield{path} = path( $urifield{path} )->absolute;
153  
154 next unless $urifield{path}->is_file;
155 next if $opt{unique} && !file_unique( $urifield{path} );
156 }
157  
158 my @pathencoded = path_uri_encode( path_split( $urifield{path} ) );
159  
160 my $href; # = $pathencoded;
161  
162 if ( !$opt{relative} ) {
163 $href = "$urifield{scheme}://$urifield{host}$href";
164 $href .= ":$urifield{port}"
165 if none { $_ == $urifield{port} } qw(80 443);
166 }
167 $href .= join '/', @pathencoded;
168  
169 $href .= "?$urifield{query}" if $urifield{query};
170 $href .= "#$urifield{fragment}" if $urifield{fragment};
171  
172 my $outstr = $href;
173  
174 if ( $opt{wrap_anchor} ) {
175 wrap_anchor(
176 $href,
177 $pathencoded[ scalar @pathencoded - 1 ],
178 %opt{qw'title nofollow target style class id'}
179 );
180 }
181  
182 push @out, $outstr;
183 }
184  
185 @out;
186 }
187