| 1 | package Net::Blossom::Server::Backend::Filesystem; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strictures 2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Class::Tiny qw(metadata_store blob_store base_url cleanup_error_handler); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Net::Blossom::BlobDescriptor; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Net::Blossom::_URL; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Net::Blossom::Server::Backend::Filesystem::BlobStore; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Net::Blossom::Server::BlobResult; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use Net::Blossom::Server::BlobStore; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use Net::Blossom::Server::MetadataStore; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 4 | our $VERSION = '0.001001'; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 4 | my @BLOB_STORE_ARGS = qw(root generation); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 11 | ▶ | sub BUILDARGS { | ||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=11 SCAR=24.1
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | 11 | my $class = shift; | |||||||||||||||||||||||||||
| 20 | 11 | my %args = _constructor_args(@_); | |||||||||||||||||||||||||||
| 21 | 11 | my %known = map { $_ => 1 } ( | |||||||||||||||||||||||||||
| 22 | qw(metadata_store blob_store base_url cleanup_error_handler), | ||||||||||||||||||||||||||||
| 23 | @BLOB_STORE_ARGS, | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | 11 | my @unknown = grep { !$known{$_} } keys %args; | |||||||||||||||||||||||||||
| 26 | 11 | ▶ | croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 10 | ▶ | croak "metadata_store is required" unless defined $args{metadata_store}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 9 | ▶ | croak "base_url is required" unless defined $args{base_url}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | croak "cleanup_error_handler must be a code reference" | ||||||||||||||||||||||||||||
| 31 | if defined $args{cleanup_error_handler} | ||||||||||||||||||||||||||||
| 32 | 8 | ▶ | && ref($args{cleanup_error_handler}) ne 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $args{"cleanup_error_handler"} and ref $args{"cleanup_error_handler"} ne "CODE"
MC/DC50%1/2
defined $args{"cleanup_error_handler"} and ref $args{"cleanup_error_handler"} ne "CODE"
defined $args{"cleanup_error_handler"}
ref $args{"cleanup_error_handler"} ne "CODE"
Truth table66%2/3
A and B
A= defined $args{"cleanup_error_handler"}B= ref $args{"cleanup_error_handler"} ne "CODE"
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 7 | Net::Blossom::Server::MetadataStore->assert_implements($args{metadata_store}); | |||||||||||||||||||||||||||
| 35 | 6 | my $base_url = _normalize_base_url($args{base_url}); | |||||||||||||||||||||||||||
| 36 | 5 | my $blob_store = delete $args{blob_store}; | |||||||||||||||||||||||||||
| 37 | my %blob_args = map { | ||||||||||||||||||||||||||||
| 38 | 5 | ▶ | exists $args{$_} ? ($_ => delete $args{$_}) : () | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | } @BLOB_STORE_ARGS; | ||||||||||||||||||||||||||||
| 40 | 5 | ▶ | croak "blob_store cannot be combined with filesystem arguments" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $blob_store and keys %blob_args
MC/DC100%2/2
defined $blob_store and keys %blob_args
defined $blob_store
keys %blob_args
Truth table100%3/3
A and B
A= defined $blob_storeB= keys %blob_args
| |||||||||||||||||||||||||||||
| 41 | if defined $blob_store && keys %blob_args; | ||||||||||||||||||||||||||||
| 42 | 4 | ▶ | $blob_store = Net::Blossom::Server::Backend::Filesystem::BlobStore->new( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | %blob_args, | ||||||||||||||||||||||||||||
| 44 | ) unless defined $blob_store; | ||||||||||||||||||||||||||||
| 45 | 3 | Net::Blossom::Server::BlobStore->assert_implements($blob_store); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | return { | ||||||||||||||||||||||||||||
| 48 | metadata_store => $args{metadata_store}, | ||||||||||||||||||||||||||||
| 49 | blob_store => $blob_store, | ||||||||||||||||||||||||||||
| 50 | base_url => $base_url, | ||||||||||||||||||||||||||||
| 51 | 0 | ▶ | cleanup_error_handler => $args{cleanup_error_handler} || sub { warn $_[0] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 52 | 3 | ▶ | }; | ||||||||||||||||||||||||||
|
Condition33%1/3
$args{'cleanup_error_handler'} || sub {
warn $_[0];
}
MC/DC0%0/2
$args{'cleanup_error_handler'} || sub {
warn $_[0];
}
$args{'cleanup_error_handler'}
sub {
warn $_[0];
}
Truth table33%1/3
A || B
A= $args{'cleanup_error_handler'}B= sub {
warn $_[0];
}
| |||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | sub deploy_schema { | ||||||||||||||||||||||||||
|
Subroutine
deploy_schema: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 57 | 0 | $self->blob_store->deploy_schema; | |||||||||||||||||||||||||||
| 58 | 0 | $self->metadata_store->deploy_schema; | |||||||||||||||||||||||||||
| 59 | 0 | return 1; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | sub begin_upload { | ||||||||||||||||||||||||||
|
Subroutine
begin_upload: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 0 | my ($self, %context) = @_; | |||||||||||||||||||||||||||
| 64 | 0 | my $blob_upload = $self->blob_store->begin_upload(%context); | |||||||||||||||||||||||||||
| 65 | 0 | Net::Blossom::Server::BlobStore->assert_upload($blob_upload); | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | return Net::Blossom::Server::Backend::Filesystem::_Upload->new( | |||||||||||||||||||||||||||
| 68 | storage => $self, | ||||||||||||||||||||||||||||
| 69 | blob_upload => $blob_upload, | ||||||||||||||||||||||||||||
| 70 | ); | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | sub get_blob { | ||||||||||||||||||||||||||
|
Subroutine
get_blob: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | 0 | my ($self, $sha256) = @_; | |||||||||||||||||||||||||||
| 75 | 0 | my $row = $self->metadata_store->find_blob($sha256); | |||||||||||||||||||||||||||
| 76 | 0 | ▶ | return unless defined $row; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | my $body = $self->blob_store->get_blob( | ||||||||||||||||||||||||||||
| 78 | $row->{storage_key}, | ||||||||||||||||||||||||||||
| 79 | size => $row->{size}, | ||||||||||||||||||||||||||||
| 80 | 0 | ); | |||||||||||||||||||||||||||
| 81 | 0 | ▶ | return unless defined $body; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | return Net::Blossom::Server::BlobResult->new( | |||||||||||||||||||||||||||
| 84 | descriptor => $self->_descriptor($row), | ||||||||||||||||||||||||||||
| 85 | body => $body, | ||||||||||||||||||||||||||||
| 86 | ); | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | sub get_blob_range { | ||||||||||||||||||||||||||
|
Subroutine
get_blob_range: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | 0 | my ($self, $sha256, %opts) = @_; | |||||||||||||||||||||||||||
| 91 | 0 | my $row = $self->metadata_store->find_blob($sha256); | |||||||||||||||||||||||||||
| 92 | 0 | ▶ | return unless defined $row; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | return $self->blob_store->get_blob_range( | ||||||||||||||||||||||||||||
| 94 | $row->{storage_key}, | ||||||||||||||||||||||||||||
| 95 | %opts, | ||||||||||||||||||||||||||||
| 96 | size => $row->{size}, | ||||||||||||||||||||||||||||
| 97 | 0 | ); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 0 | ▶ | sub head_blob { | ||||||||||||||||||||||||||
|
Subroutine
head_blob: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 101 | 0 | my ($self, $sha256) = @_; | |||||||||||||||||||||||||||
| 102 | 0 | my $row = $self->metadata_store->find_blob($sha256); | |||||||||||||||||||||||||||
| 103 | 0 | ▶ | return unless defined $row; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 0 | return $self->_descriptor($row); | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | sub delete_blob { | ||||||||||||||||||||||||||
|
Subroutine
delete_blob: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 108 | 0 | my ($self, $sha256, %opts) = @_; | |||||||||||||||||||||||||||
| 109 | 0 | my $metadata = $self->metadata_store; | |||||||||||||||||||||||||||
| 110 | 0 | my $storage_key; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | ▶ | my $deleted = $metadata->with_transaction(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 113 | 0 | $metadata->lock_blob($sha256); | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | if (defined $opts{pubkey}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | return 0 unless $metadata->delete_owner($sha256, $opts{pubkey}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | if (!$metadata->owner_count($sha256)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 0 | my $record = $metadata->find_blob($sha256); | |||||||||||||||||||||||||||
| 119 | 0 | ▶ | if (defined $record) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 0 | $storage_key = $record->{storage_key}; | |||||||||||||||||||||||||||
| 121 | 0 | $metadata->delete_blob($sha256); | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | 0 | return 1; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | my $record = $metadata->find_blob($sha256); | |||||||||||||||||||||||||||
| 128 | 0 | ▶ | return 0 unless defined $record; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 0 | $storage_key = $record->{storage_key}; | |||||||||||||||||||||||||||
| 130 | 0 | $metadata->delete_owners($sha256); | |||||||||||||||||||||||||||
| 131 | 0 | $metadata->delete_blob($sha256); | |||||||||||||||||||||||||||
| 132 | 0 | return 1; | |||||||||||||||||||||||||||
| 133 | 0 | }); | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | ▶ | $self->_delete_file($storage_key) if defined $storage_key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 0 | ▶ | return $deleted ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | ▶ | sub list_blobs { | ||||||||||||||||||||||||||
|
Subroutine
list_blobs: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 140 | 0 | my ($self, $pubkey, %opts) = @_; | |||||||||||||||||||||||||||
| 141 | 0 | my $rows = $self->metadata_store->list_blobs($pubkey, %opts); | |||||||||||||||||||||||||||
| 142 | 0 | return [map { $self->_descriptor($_) } @$rows]; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | ▶ | sub _commit_upload { | ||||||||||||||||||||||||||
|
Subroutine
_commit_upload: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 146 | 0 | my ($self, $upload, %metadata) = @_; | |||||||||||||||||||||||||||
| 147 | 0 | my $store = $self->metadata_store; | |||||||||||||||||||||||||||
| 148 | 0 | my $created; | |||||||||||||||||||||||||||
| 149 | my $storage_key; | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | my $ok = eval { | |||||||||||||||||||||||||||
| 152 | 0 | ▶ | $store->with_transaction(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 153 | 0 | $store->lock_blob($metadata{sha256}); | |||||||||||||||||||||||||||
| 154 | 0 | my $record = $store->find_blob($metadata{sha256}); | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 0 | ▶ | if (defined $record) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 0 | $created = 0; | |||||||||||||||||||||||||||
| 158 | 0 | $storage_key = $record->{storage_key}; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | else { | ||||||||||||||||||||||||||||
| 161 | 0 | $storage_key = $upload->_prepare(%metadata); | |||||||||||||||||||||||||||
| 162 | 0 | ▶ | $created = $store->insert_blob( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | %metadata, | ||||||||||||||||||||||||||||
| 164 | storage_key => $storage_key, | ||||||||||||||||||||||||||||
| 165 | ) ? 1 : 0; | ||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 0 | ▶ | $store->upsert_owner(%metadata) if defined $metadata{pubkey}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 0 | return 1; | |||||||||||||||||||||||||||
| 170 | 0 | }); | |||||||||||||||||||||||||||
| 171 | 0 | 1; | |||||||||||||||||||||||||||
| 172 | }; | ||||||||||||||||||||||||||||
| 173 | 0 | my $error = $@; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 0 | ▶ | if (!$ok) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 0 | eval { $upload->_cleanup(0) }; | |||||||||||||||||||||||||||
| 177 | 0 | die $error; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 0 | my $cleanup_ok = eval { $upload->_cleanup($created); 1 }; | |||||||||||||||||||||||||||
| 181 | 0 | ▶ | $self->_report_cleanup_error($@, $storage_key) unless $cleanup_ok; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | return { | ||||||||||||||||||||||||||||
| 184 | 0 | descriptor => $self->_descriptor(\%metadata), | |||||||||||||||||||||||||||
| 185 | created => $created, | ||||||||||||||||||||||||||||
| 186 | }; | ||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 0 | ▶ | sub _delete_file { | ||||||||||||||||||||||||||
|
Subroutine
_delete_file: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 190 | 0 | my ($self, $storage_key) = @_; | |||||||||||||||||||||||||||
| 191 | 0 | my $ok = eval { $self->blob_store->delete_blob($storage_key); 1 }; | |||||||||||||||||||||||||||
| 192 | 0 | ▶ | $self->_report_cleanup_error($@, $storage_key) unless $ok; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | 0 | return; | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 0 | ▶ | sub _report_cleanup_error { | ||||||||||||||||||||||||||
|
Subroutine
_report_cleanup_error: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 197 | 0 | my ($self, $error, $storage_key) = @_; | |||||||||||||||||||||||||||
| 198 | 0 | ▶ | return unless defined $error && length $error; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $error and length $error
MC/DC0%0/2
defined $error and length $error
defined $error
length $error
Truth table0%0/3
A and B
A= defined $errorB= length $error
| |||||||||||||||||||||||||||||
| 199 | 0 | my $ok = eval { $self->cleanup_error_handler->($error, $storage_key); 1 }; | |||||||||||||||||||||||||||
| 200 | 0 | ▶ | warn $@ unless $ok; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 0 | return; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 0 | ▶ | sub _descriptor { | ||||||||||||||||||||||||||
|
Subroutine
_descriptor: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 205 | 0 | my ($self, $row) = @_; | |||||||||||||||||||||||||||
| 206 | return Net::Blossom::BlobDescriptor->new( | ||||||||||||||||||||||||||||
| 207 | url => $self->base_url . '/' . $row->{sha256}, | ||||||||||||||||||||||||||||
| 208 | sha256 => $row->{sha256}, | ||||||||||||||||||||||||||||
| 209 | size => 0 + $row->{size}, | ||||||||||||||||||||||||||||
| 210 | type => $row->{type}, | ||||||||||||||||||||||||||||
| 211 | uploaded => 0 + $row->{uploaded}, | ||||||||||||||||||||||||||||
| 212 | 0 | ); | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 6 | ▶ | sub _normalize_base_url { | ||||||||||||||||||||||||||
|
Subroutine
_normalize_base_url: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 216 | 6 | my ($base_url) = @_; | |||||||||||||||||||||||||||
| 217 | 6 | ▶ | croak "base_url must be a scalar" if ref($base_url); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | 6 | ▶ | croak "base_url is required" unless length $base_url; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | 6 | ▶ | croak "base_url must be a valid HTTP base URL" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | unless Net::Blossom::_URL::http_base_url($base_url); | ||||||||||||||||||||||||||||
| 221 | 5 | $base_url =~ s{/+\z}{}; | |||||||||||||||||||||||||||
| 222 | 5 | return $base_url; | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 11 | ▶ | sub _constructor_args { | ||||||||||||||||||||||||||
|
Subroutine
_constructor_args: called CC=4 SCAR=17.1
| |||||||||||||||||||||||||||||
| 226 | 0 | ▶ | return %{$_[0]} if @_ == 1 && ref($_[0]) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ == 1 and ref $_[0] eq "HASH"
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1
ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
| |||||||||||||||||||||||||||||
| 227 | 11 | ▶ | croak "constructor arguments must be name/value pairs" if @_ % 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 228 | 11 | return @_; | |||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | { | ||||||||||||||||||||||||||||
| 232 | package Net::Blossom::Server::Backend::Filesystem::_Upload; | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 4 | ▶ | use strictures 2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 4 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 237 | 4 | ▶ | use Class::Tiny qw(storage blob_upload), { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 238 | committed => 0, | ||||||||||||||||||||||||||||
| 239 | aborted => 0, | ||||||||||||||||||||||||||||
| 240 | 4 | }; | |||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | 0 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 243 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 244 | 0 | $self->committed; | |||||||||||||||||||||||||||
| 245 | 0 | $self->aborted; | |||||||||||||||||||||||||||
| 246 | 0 | return; | |||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 0 | ▶ | sub write { | ||||||||||||||||||||||||||
|
Subroutine
write: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 250 | 0 | my ($self, $chunk) = @_; | |||||||||||||||||||||||||||
| 251 | 0 | ▶ | croak "upload is already committed" if $self->{committed}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 252 | 0 | ▶ | croak "upload is aborted" if $self->{aborted}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 253 | 0 | return $self->{blob_upload}->write($chunk); | |||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | 0 | ▶ | sub commit { | ||||||||||||||||||||||||||
|
Subroutine
commit: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 257 | 0 | my ($self, %metadata) = @_; | |||||||||||||||||||||||||||
| 258 | 0 | ▶ | croak "upload is already committed" if $self->{committed}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 259 | 0 | ▶ | croak "upload is aborted" if $self->{aborted}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 260 | 0 | my $result = $self->{storage}->_commit_upload($self, %metadata); | |||||||||||||||||||||||||||
| 261 | 0 | $self->{committed} = 1; | |||||||||||||||||||||||||||
| 262 | 0 | return $result; | |||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | 0 | ▶ | sub abort { | ||||||||||||||||||||||||||
|
Subroutine
abort: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 266 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 267 | 0 | ▶ | return 1 if $self->{aborted} || $self->{committed}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"aborted"} or $self->{"committed"}
MC/DC0%0/2
$self->{"aborted"} or $self->{"committed"}
$self->{"aborted"}
$self->{"committed"}
Truth table0%0/3
A or B
A= $self->{"aborted"}B= $self->{"committed"}
| |||||||||||||||||||||||||||||
| 268 | 0 | $self->{aborted} = 1; | |||||||||||||||||||||||||||
| 269 | 0 | return $self->{blob_upload}->abort; | |||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | 0 | ▶ | sub _prepare { | ||||||||||||||||||||||||||
|
Subroutine
_prepare: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 273 | 0 | my ($self, %metadata) = @_; | |||||||||||||||||||||||||||
| 274 | 0 | return $self->{blob_upload}->prepare(%metadata); | |||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | 0 | ▶ | sub _cleanup { | ||||||||||||||||||||||||||
|
Subroutine
_cleanup: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 278 | 0 | my ($self, $created) = @_; | |||||||||||||||||||||||||||
| 279 | return $created | ||||||||||||||||||||||||||||
| 280 | ? $self->{blob_upload}->commit | ||||||||||||||||||||||||||||
| 281 | 0 | ▶ | : $self->{blob_upload}->abort; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | 0 | ▶ | sub DEMOLISH { | ||||||||||||||||||||||||||
|
Subroutine
DEMOLISH: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 285 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 286 | 0 | ▶ | return if $self->{committed} || $self->{aborted}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"committed"} or $self->{"aborted"}
MC/DC0%0/2
$self->{"committed"} or $self->{"aborted"}
$self->{"committed"}
$self->{"aborted"}
Truth table0%0/3
A or B
A= $self->{"committed"}B= $self->{"aborted"}
| |||||||||||||||||||||||||||||
| 287 | 0 | eval { $self->abort }; | |||||||||||||||||||||||||||
| 288 | 0 | return; | |||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | 4 | 1; | |||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | =pod | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | =head1 NAME | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | Net::Blossom::Server::Backend::Filesystem - Filesystem storage for Blossom servers | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | use DBI; | ||||||||||||||||||||||||||||
| 303 | use Net::Blossom::Server; | ||||||||||||||||||||||||||||
| 304 | use Net::Blossom::Server::Backend::Filesystem; | ||||||||||||||||||||||||||||
| 305 | use Net::Blossom::Server::Backend::SQLite::MetadataStore; | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | my $dbh = DBI->connect('dbi:SQLite:dbname=blossom.sqlite', '', '', { | ||||||||||||||||||||||||||||
| 308 | AutoCommit => 1, | ||||||||||||||||||||||||||||
| 309 | RaiseError => 1, | ||||||||||||||||||||||||||||
| 310 | PrintError => 0, | ||||||||||||||||||||||||||||
| 311 | }); | ||||||||||||||||||||||||||||
| 312 | my $metadata = Net::Blossom::Server::Backend::SQLite::MetadataStore->new( | ||||||||||||||||||||||||||||
| 313 | dbh => $dbh, | ||||||||||||||||||||||||||||
| 314 | ); | ||||||||||||||||||||||||||||
| 315 | my $storage = Net::Blossom::Server::Backend::Filesystem->new( | ||||||||||||||||||||||||||||
| 316 | metadata_store => $metadata, | ||||||||||||||||||||||||||||
| 317 | root => '/srv/blossom', | ||||||||||||||||||||||||||||
| 318 | base_url => 'https://cdn.example.com', | ||||||||||||||||||||||||||||
| 319 | ); | ||||||||||||||||||||||||||||
| 320 | $storage->deploy_schema; | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | my $server = Net::Blossom::Server->new(storage => $storage); | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | This module implements L<Net::Blossom::Server::Storage> for | ||||||||||||||||||||||||||||
| 327 | L<Net::Blossom::Server>. By default, blob bytes are stored in ordinary files. | ||||||||||||||||||||||||||||
| 328 | Blob metadata is supplied separately through any | ||||||||||||||||||||||||||||
| 329 | L<Net::Blossom::Server::MetadataStore> implementation. This distribution has | ||||||||||||||||||||||||||||
| 330 | no runtime dependency on a particular metadata backend; install one | ||||||||||||||||||||||||||||
| 331 | separately. | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | The default blob store stages uploads below C<root>, synchronizes them, and | ||||||||||||||||||||||||||||
| 334 | publishes them under immutable, generation-specific keys. Blob paths are | ||||||||||||||||||||||||||||
| 335 | sharded by the first four hexadecimal characters of their SHA-256 hash. | ||||||||||||||||||||||||||||
| 336 | Downloads stream from open filehandles and do not load complete blobs into | ||||||||||||||||||||||||||||
| 337 | memory. | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | =head1 CONSTRUCTOR | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | =head2 new | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | my $storage = Net::Blossom::Server::Backend::Filesystem->new(%args); | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | Requires C<metadata_store> and an HTTP or HTTPS C<base_url>. Also requires | ||||||||||||||||||||||||||||
| 346 | either C<root> or C<blob_store>. With the default blob store, C<root> is | ||||||||||||||||||||||||||||
| 347 | converted to an absolute path and C<deploy_schema> creates the root, blob, and | ||||||||||||||||||||||||||||
| 348 | staging directories when needed. The platform and filesystem must support hard | ||||||||||||||||||||||||||||
| 349 | links and directory synchronization. C<root/blobs> and C<root/.staging> must be | ||||||||||||||||||||||||||||
| 350 | on the same filesystem. | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | C<generation> may supply a callback that returns the safe filename suffix for | ||||||||||||||||||||||||||||
| 353 | each new blob. The suffix must begin with an ASCII letter or digit and contain | ||||||||||||||||||||||||||||
| 354 | only ASCII letters, digits, periods, underscores, or hyphens. The default is a | ||||||||||||||||||||||||||||
| 355 | random 128-bit hexadecimal value. Reusing a suffix for the same hash makes the | ||||||||||||||||||||||||||||
| 356 | upload fail instead of overwriting the existing file. | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | A custom C<blob_store> may replace the default filesystem component. It must | ||||||||||||||||||||||||||||
| 359 | implement L<Net::Blossom::Server::BlobStore>. It must accept | ||||||||||||||||||||||||||||
| 360 | C<< get_blob($key, size => $bytes) >> and permit deletion after the metadata | ||||||||||||||||||||||||||||
| 361 | transaction commits. It cannot be combined with C<root> or C<generation>. | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | C<cleanup_error_handler> may provide a callback for a blob cleanup failure that | ||||||||||||||||||||||||||||
| 364 | occurs after metadata commits. It receives the error and storage key. The | ||||||||||||||||||||||||||||
| 365 | default handler warns. | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | =head1 CONSISTENCY | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | This section describes the default blob store. Prepared files are durable | ||||||||||||||||||||||||||||
| 370 | before their storage keys enter metadata. Publishing uses a non-overwriting | ||||||||||||||||||||||||||||
| 371 | hard link, so concurrent writers cannot replace an existing generation. A | ||||||||||||||||||||||||||||
| 372 | metadata rollback after publication can leave an unreferenced file. With | ||||||||||||||||||||||||||||
| 373 | uploads stopped, operators may remove files whose keys are absent from the | ||||||||||||||||||||||||||||
| 374 | metadata store. | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | A process crash can leave files in C<root/.staging>. With uploads stopped, all | ||||||||||||||||||||||||||||
| 377 | files in that directory may be removed. | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | Deletion commits metadata before unlinking the file. A cleanup failure is | ||||||||||||||||||||||||||||
| 380 | reported through C<cleanup_error_handler> and may also leave an unreferenced | ||||||||||||||||||||||||||||
| 381 | file. Generation-specific keys prevent delayed deletion from erasing a later | ||||||||||||||||||||||||||||
| 382 | upload of the same hash. | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | The storage root must be writable only by trusted service processes. Files are | ||||||||||||||||||||||||||||
| 385 | created with owner-only permissions. Direct external changes can violate the | ||||||||||||||||||||||||||||
| 386 | backend's consistency guarantees. | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | =head1 SHARED FILESYSTEMS | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | With the default blob store, multiple processes on one host may use the same | ||||||||||||||||||||||||||||
| 391 | root when all processes use the same metadata store and it serializes their | ||||||||||||||||||||||||||||
| 392 | changes. This release is tested only on a local Linux filesystem. It makes no | ||||||||||||||||||||||||||||
| 393 | compatibility claim for multi-node filesystems such as NFS or CephFS. | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | =head1 METHODS | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | These methods implement L<Net::Blossom::Server::Storage>. | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | =head2 BUILDARGS | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | Internal C<Class::Tiny> constructor hook. | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | =head2 metadata_store | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | Returns the configured metadata store. | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | =head2 blob_store | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | Returns the configured blob store. | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | =head2 base_url | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | Returns the public base URL without a trailing slash. | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | =head2 cleanup_error_handler | ||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | Returns the post-commit cleanup error callback. | ||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | =head2 deploy_schema | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | Deploys the blob store and metadata store. | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | =head2 begin_upload | ||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | Returns a streaming upload writer coordinated with the metadata store. | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | =head2 get_blob | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | Returns a L<Net::Blossom::Server::BlobResult> or C<undef> when metadata or bytes | ||||||||||||||||||||||||||||
| 430 | are absent. | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | =head2 get_blob_range | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | Returns one requested byte range as a positioned, bounded file stream, or | ||||||||||||||||||||||||||||
| 435 | C<undef> when metadata or bytes are absent. | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | =head2 head_blob | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | Returns a L<Net::Blossom::BlobDescriptor> from metadata without opening bytes. | ||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | =head2 delete_blob | ||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | Deletes one owner or the complete blob. Returns true when the requested record | ||||||||||||||||||||||||||||
| 444 | existed. | ||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | =head2 list_blobs | ||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | Returns an array reference of descriptors in metadata-store order. | ||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | =cut | ||||||||||||||||||||||||||||