-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfotostore.pl
executable file
·361 lines (277 loc) · 9.62 KB
/
fotostore.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/usr/bin/env perl
use strict;
use warnings;
use lib 'lib';
use Mojolicious::Lite; # app, get, post is exported.
use Mojo::Promise;
use Mojo::IOLoop;
use File::Basename qw/basename fileparse/;
use File::Path 'mkpath';
use File::Spec 'catfile';
use Cwd;
use POSIX;
use Imager;
use DBI;
use Digest::SHA;
use FotoStore::DB;
use Data::Dumper;
$Data::Dumper::Maxdepth = 2;
my $config = plugin 'Config' => { file => 'application.conf' };
my $db = FotoStore::DB->new( $config->{'db_file'} );
# Image base URL
my $IMAGE_BASE = 'images';
my $ORIG_DIR = 'orig';
# set allowed thumbnails scale and image scales
my $thumbs_size = $config->{'thumbnails_size'};
my $scales_map = $config->{'image_scales'};
$scales_map->{$thumbs_size} = 1;
#Sort and filter values for array of available scales
my @scale_width =
map { $scales_map->{$_} == 1 ? $_ : undef }
sort { $a <=> $b } keys(%$scales_map);
my $sha = Digest::SHA->new('sha256');
# Directory to save image files
my $IMAGE_DIR = File::Spec->catfile( getcwd(), 'public', $IMAGE_BASE );
my $log = Mojo::Log->new();
plugin 'authentication', {
autoload_user => 1,
load_user => sub {
my $self = shift;
my $uid = shift;
return $db->get_user($uid);
},
validate_user => sub {
my $self = shift;
my $username = shift || '';
my $password = shift || '';
my $extradata = shift || {};
my $digest = $sha->add($password);
my $user_id = $db->check_user( $username, $digest->hexdigest() );
return $user_id;
},
};
post '/login' => sub {
my $self = shift;
my $u = $self->req->param('username');
my $p = $self->req->param('password');
if ( $self->authenticate( $u, $p ) ) {
$self->redirect_to('/');
}
else {
$self->render( text => 'Login failed :(' );
}
};
get '/logout' => sub {
my $self = shift;
$self->logout();
$self->render( message => 'bye' );
};
get '/register' => ( authenticated => 0 ) => sub {
};
post '/register' => ( authenticated => 0 ) => sub {
my $self = shift;
my $username = $self->req->param('username') || "";
my $password = $self->req->param('password') || "";
my $fullname = $self->req->param('fullname') || "";
my $invite = $self->req->param('invite') || "";
if ( $invite eq $config->{'invite_code'} ) {
#chek that username is not taken
my $user = $db->get_user($username);
if ( $user->{'user_id'} > 0 ) {
$self->render(
template => 'error',
message => 'Username already taken!'
);
return 0;
}
if ( $fullname eq '' ) {
$fullname = $username;
}
my $digest = $sha->add($password);
$db->add_user( $username, $digest->hexdigest(), $fullname );
#Authenticate user after add
if ( $self->authenticate( $username, $password ) ) {
$self->redirect_to('/');
}
else {
$self->render( template => 'error', message => 'Login failed :(' );
}
}
else {
$self->render( template => 'error', message => 'invalid invite code' );
}
};
# Display top page
get '/' => sub {
my $self = shift;
my $current_user = $self->current_user;
} => 'index';
get '/get_images' => ( authenticated => 1 ) => sub {
my $self = shift;
#Getting current user
my $current_user = $self->current_user;
my $user_id = $current_user->{'user_id'};
#Getting images list with paging
my $page = $self->param('page') || 1;
my $items = $self->param('per-page') || 20;
if (($page !~ /^\d+$/) || ($page <= 1)) { $page = 1}
if (($items !~ /^\d+$/) || ($items <= 0)) { $items = 20}
# process images list
my $req_result = $db->get_files( $current_user->{'user_id'}, $items , $page);
my $files_list = $req_result->{'images_list'};
my $pages_count = ceil($req_result->{'total_rows'}/$items);
my $thumbs_dir =
File::Spec->catfile( $IMAGE_DIR, $current_user->{'user_id'},
$thumbs_size );
my @images = map { $_->{'file_name'} } @$files_list;
my $images = [];
for my $img_item (@$files_list) {
my $file = $img_item->{'file_name'};
my $img_hash = {};
$img_hash->{'id'} = $img_item->{'file_id'};
$img_hash->{'filename'} = $img_item->{'original_filename'};
$img_hash->{'original_url'} =
File::Spec->catfile( '/', $IMAGE_BASE, $current_user->{'user_id'},
$ORIG_DIR, $file );
$img_hash->{'thumbnail_url'} =
File::Spec->catfile( '/', $IMAGE_BASE, $current_user->{'user_id'},
$thumbs_size, $file );
my @scaled = ();
for my $scale (@scale_width) {
if ( -r File::Spec->catfile( get_path( $user_id, $scale ), $file ) )
{
push(
@scaled,
{
'size' => $scale,
'url' => File::Spec->catfile(
'/', $IMAGE_BASE, $user_id, $scale, $file
)
}
);
}
}
$img_hash->{'scales'} = \@scaled;
push( @$images, $img_hash );
}
my $reply_data = { current_page => $page, items_per_page => $items, pages_count => $pages_count, images_list => $images };
# Render
return $self->render( json => $reply_data );
};
# Upload image file
# There is no restriction for file size in app because restriction is present in nginx configuration
post '/upload' => ( authenticated => 1 ) => sub {
my $self = shift;
# Uploaded image(Mojo::Upload object)
my $image = $self->req->upload('image');
my $user = $self->current_user();
my $user_id = $user->{'user_id'};
# Not upload
unless ($image) {
return $self->render(
template => 'error',
message => "Upload fail. File is not specified."
);
}
# Check file type
my $image_type = $image->headers->content_type;
my %valid_types = (
'image/gif' => 'gif',
'image/jpeg' => 'jpg',
'image/png' => 'png'
);
# Content type is wrong
unless ( $valid_types{$image_type} ) {
return $self->render(
template => 'error',
message => "Upload fail. Content type is wrong."
);
}
my $ext = $valid_types{$image_type};
# Image file
my $filename = sprintf( '%s.%s', create_hash( $image->slurp() ), $ext );
my $image_file =
File::Spec->catfile( get_path( $user_id, $ORIG_DIR ), $filename );
# Save to file
$image->move_to($image_file);
my $promise = store_image($image_file, $image->filename, $user_id);
#TODO: add errors handling
Mojo::Promise->all($promise)->then(sub {
$self->render(
json => {
files => [
{
name => $image->filename,
size => $image->size,
url => sprintf( '/images/orig/%s', $filename ),
thumbnailUrl => sprintf( '/images/200/%s', $filename ),
}
]
}
);
})->wait;
} => 'upload';
sub create_hash {
my $data_to_hash = shift;
$sha->add($data_to_hash);
return $sha->hexdigest();
}
sub get_path {
my ( $user_id, $size ) = @_;
my $path = File::Spec->catfile( $IMAGE_DIR, $user_id, $size );
unless ( -d $path ) {
mkpath $path or die "Cannot create directory: $path";
}
return $path;
}
sub store_image {
my $image_file = shift;
my $original_filename = shift;
my $user_id = shift;
my $promise = Mojo::Promise->new;
# Process and store uploaded file in a separate process
Mojo::IOLoop->subprocess(
sub {
my $subprocess = shift;
my $filename = fileparse($image_file);
my $imager = Imager->new();
$imager->read( file => $image_file ) or die $imager->errstr;
#http://sylvana.net/jpegcrop/exif_orientation.html
#http://myjaphoo.de/docs/exifidentifiers.html
my $rotation_angle = $imager->tags( name => "exif_orientation" ) || 1;
$log->debug(
"Rotation angle [" . $rotation_angle . "]" );
if ( $rotation_angle == 3 ) {
$imager = $imager->rotate( degrees => 180 );
}
elsif ( $rotation_angle == 6 ) {
$imager = $imager->rotate( degrees => 90 );
}
my $original_width = $imager->getwidth();
for my $scale (@scale_width) {
#Skip sizes which more than original image
if ( $scale >= $original_width ) {
next;
}
my $scaled = $imager->scale( xpixels => $scale );
$scaled->write( file =>
File::Spec->catfile( get_path( $user_id, $scale ), $filename ) )
or die $scaled->errstr;
}
if ( !$db->add_file( $user_id, $filename, $original_filename ) ) {
$log->error(sprintf('Can\'t save file %s', $filename));
die sprintf('Can\'t save file %s', $filename);
}
return $filename;
},
sub {
my ($subprocess, $err, @results) = @_;
$log->error("Subprocess error: $err") and return if $err;
$promise->reject("Subprocess error: $err @results") if $err;
$promise->resolve(1, @results);
}
);
return $promise;
}
Mojo::IOLoop->start;
app->start;