| 1 | package Net::PhotoBackup::Server; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use 5.008001; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | =encoding utf-8 | ||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | Net::PhotoBackup::Server - perl5 server for https://photobackup.github.io | ||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | # Initial setup of server config. | ||||||||||||||||||||||||||||||||
| 16 | photobackup.pl init | ||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | # Launch server using config. | ||||||||||||||||||||||||||||||||
| 19 | photobackup.pl run | ||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | Net::PhotoBackup::Server provides a server and startup script for | ||||||||||||||||||||||||||||||||
| 24 | L<PhotoBackup|https://photobackup.github.io/> Android app. It was developed by | ||||||||||||||||||||||||||||||||
| 25 | reading the L<API docs|https://github.com/PhotoBackup/api/blob/master/api.raml> | ||||||||||||||||||||||||||||||||
| 26 | and looking at the sourcecode of the | ||||||||||||||||||||||||||||||||
| 27 | L<bottle|https://github.com/PhotoBackup/server-bottle> and | ||||||||||||||||||||||||||||||||
| 28 | L<flask|https://github.com/PhotoBackup/server-flask> python implementations. | ||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||
| 30 | =cut | ||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | 2 | ▶ | use Data::Dumper; $Data::Dumper::Sortkeys = 1; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 33 | 2 | ▶ | use Digest::SHA (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 34 | 2 | ▶ | use File::Copy (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 35 | 2 | ▶ | use File::HomeDir (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 36 | 2 | ▶ | use File::Spec (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 37 | 2 | ▶ | use Plack::Request; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 38 | 2 | ▶ | use Plack::Runner; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 39 | 2 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | 2 | our $VERSION = "0.11"; | |||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | sub version { $VERSION } | ||||||||||||||||||||||||||||||
|
Subroutine
version: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | =head2 new() | ||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | Constructor. | ||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | Any args will be added to $self, overriding any defaults. | ||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | =cut | ||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 53 | 1 | my $class = shift; | |||||||||||||||||||||||||||||||
| 54 | 1 | my %args = @_; | |||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | 1 | return bless { | |||||||||||||||||||||||||||||||
| 57 | config_file => File::Spec->catfile(File::HomeDir->my_home, '.photobackup'), | ||||||||||||||||||||||||||||||||
| 58 | pid => File::Spec->catfile(File::HomeDir->my_home, '.photobackup.pid'), | ||||||||||||||||||||||||||||||||
| 59 | env => 'deployment', | ||||||||||||||||||||||||||||||||
| 60 | daemonize => 1, | ||||||||||||||||||||||||||||||||
| 61 | workers => 3, | ||||||||||||||||||||||||||||||||
| 62 | %args, | ||||||||||||||||||||||||||||||||
| 63 | }, $class; | ||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | =head2 init() | ||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | Create, or recreate the user's config file. | ||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | The user will be prompted for the following information: | ||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | Media root - Directory where the pictures will be stored. | ||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | Server password - The password to use for all HTTP operations. | ||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | Server port - Defaults to 8420. | ||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | Some rudimentary checking will be done for valid input. | ||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | =cut | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | sub init { | ||||||||||||||||||||||||||||||
|
Subroutine
init: not called CC=12 SCAR=50.5
Pod
documented
| |||||||||||||||||||||||||||||||||
| 84 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | 0 | my $config = $self->config; | |||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | do { | ||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | print "Media root - Where should the pictures be stored" . ($config->{MediaRoot} ? " [$config->{MediaRoot}]: " : ": "); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 90 | 0 | my $media_root = <STDIN>; | |||||||||||||||||||||||||||||||
| 91 | 0 | chomp $media_root; | |||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | $config->{MediaRoot} = $media_root unless $media_root eq ''; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | while ( ! $config->{MediaRoot} ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | if ( ! -d $config->{MediaRoot} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 96 | 0 | print "MediaRoot '$config->{MediaRoot}' does not exist. Creating...\n"; | |||||||||||||||||||||||||||||||
| 97 | mkdir $config->{MediaRoot} | ||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | or die "FATAL: Unable to create '$config->{MediaRoot}' - $!"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||
| 101 | 0 | my $password; | |||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | do { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 103 | 0 | print "Server password - The password required for HTTP operations: "; | |||||||||||||||||||||||||||||||
| 104 | 0 | system "stty -echo"; | |||||||||||||||||||||||||||||||
| 105 | 0 | $password = <STDIN>; | |||||||||||||||||||||||||||||||
| 106 | 0 | chomp $password; | |||||||||||||||||||||||||||||||
| 107 | 0 | print "\n"; | |||||||||||||||||||||||||||||||
| 108 | 0 | system "stty echo"; | |||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||
| 110 | while ( ! $password ); | ||||||||||||||||||||||||||||||||
| 111 | 0 | $config->{Password} = Digest::SHA::sha512_hex $password; | |||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | do { | ||||||||||||||||||||||||||||||||
| 114 | 0 | ▶ | print "Server port [" . ($config->{Port} || 8420) . "]: "; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$config->{'Port'} || 8420
MC/DC0%0/1
$config->{'Port'} || 8420
$config->{'Port'}
Truth table0%0/2
A
A= $config->{'Port'}
| |||||||||||||||||||||||||||||||||
| 115 | 0 | my $port = <STDIN>; | |||||||||||||||||||||||||||||||
| 116 | 0 | chomp $port; | |||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | $config->{Port} = $port eq '' ? ($config->{Port} || 8420) : $port =~ m{ \A \d+ \z }xms ? $port : undef; | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/2
$config->{"Port"} or 8420
MC/DC0%0/1
$config->{"Port"} or 8420
$config->{"Port"}
Truth table0%0/2
A
A= $config->{"Port"}
| |||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | while ( ! $config->{Port} ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||
| 121 | 0 | $self->config($config); | |||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | 0 | print "\nConfig written. Launch PhotoBackup server with 'photobackup.pl run'\n"; | |||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||
| 126 | =head2 config() | ||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||
| 128 | Read and write server config file. | ||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | Returns undef if config file doesn't exist, or doesn't hold all required | ||||||||||||||||||||||||||||||||
| 131 | data. | ||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||
| 133 | The config will be written to ~/.photobackup in INI format. | ||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||
| 135 | I'm reading and writing this simple INI file manually rather than using a | ||||||||||||||||||||||||||||||||
| 136 | CPAN module so as to reduce the dependencies. | ||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||
| 138 | =cut | ||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||
| 140 | 1 | ▶ | sub config { | ||||||||||||||||||||||||||||||
|
Subroutine
config: called CC=18 SCAR=35.6
Pod
documented
| |||||||||||||||||||||||||||||||||
| 141 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||
| 142 | 1 | my $config = shift; | |||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||
| 144 | 1 | my @required_keys = qw( MediaRoot Password Port ); | |||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||
| 146 | 1 | ▶ | if ($config) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||
| 148 | 0 | foreach my $key (@required_keys) { | |||||||||||||||||||||||||||||||
| 149 | 0 | ▶ | die "config() config hashref arg missing '$key'. Got " . Dumper($config) unless $config->{$key}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||
| 152 | open my $FH, '>', $self->{config_file} | ||||||||||||||||||||||||||||||||
| 153 | 0 | ▶ | or die "config() unable to open config file '$self->{config_file}' for writing - $!"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||
| 155 | 0 | print $FH "# Settings for Net::PhotoBackup::Server - perldoc Net::PhotoBackup::Server\n"; | |||||||||||||||||||||||||||||||
| 156 | 0 | print $FH "[photobackup]\n"; | |||||||||||||||||||||||||||||||
| 157 | 0 | foreach my $key (@required_keys) { | |||||||||||||||||||||||||||||||
| 158 | 0 | print $FH "$key=$config->{$key}\n"; | |||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||
| 161 | 0 | ▶ | close $FH | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 162 | or die "config() unable to close config file '$self->{config_file}' after writing - $!"; | ||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||
| 164 | else { | ||||||||||||||||||||||||||||||||
| 165 | 1 | ▶ | if ( -f "$self->{config_file}" ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 166 | open my $FH, '<', $self->{config_file} | ||||||||||||||||||||||||||||||||
| 167 | 1 | ▶ | or die "config() unable to open config file '$self->{config_file}' for reading - $!"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 168 | 1 | my $in_section; | |||||||||||||||||||||||||||||||
| 169 | 1 | LINE: foreach my $line ( <$FH> ) { | |||||||||||||||||||||||||||||||
| 170 | 6 | chomp $line; | |||||||||||||||||||||||||||||||
| 171 | 6 | ▶ | if ( $in_section ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 172 | 3 | ▶ | if ( $line =~ m{ \A \s* \[ }xms ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 173 | 0 | last LINE; | |||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||
| 175 | # MediaRoot can contain everything but NUL. | ||||||||||||||||||||||||||||||||
| 176 | 3 | ▶ | if ( $line =~ m{ \A \s* MediaRoot \s* = \s* ([^\0]+) \s* \z }xms ) { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 177 | 1 | $config->{MediaRoot} = $1; | |||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||
| 179 | # Password is 128 hex digits only. | ||||||||||||||||||||||||||||||||
| 180 | elsif( $line =~ m{ \A \s* Password \s* = \s* ([0-9A-F]{128}) \s* \z }ixms ) { | ||||||||||||||||||||||||||||||||
| 181 | 1 | $config->{Password} = $1; | |||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||
| 183 | # Port is just digits. | ||||||||||||||||||||||||||||||||
| 184 | elsif ( $line =~ m{ \A \s* Port \s* = \s* (\d+) \s* \z }xms ) { | ||||||||||||||||||||||||||||||||
| 185 | 1 | $config->{Port} = $1; | |||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||
| 188 | elsif ( $line =~ m{ \A \s* \[ photobackup \] \s* \z }xms ) { | ||||||||||||||||||||||||||||||||
| 189 | 1 | $in_section = 1; | |||||||||||||||||||||||||||||||
| 190 | 1 | next LINE; | |||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||
| 192 | else { | ||||||||||||||||||||||||||||||||
| 193 | 2 | next LINE; | |||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||
| 196 | 1 | foreach my $key (@required_keys) { | |||||||||||||||||||||||||||||||
| 197 | 3 | ▶ | die "config() config hashref from file '$self->{config_file}' missing '$key'. Got " . Dumper($config) unless $config->{$key}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||
| 201 | 1 | return $config; | |||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||
| 204 | =head2 run() | ||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||
| 206 | Launch the PhotoBackup web service using config from the conf file. | ||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||
| 208 | =cut | ||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||
| 210 | 0 | ▶ | sub run { | ||||||||||||||||||||||||||||||
|
Subroutine
run: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 211 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||
| 213 | 0 | ▶ | $self->init unless $self->config; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||
| 215 | 0 | my $config = $self->config; | |||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||
| 217 | my $runner = Plack::Runner->new( | ||||||||||||||||||||||||||||||||
| 218 | daemonize => $self->{daemonize}, | ||||||||||||||||||||||||||||||||
| 219 | env => $self->{env}, | ||||||||||||||||||||||||||||||||
| 220 | 0 | server => 'Starman', | |||||||||||||||||||||||||||||||
| 221 | version_cb => \&version | ||||||||||||||||||||||||||||||||
| 222 | ); | ||||||||||||||||||||||||||||||||
| 223 | $runner->parse_options( | ||||||||||||||||||||||||||||||||
| 224 | '--port' => $config->{Port}, | ||||||||||||||||||||||||||||||||
| 225 | '--workers' => $self->{workers}, | ||||||||||||||||||||||||||||||||
| 226 | '--pid' => $self->{pid}, | ||||||||||||||||||||||||||||||||
| 227 | 0 | ); | |||||||||||||||||||||||||||||||
| 228 | 0 | $runner->run( $self->app ); | |||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||
| 232 | =head2 stop() | ||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||
| 234 | Kill any running PhotoBackup web service. | ||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||
| 236 | =cut | ||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||
| 238 | 0 | ▶ | sub stop { | ||||||||||||||||||||||||||||||
|
Subroutine
stop: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 239 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||
| 241 | 0 | ▶ | return unless -f $self->{pid}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||
| 243 | 0 | my $pid = do { local( @ARGV, $/ ) = $self->{pid} ; <> }; | |||||||||||||||||||||||||||||||
| 244 | 0 | chomp $pid; | |||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||
| 246 | 0 | ▶ | kill 'TERM', $pid if $pid; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||
| 248 | 0 | unlink $self->{pid}; | |||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||
| 251 | =head2 app() | ||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||
| 253 | Return the PSGI application subref. | ||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||
| 255 | =cut | ||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||
| 257 | 1 | ▶ | sub app { | ||||||||||||||||||||||||||||||
|
Subroutine
app: called CC=2 SCAR=9.2
Pod
documented
| |||||||||||||||||||||||||||||||||
| 258 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||
| 259 | 1 | ▶ | my $config = shift || $self->config; | ||||||||||||||||||||||||||||||
|
Condition33%1/3
shift() || $self->config
MC/DC0%0/2
shift() || $self->config
shift()
$self->config
Truth table33%1/3
A || B
A= shift()B= $self->config
| |||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||
| 261 | 4 | ▶ | return sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=14 SCAR=37.1
| |||||||||||||||||||||||||||||||||
| 262 | 4 | my $env = shift; # PSGI env | |||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||
| 264 | 4 | my $req = Plack::Request->new($env); | |||||||||||||||||||||||||||||||
| 265 | 4 | my $path_info = $req->path_info; | |||||||||||||||||||||||||||||||
| 266 | 4 | my $method = $req->method; | |||||||||||||||||||||||||||||||
| 267 | 4 | my $post_vars = $req->body_parameters; | |||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||
| 269 | 4 | ▶ | if ( $path_info eq '' || $path_info eq '/' ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$path_info eq "" or $path_info eq "/"
MC/DC50%1/2
$path_info eq "" or $path_info eq "/"
$path_info eq ""
$path_info eq "/"
Truth table66%2/3
A or B
A= $path_info eq ""B= $path_info eq "/"
| |||||||||||||||||||||||||||||||||
| 270 | 1 | ▶ | if ( $method eq 'GET' ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 271 | # GET / : Redirect to https://photobackup.github.io/ | ||||||||||||||||||||||||||||||||
| 272 | 1 | return [301, [ Location => 'https://photobackup.github.io/' ], []]; | |||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||
| 274 | elsif ( $method eq 'POST' ) { | ||||||||||||||||||||||||||||||||
| 275 | # POST / : Store new image file in MediaRoot. Needs password. | ||||||||||||||||||||||||||||||||
| 276 | 0 | ▶ | if ( ! length $post_vars->{password} || $post_vars->{password} ne $config->{Password} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not length $post_vars->{"password"} or $post_vars->{"password"} ne $config->{"Password"}
MC/DC0%0/2
not length $post_vars->{"password"} or $post_vars->{"password"} ne $config->{"Password"}
not length $post_vars->{"password"}
$post_vars->{"password"} ne $config->{"Password"}
Truth table0%0/3
not(A) or B
A= not length $post_vars->{"password"}B= $post_vars->{"password"} ne $config->{"Password"}
| |||||||||||||||||||||||||||||||||
| 277 | 0 | return [ 403, [], [ "403 - wrong password!" ] ]; | |||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||
| 279 | 0 | my $upload = $req->uploads->{upfile}; | |||||||||||||||||||||||||||||||
| 280 | 0 | ▶ | if ( ! $upload || ! -f $upload->path ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$upload and -f $upload->path
MC/DC0%0/2
$upload and -f $upload->path
$upload
-f $upload->path
Truth table0%0/3
A and B
A= $uploadB= -f $upload->path
| |||||||||||||||||||||||||||||||||
| 281 | 0 | return [ 401, [], [ "401 - no file in the request!" ] ]; | |||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||
| 283 | 0 | my $filesize = $req->body_parameters->{filesize}; | |||||||||||||||||||||||||||||||
| 284 | 0 | ▶ | if ( ! $filesize ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 285 | 0 | return [ 400, [], [ "400 - missing file size in the request!" ] ]; | |||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||
| 287 | 0 | my $store_path = File::Spec->catfile($config->{MediaRoot}, $upload->basename); | |||||||||||||||||||||||||||||||
| 288 | 0 | File::Copy::move $upload->path, $store_path; | |||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||
| 290 | 0 | return [ 200, [], [ "200 - file stored" ] ]; | |||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||
| 294 | elsif ( $path_info eq '/test' ) { | ||||||||||||||||||||||||||||||||
| 295 | # POST /test : Check password, then attempt to write test file to MediaRoot. | ||||||||||||||||||||||||||||||||
| 296 | 3 | ▶ | if ( ! length $post_vars->{password} || $post_vars->{password} ne $config->{Password} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not length $post_vars->{"password"} or $post_vars->{"password"} ne $config->{"Password"}
MC/DC100%2/2
not length $post_vars->{"password"} or $post_vars->{"password"} ne $config->{"Password"}
not length $post_vars->{"password"}
$post_vars->{"password"} ne $config->{"Password"}
Truth table100%3/3
not(A) or B
A= not length $post_vars->{"password"}B= $post_vars->{"password"} ne $config->{"Password"}
| |||||||||||||||||||||||||||||||||
| 297 | 2 | return [ 403, [], [ "403 - wrong password!"]]; | |||||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||||
| 299 | 1 | ▶ | if ( ! -d $config->{MediaRoot} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 300 | 0 | return [ 500, [], [ "500 - MediaRoot '$config->{MediaRoot}' does not exist" ]]; | |||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||
| 302 | 1 | my $tmp_file = File::Spec->catfile($config->{MediaRoot}, '__photobackup_test_file_' . $$); | |||||||||||||||||||||||||||||||
| 303 | 1 | ▶ | try { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 304 | 1 | open my $FH, '>', $tmp_file; | |||||||||||||||||||||||||||||||
| 305 | 1 | print $FH 'TEST'; | |||||||||||||||||||||||||||||||
| 306 | 1 | unlink $tmp_file; | |||||||||||||||||||||||||||||||
| 307 | 1 | return [ 200, [], [ "200 - All tests passed" ]]; | |||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||
| 309 | 0 | ▶ | catch { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 310 | 0 | return [ 500, [], [ "500 - Can't write to MediaRoot '$config->{MediaRoot}'" ]]; | |||||||||||||||||||||||||||||||
| 311 | 1 | }; | |||||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||
| 314 | 1 | }; | |||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||
| 317 | 2 | 1; | |||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||
| 319 | __END__ | ||||||||||||||||||||||||||||||||