line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::PhotoBackup::Server; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
719
|
use 5.008001; |
|
1
|
|
|
|
|
3
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
19
|
|
5
|
1
|
|
|
1
|
|
12
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
33
|
|
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 Android app. It was developed by |
25
|
|
|
|
|
|
|
reading the L |
26
|
|
|
|
|
|
|
and looking at the sourcecode of the |
27
|
|
|
|
|
|
|
L and |
28
|
|
|
|
|
|
|
L python implementations. |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=cut |
31
|
|
|
|
|
|
|
|
32
|
1
|
|
|
1
|
|
888
|
use Data::Dumper; $Data::Dumper::Sortkeys = 1; |
|
1
|
|
|
|
|
6315
|
|
|
1
|
|
|
|
|
73
|
|
33
|
1
|
|
|
1
|
|
820
|
use Digest::SHA (); |
|
1
|
|
|
|
|
3509
|
|
|
1
|
|
|
|
|
22
|
|
34
|
1
|
|
|
1
|
|
704
|
use File::Copy (); |
|
1
|
|
|
|
|
2252
|
|
|
1
|
|
|
|
|
19
|
|
35
|
1
|
|
|
1
|
|
691
|
use File::HomeDir (); |
|
1
|
|
|
|
|
5403
|
|
|
1
|
|
|
|
|
17
|
|
36
|
1
|
|
|
1
|
|
5
|
use File::Spec (); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
17
|
|
37
|
1
|
|
|
1
|
|
749
|
use Plack::Request; |
|
1
|
|
|
|
|
66849
|
|
|
1
|
|
|
|
|
32
|
|
38
|
1
|
|
|
1
|
|
661
|
use Plack::Runner; |
|
1
|
|
|
|
|
7438
|
|
|
1
|
|
|
|
|
29
|
|
39
|
1
|
|
|
1
|
|
6
|
use Try::Tiny; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1549
|
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
our $VERSION = "5"; |
42
|
0
|
|
|
0
|
0
|
|
sub version { $VERSION } |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=head2 new() |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
Constructor. |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
Any args will be added to $self, overriding any defaults. |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=cut |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub new { |
53
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
54
|
0
|
|
|
|
|
|
my %args = @_; |
55
|
|
|
|
|
|
|
|
56
|
0
|
|
|
|
|
|
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
|
|
|
|
|
|
|
sub init { |
84
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
85
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
|
my $config = $self->config; |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
do { |
89
|
0
|
0
|
|
|
|
|
print "Media root - Where should the pictures be stored" . ($config->{MediaRoot} ? " [$config->{MediaRoot}]: " : ": "); |
90
|
0
|
|
|
|
|
|
my $media_root = ; |
91
|
0
|
|
|
|
|
|
chomp $media_root; |
92
|
0
|
0
|
|
|
|
|
$config->{MediaRoot} = $media_root unless $media_root eq ''; |
93
|
|
|
|
|
|
|
} |
94
|
0
|
|
|
|
|
|
while ( ! $config->{MediaRoot} ); |
95
|
0
|
0
|
|
|
|
|
if ( ! -d $config->{MediaRoot} ) { |
96
|
0
|
|
|
|
|
|
print "MediaRoot '$config->{MediaRoot}' does not exist. Creating...\n"; |
97
|
|
|
|
|
|
|
mkdir $config->{MediaRoot} |
98
|
0
|
0
|
|
|
|
|
or die "FATAL: Unable to create '$config->{MediaRoot}' - $!"; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
0
|
|
|
|
|
|
my $password; |
102
|
0
|
|
|
|
|
|
do { |
103
|
0
|
|
|
|
|
|
print "Server password - The password required for HTTP operations: "; |
104
|
0
|
|
|
|
|
|
system "stty -echo"; |
105
|
0
|
|
|
|
|
|
$password = ; |
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
|
|
0
|
|
|
|
print "Server port [" . ($config->{Port} || 8420) . "]: "; |
115
|
0
|
|
|
|
|
|
my $port = ; |
116
|
0
|
|
|
|
|
|
chomp $port; |
117
|
0
|
0
|
0
|
|
|
|
$config->{Port} = $port eq '' ? ($config->{Port} || 8420) : $port =~ m{ \A \d+ \z }xms ? $port : undef; |
|
|
0
|
|
|
|
|
|
118
|
|
|
|
|
|
|
} |
119
|
0
|
|
|
|
|
|
while ( ! $config->{Port} ); |
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
|
|
|
|
|
|
|
sub config { |
141
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
142
|
0
|
|
|
|
|
|
my $config = shift; |
143
|
|
|
|
|
|
|
|
144
|
0
|
|
|
|
|
|
my @required_keys = qw( MediaRoot Password Port ); |
145
|
|
|
|
|
|
|
|
146
|
0
|
0
|
|
|
|
|
if ($config) { |
147
|
|
|
|
|
|
|
|
148
|
0
|
|
|
|
|
|
foreach my $key (@required_keys) { |
149
|
0
|
0
|
|
|
|
|
die "config() config hashref arg missing '$key'. Got " . Dumper($config) unless $config->{$key}; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
open my $FH, '>', $self->{config_file} |
153
|
0
|
0
|
|
|
|
|
or die "config() unable to open config file '$self->{config_file}' for writing - $!"; |
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
|
0
|
|
|
|
|
close $FH |
162
|
|
|
|
|
|
|
or die "config() unable to close config file '$self->{config_file}' after writing - $!"; |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
else { |
165
|
0
|
0
|
|
|
|
|
if ( -f "$self->{config_file}" ) { |
166
|
|
|
|
|
|
|
open my $FH, '<', $self->{config_file} |
167
|
0
|
0
|
|
|
|
|
or die "config() unable to open config file '$self->{config_file}' for reading - $!"; |
168
|
0
|
|
|
|
|
|
my $in_section; |
169
|
0
|
|
|
|
|
|
LINE: foreach my $line ( <$FH> ) { |
170
|
0
|
|
|
|
|
|
chomp $line; |
171
|
0
|
0
|
|
|
|
|
if ( $in_section ) { |
|
|
0
|
|
|
|
|
|
172
|
0
|
0
|
|
|
|
|
if ( $line =~ m{ \A \s* \[ }xms ) { |
173
|
0
|
|
|
|
|
|
last LINE; |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
# MediaRoot can contain everything but NUL. |
176
|
0
|
0
|
|
|
|
|
if ( $line =~ m{ \A \s* MediaRoot \s* = \s* ([^\0]+) \s* \z }xms ) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
177
|
0
|
|
|
|
|
|
$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
|
0
|
|
|
|
|
|
$config->{Password} = $1; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
# Port is just digits. |
184
|
|
|
|
|
|
|
elsif ( $line =~ m{ \A \s* Port \s* = \s* (\d+) \s* \z }xms ) { |
185
|
0
|
|
|
|
|
|
$config->{Port} = $1; |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
elsif ( $line =~ m{ \A \s* \[ photobackup \] \s* \z }xms ) { |
189
|
0
|
|
|
|
|
|
$in_section = 1; |
190
|
0
|
|
|
|
|
|
next LINE; |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
else { |
193
|
0
|
|
|
|
|
|
next LINE; |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
} |
196
|
0
|
|
|
|
|
|
foreach my $key (@required_keys) { |
197
|
0
|
0
|
|
|
|
|
die "config() config hashref from file '$self->{config_file}' missing '$key'. Got " . Dumper($config) unless $config->{$key}; |
198
|
|
|
|
|
|
|
} |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
} |
201
|
0
|
|
|
|
|
|
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
|
|
|
|
|
|
|
sub run { |
211
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
212
|
|
|
|
|
|
|
|
213
|
0
|
0
|
|
|
|
|
$self->init unless $self->config; |
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
|
|
|
|
|
|
|
sub stop { |
239
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
240
|
|
|
|
|
|
|
|
241
|
0
|
0
|
|
|
|
|
return unless -f $self->{pid}; |
242
|
|
|
|
|
|
|
|
243
|
0
|
|
|
|
|
|
my $pid = do { local( @ARGV, $/ ) = $self->{pid} ; <> }; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
244
|
0
|
|
|
|
|
|
chomp $pid; |
245
|
|
|
|
|
|
|
|
246
|
0
|
0
|
|
|
|
|
kill 'TERM', $pid if $pid; |
247
|
|
|
|
|
|
|
|
248
|
0
|
|
|
|
|
|
unlink $self->{pid}; |
249
|
|
|
|
|
|
|
} |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
=head2 app() |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
Return the PSGI application subref. |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
=cut |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
sub app { |
258
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
259
|
0
|
|
0
|
|
|
|
my $config = shift || $self->config; |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
return sub { |
262
|
0
|
|
|
0
|
|
|
my $env = shift; # PSGI env |
263
|
|
|
|
|
|
|
|
264
|
0
|
|
|
|
|
|
my $req = Plack::Request->new($env); |
265
|
0
|
|
|
|
|
|
my $path_info = $req->path_info; |
266
|
0
|
|
|
|
|
|
my $method = $req->method; |
267
|
0
|
|
|
|
|
|
my $post_vars = $req->body_parameters; |
268
|
|
|
|
|
|
|
|
269
|
0
|
0
|
0
|
|
|
|
if ( $path_info eq '' || $path_info eq '/' ) { |
|
|
0
|
|
|
|
|
|
270
|
0
|
0
|
|
|
|
|
if ( $method eq 'GET' ) { |
|
|
0
|
|
|
|
|
|
271
|
|
|
|
|
|
|
# GET / : Redirect to https://photobackup.github.io/ |
272
|
0
|
|
|
|
|
|
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
|
0
|
0
|
|
|
|
if ( ! length $post_vars->{password} || $post_vars->{password} ne $config->{Password} ) { |
277
|
0
|
|
|
|
|
|
return [ 403, [], [ "403 - wrong password!" ] ]; |
278
|
|
|
|
|
|
|
} |
279
|
0
|
|
|
|
|
|
my $upload = $req->uploads->{upfile}; |
280
|
0
|
0
|
0
|
|
|
|
if ( ! $upload || ! -f $upload->path ) { |
281
|
0
|
|
|
|
|
|
return [ 401, [], [ "401 - no file in the request!" ] ]; |
282
|
|
|
|
|
|
|
} |
283
|
0
|
|
|
|
|
|
my $filesize = $req->body_parameters->{filesize}; |
284
|
0
|
0
|
|
|
|
|
if ( ! $filesize ) { |
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
|
0
|
0
|
0
|
|
|
|
if ( ! length $post_vars->{password} || $post_vars->{password} ne $config->{Password} ) { |
297
|
0
|
|
|
|
|
|
return [ 403, [], [ "403 - wrong password!"]]; |
298
|
|
|
|
|
|
|
} |
299
|
0
|
0
|
|
|
|
|
if ( ! -d $config->{MediaRoot} ) { |
300
|
0
|
|
|
|
|
|
return [ 500, [], [ "500 - MediaRoot '$config->{MediaRoot}' does not exist" ]]; |
301
|
|
|
|
|
|
|
} |
302
|
0
|
|
|
|
|
|
my $tmp_file = File::Spec->catfile($config->{MediaRoot}, '__photobackup_test_file_' . $$); |
303
|
|
|
|
|
|
|
try { |
304
|
0
|
|
|
|
|
|
open my $FH, '>', $tmp_file; |
305
|
0
|
|
|
|
|
|
print $FH 'TEST'; |
306
|
0
|
|
|
|
|
|
unlink $tmp_file; |
307
|
0
|
|
|
|
|
|
return [ 200, [], [ "200 - All tests passed" ]]; |
308
|
|
|
|
|
|
|
} |
309
|
|
|
|
|
|
|
catch { |
310
|
0
|
|
|
|
|
|
return [ 500, [], [ "500 - Can't write to MediaRoot '$config->{MediaRoot}'" ]]; |
311
|
0
|
|
|
|
|
|
}; |
312
|
|
|
|
|
|
|
} |
313
|
|
|
|
|
|
|
|
314
|
0
|
|
|
|
|
|
}; |
315
|
|
|
|
|
|
|
} |
316
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
1; |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
__END__ |