FileMetadata.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Deletion.pm ↑ Summary FollowList.pm »
1 package Net::Nostr::FileMetadata;
2  
3 1 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Net::Nostr::_ConstructorArgs ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 0 ▶use Net::Nostr::Event;
Subroutine BEGIN: called CC=1 SCAR=2.6
9  
10 use Class::Tiny qw(
11 url
12 m
13 x
14 ox
15 size
16 dim
17 magnet
18 i
19 blurhash
20 thumb
21 thumb_hash
22 image
23 image_hash
24 summary
25 alt
26 fallback
27 service
28 );
29  
30 my $HEX64 = qr/\A[0-9a-f]{64}\z/;
31  
32 my %SIMPLE_TAGS = map { $_ => 1 }
33 qw(url m x ox size dim magnet i blurhash summary alt service);
34  
35 sub new {
36 my $class = shift;
37 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
38 $args{fallback} //= [];
39 my $self = bless { %args }, $class;
40 my %known; @known{Class::Tiny->get_all_attributes_for($class)} = ();
41 my @unknown = grep { !exists $known{$_} } keys %$self;
42 croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown;
43 return $self;
44 }
45  
46 sub to_event {
47 my $class = shift;
48 my %args = Net::Nostr::_ConstructorArgs::normalize(@_);
49  
50 my $url = delete $args{url} // croak "to_event requires 'url'";
51 my $m = delete $args{m} // croak "to_event requires 'm'";
52 my $x = delete $args{x} // croak "to_event requires 'x'";
53 my $ox = delete $args{ox} // croak "to_event requires 'ox'";
54  
55 croak "x must be 64-char lowercase hex (SHA-256)" unless $x =~ $HEX64;
56 croak "ox must be 64-char lowercase hex (SHA-256)" unless $ox =~ $HEX64;
57 croak "m must be a lowercase MIME type (e.g. 'image/jpeg')"
58 unless $m =~ m{/};
59 croak "m must be a lowercase MIME type" unless $m eq lc($m);
60  
61 my @tags;
62 push @tags, ['url', $url];
63 push @tags, ['m', $m];
64 push @tags, ['x', $x];
65 push @tags, ['ox', $ox];
66  
67 for my $key (qw(size dim magnet i blurhash summary alt service)) {
68 my $val = delete $args{$key};
69 next unless defined $val;
70 if ($key eq 'dim') {
71 croak "dim must be in '<width>x<height>' format (e.g. '1920x1080')"
72 unless $val =~ /\A\d+x\d+\z/;
73 }
74 push @tags, [$key, $val];
75 }
76  
77 # thumb/image with optional hash
78 for my $key (qw(thumb image)) {
79 my $val = delete $args{$key};
80 my $hash = delete $args{"${key}_hash"};
81 next unless defined $val;
82 my @tag = ($key, $val);
83 push @tag, $hash if defined $hash;
84 push @tags, \@tag;
85 }
86  
87 # fallback (zero or more)
88 my $fallback = delete $args{fallback};
89 if ($fallback) {
90 for my $fb (@$fallback) {
91 push @tags, ['fallback', $fb];
92 }
93 }
94  
95 # extra_tags
96 my $extra = delete $args{extra_tags};
97 if ($extra) {
98 push @tags, @$extra;
99 }
100  
101 return Net::Nostr::Event->new(%args, kind => 1063, tags => \@tags);
102 }
103  
104 sub from_event {
105 my ($class, $event) = @_;
106 return undef unless $event->kind == 1063;
107  
108 my %attrs;
109 my @fallback;
110  
111 for my $tag (@{$event->tags}) {
112 my $name = $tag->[0];
113 if ($name eq 'fallback') {
114 push @fallback, $tag->[1];
115 } elsif ($name eq 'thumb') {
116 $attrs{thumb} = $tag->[1];
117 $attrs{thumb_hash} = $tag->[2] if defined $tag->[2];
118 } elsif ($name eq 'image') {
119 $attrs{image} = $tag->[1];
120 $attrs{image_hash} = $tag->[2] if defined $tag->[2];
121 } elsif ($SIMPLE_TAGS{$name}) {
122 $attrs{$name} = $tag->[1];
123 }
124 }
125  
126 return $class->new(%attrs, fallback => \@fallback);
127 }
128  
129 sub validate {
130 my ($class, $event) = @_;
131  
132 croak "file metadata event MUST be kind 1063" unless $event->kind == 1063;
133  
134 my %has;
135 for my $tag (@{$event->tags}) {
136 $has{$tag->[0]} = 1;
137 }
138  
139 croak "file metadata event MUST have a 'url' tag" unless $has{url};
140 croak "file metadata event MUST have an 'm' tag" unless $has{m};
141 croak "file metadata event MUST have an 'x' tag" unless $has{x};
142 croak "file metadata event MUST have an 'ox' tag" unless $has{ox};
143  
144 return 1;
145 }
146  
147 1;
148  
149 __END__
« Deletion.pm ↑ Summary FollowList.pm »