Compare commits
2 commits
Author | SHA1 | Date | |
---|---|---|---|
|
140a179a8b | ||
|
a98302199a |
13 changed files with 100 additions and 205 deletions
108
fotostore.pl
108
fotostore.pl
|
@ -38,8 +38,8 @@ $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);
|
||||
map { $scales_map->{$_} == 1 ? $_ : undef }
|
||||
sort { $a <=> $b } keys(%$scales_map);
|
||||
|
||||
my $sha = Digest::SHA->new('sha256');
|
||||
|
||||
|
@ -156,31 +156,31 @@ get '/get_images' => ( authenticated => 1 ) => sub {
|
|||
|
||||
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 );
|
||||
File::Spec->catfile( $IMAGE_DIR, $current_user->{'user_id'},
|
||||
$thumbs_size );
|
||||
|
||||
my @images = map { $_->file_name } @$files_list;
|
||||
my @images = map { $_->{'file_name'} } @$files_list;
|
||||
|
||||
my $images = [];
|
||||
|
||||
for my $img_item (@$files_list) {
|
||||
my $file = $img_item->file_name;
|
||||
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->{'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 );
|
||||
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 );
|
||||
File::Spec->catfile( '/', $IMAGE_BASE, $current_user->{'user_id'},
|
||||
$thumbs_size, $file );
|
||||
|
||||
my @scaled = ();
|
||||
for my $scale (@scale_width) {
|
||||
|
@ -196,13 +196,14 @@ get '/get_images' => ( authenticated => 1 ) => sub {
|
|||
}
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$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 };
|
||||
|
@ -251,52 +252,32 @@ post '/upload' => ( authenticated => 1 ) => sub {
|
|||
# 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 );
|
||||
File::Spec->catfile( get_path( $user_id, $ORIG_DIR ), $filename );
|
||||
|
||||
# Save to file
|
||||
$image->move_to($image_file);
|
||||
|
||||
my $imager = Imager->new();
|
||||
$imager->read( file => $image_file ) or die $imager->errstr;
|
||||
|
||||
my $promise = store_image($imager , $filename, $image->filename, $user_id);
|
||||
|
||||
|
||||
my $promise = store_image($image_file, $image->filename, $user_id);
|
||||
|
||||
#TODO: add errors handling
|
||||
Mojo::Promise->all($promise)->then(sub {
|
||||
my $res = shift;
|
||||
save_tags($imager, $res->[1]);
|
||||
|
||||
$self->render(
|
||||
json => {
|
||||
files => [
|
||||
{
|
||||
name => $image->filename,
|
||||
size => $image->size,
|
||||
url => sprintf( '/images/orig/%s', $filename ),
|
||||
thumbnailUrl => sprintf( '/images/200/%s', $filename ),
|
||||
}
|
||||
]
|
||||
}
|
||||
);
|
||||
json => {
|
||||
files => [
|
||||
{
|
||||
name => $image->filename,
|
||||
size => $image->size,
|
||||
url => sprintf( '/images/orig/%s', $filename ),
|
||||
thumbnailUrl => sprintf( '/images/200/%s', $filename ),
|
||||
}
|
||||
]
|
||||
}
|
||||
);
|
||||
})->wait;
|
||||
|
||||
} => 'upload';
|
||||
|
||||
post '/album' => ( authenticated => 1 ) => sub {
|
||||
my $self = shift;
|
||||
|
||||
my $user_id = $self->current_user()->{'user_id'};
|
||||
my $album_name = $self->req->param('album_name');
|
||||
my $album_desc = $self->req->param('album_desc') || '';
|
||||
|
||||
my $album = $db->add_album($user_id, $album_name, $album_desc);
|
||||
$self->render(
|
||||
json => { album_id => $album->album_id,
|
||||
album_name => $album->album_name
|
||||
}
|
||||
)
|
||||
};
|
||||
|
||||
sub create_hash {
|
||||
my $data_to_hash = shift;
|
||||
|
||||
|
@ -314,8 +295,7 @@ sub get_path {
|
|||
}
|
||||
|
||||
sub store_image {
|
||||
my $imager = shift;
|
||||
my $filename = shift;
|
||||
my $image_file = shift;
|
||||
my $original_filename = shift;
|
||||
my $user_id = shift;
|
||||
|
||||
|
@ -323,6 +303,12 @@ sub store_image {
|
|||
# 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;
|
||||
|
@ -349,17 +335,16 @@ sub store_image {
|
|||
|
||||
$scaled->write( file =>
|
||||
File::Spec->catfile( get_path( $user_id, $scale ), $filename ) )
|
||||
or die $scaled->errstr;
|
||||
or die $scaled->errstr;
|
||||
}
|
||||
|
||||
my $file_info = $db->add_file( $user_id, $filename, $original_filename );
|
||||
if ( !defined $file_info ) {
|
||||
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 $file_info->file_id;
|
||||
return $filename;
|
||||
},
|
||||
sub {
|
||||
my ($subprocess, $err, @results) = @_;
|
||||
|
@ -372,16 +357,5 @@ sub store_image {
|
|||
return $promise;
|
||||
}
|
||||
|
||||
sub save_tags {
|
||||
my $image = shift;
|
||||
my $db_file_id = shift;
|
||||
|
||||
my @tags = $image->tags();
|
||||
|
||||
my %tags_data = map { $_->[0] => $_->[1]} @tags;
|
||||
$db->save_tags($db_file_id, \%tags_data);
|
||||
|
||||
}
|
||||
|
||||
Mojo::IOLoop->start;
|
||||
app->start;
|
||||
|
|
|
@ -4,30 +4,24 @@ use v5.20;
|
|||
use strict;
|
||||
use warnings;
|
||||
|
||||
use feature qw(signatures say);
|
||||
use feature qw(signatures);
|
||||
no warnings qw(experimental::signatures);
|
||||
|
||||
use Data::Dumper;
|
||||
use DBIx::Struct qw(connector);
|
||||
|
||||
|
||||
|
||||
sub new {
|
||||
my $class = shift;
|
||||
my $db_file = shift;
|
||||
|
||||
my $dbix = DBIx::Struct::connect(sprintf('dbi:SQLite:dbname=%s', $db_file),"","");
|
||||
|
||||
my $dbh = DBI->connect(sprintf('dbi:SQLite:dbname=%s', $db_file),"","");
|
||||
my $self = {
|
||||
dbix_connector => $dbix,
|
||||
dbh => $dbh,
|
||||
};
|
||||
bless $self, $class;
|
||||
return $self;
|
||||
}
|
||||
|
||||
sub check_user ($self, $nickname, $password) {
|
||||
my $row = one_row('users', { nickname => $nickname, password => $password });
|
||||
return $row->user_id;
|
||||
my ($user_id) = $self->{'dbh'}->selectrow_array(q~select user_id from users where nickname=? and password=?~, undef, ($nickname, $password));
|
||||
return $user_id;
|
||||
}
|
||||
|
||||
sub get_user ($self, $user_id) {
|
||||
|
@ -39,31 +33,31 @@ sub get_user ($self, $user_id) {
|
|||
}
|
||||
|
||||
sub _get_user_by_user_id ($self, $user_id) {
|
||||
my $row = one_row('users', {user_id => $user_id}) || return {};
|
||||
return {user_id => $row->user_id, nickname => $row->nickname, fullname => $row->fullname, timestamp => $row->timestamp};
|
||||
my $user_data = $self->{'dbh'}->selectrow_hashref(q~select user_id, nickname, fullname, timestamp from users where user_id=?~, {}, ($user_id));
|
||||
return $user_data;
|
||||
}
|
||||
|
||||
sub _get_user_by_username($self, $username) {
|
||||
my $row = one_row('users', {nickname => $username}) || return {};
|
||||
return {user_id => $row->user_id, nickname => $row->nickname, fullname => $row->fullname, timestamp => $row->timestamp};
|
||||
my $user_data = $self->{'dbh'}->selectrow_hashref(q~select user_id, nickname, fullname, timestamp from users where nickname=?~, {}, ($username));
|
||||
return $user_data;
|
||||
}
|
||||
|
||||
|
||||
sub add_user($self, $username, $password, $fullname) {
|
||||
my $row = new_row('users', nickname => $username, password => $password, fullname => $fullname);
|
||||
return $row;
|
||||
my $rows = $self->{'dbh'}->do(q~insert into users (nickname, password, fullname) values (?, ?, ?)~, undef, ($username, $password, $fullname));
|
||||
if ($self->{'dbh'}->errstr) {
|
||||
die $self->{'dbh'}->errstr;
|
||||
}
|
||||
return $rows;
|
||||
}
|
||||
|
||||
|
||||
sub add_file($self, $user_id, $filename, $original_filename) {
|
||||
|
||||
my $row = new_row('images',
|
||||
owner_id => $user_id,
|
||||
file_name => $filename,
|
||||
original_filename => $original_filename
|
||||
);
|
||||
|
||||
return $row;
|
||||
my $rows = $self->{'dbh'}->do(q~insert into images (owner_id, file_name, original_filename) values (?, ?, ?)~, undef, ($user_id, $filename, $original_filename));
|
||||
if ($self->{'dbh'}->errstr) {
|
||||
die $self->{'dbh'}->errstr;
|
||||
}
|
||||
return $rows;
|
||||
}
|
||||
|
||||
sub get_files($self, $user_id, $items_count=20, $page=1) {
|
||||
|
@ -73,48 +67,10 @@ sub get_files($self, $user_id, $items_count=20, $page=1) {
|
|||
$page = 1 if ($page < 1);
|
||||
my $start_at = --$page * $items_count;
|
||||
|
||||
# my ($rows_count) = $self->{'dbh'}->selectrow_array(q~select count(*) from images where owner_id=? ~, undef , $user_id);
|
||||
my $rows_count = one_row(['images' => -count => 'file_id', -where => { 'owner_id' => $user_id}] );
|
||||
|
||||
# my $images_list = $self->{'dbh'}->selectall_arrayref(q~select * from images where owner_id=? order by created_time desc LIMIT ? OFFSET ? ~, { Slice => {} }, $user_id, $items_count, $start_at );
|
||||
my $images_list = all_rows([
|
||||
'images' =>
|
||||
-where => { 'owner_id' => $user_id },
|
||||
-limit => $items_count,
|
||||
-offset => $start_at
|
||||
]);
|
||||
my ($rows_count) = $self->{'dbh'}->selectrow_array(q~select count(*) from images where owner_id=? ~, undef , $user_id);
|
||||
my $images_list = $self->{'dbh'}->selectall_arrayref(q~select * from images where owner_id=? order by created_time desc LIMIT ? OFFSET ? ~, { Slice => {} }, $user_id, $items_count, $start_at );
|
||||
|
||||
return { total_rows => $rows_count, images_list => $images_list };
|
||||
}
|
||||
|
||||
sub add_album($self, $user_id, $album_name, $album_desc) {
|
||||
my $row = new_row('albums', name => $album_name, description => $album_desc, owner_id => $user_id);
|
||||
return $row;
|
||||
}
|
||||
|
||||
sub save_tag($self, $db_file_id, $tag_name, $tag_value) {
|
||||
eval {
|
||||
my $row = new_row('exif_data', 'exif_tag' => $tag_name, 'tag_data' => $tag_value,'image_id' => $db_file_id, deleted => 0) || die "error!";
|
||||
return $row;
|
||||
};
|
||||
if ($@) {
|
||||
say STDERR ("Error! $@");
|
||||
}
|
||||
}
|
||||
|
||||
sub save_tags($self, $db_file_id, $tag_data) {
|
||||
eval {
|
||||
connector->txn(sub {
|
||||
for my $key (keys %$tag_data) {
|
||||
$self->save_tag($db_file_id, $key, $tag_data->{$key});
|
||||
}
|
||||
});
|
||||
};
|
||||
if ($@) {
|
||||
say STDERR ("Error! $@");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
1;
|
6
public/vendor/js/vue.min.js
vendored
6
public/vendor/js/vue.min.js
vendored
File diff suppressed because one or more lines are too long
|
@ -2,14 +2,13 @@
|
|||
|
||||
BEGIN;
|
||||
|
||||
CREATE TABLE `albums` (
|
||||
`album_id` INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
`name` STRING NOT NULL,
|
||||
`description` TEXT,
|
||||
`created` DATETIME DEFAULT (CURRENT_TIMESTAMP),
|
||||
`modified` DATETIME DEFAULT (CURRENT_TIMESTAMP),
|
||||
`deleted` BOOLEAN,
|
||||
`owner_id` INTEGER NOT NULL
|
||||
);
|
||||
CREATE TABLE albums (
|
||||
album_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name STRING NOT NULL,
|
||||
description TEXT,
|
||||
created DATETIME DEFAULT (CURRENT_TIMESTAMP),
|
||||
modified DATETIME DEFAULT (CURRENT_TIMESTAMP),
|
||||
deleted BOOLEAN
|
||||
);
|
||||
|
||||
COMMIT;
|
||||
|
|
|
@ -1,13 +0,0 @@
|
|||
-- Deploy fotostore:exif_data to sqlite
|
||||
|
||||
BEGIN;
|
||||
|
||||
CREATE TABLE `exif_data` (
|
||||
`record_id` INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
`exif_tag` TEXT NOT NULL,
|
||||
`tag_data` TEXT NOT NULL,
|
||||
`image_id` INTEGER NOT NULL,
|
||||
`deleted` BOOLEAN DEFAULT 0
|
||||
);
|
||||
|
||||
COMMIT;
|
|
@ -14,4 +14,4 @@ CREATE TABLE images (
|
|||
REFERENCES users (user_id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
COMMIT;
|
||||
COMMIT;
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
-- Revert fotostore:exif_data from sqlite
|
||||
|
||||
BEGIN;
|
||||
|
||||
-- XXX Add DDLs here.
|
||||
|
||||
COMMIT;
|
|
@ -1,5 +1,7 @@
|
|||
-- Revert fotostore:images from sqlite
|
||||
|
||||
BEGIN;
|
||||
|
||||
DROP TABLE images;
|
||||
|
||||
COMMIT;
|
||||
COMMIT;
|
||||
|
|
|
@ -11,5 +11,3 @@ user_albums [users albums] 2017-07-22T09:47:48Z Denis Fedoseev <denis.fedoseev@g
|
|||
@v1.0.0 2017-08-02T03:46:51Z Denis Fedoseev <denis.fedoseev@gmail.com> # Tag v1.0.0.
|
||||
images [images@v1.0.0] 2017-08-02T03:55:08Z Denis Fedoseev <denis.fedoseev@gmail.com> # Adds images.original_filename.
|
||||
@v1.1.0 2017-08-02T04:46:42Z Denis Fedoseev <denis.fedoseev@gmail.com> # Tag v1.1.0.
|
||||
|
||||
exif_data 2018-08-19T08:04:49Z Denis Fedoseev <denis.fedoseev@gmail.com> # exif data table
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
-- Verify fotostore:exif_data on sqlite
|
||||
|
||||
BEGIN;
|
||||
|
||||
-- XXX Add verifications here.
|
||||
|
||||
ROLLBACK;
|
|
@ -4,4 +4,4 @@ BEGIN;
|
|||
|
||||
select file_id, owner_id, file_name, created_time from images where 0;
|
||||
|
||||
ROLLBACK;
|
||||
ROLLBACK;
|
||||
|
|
|
@ -5,7 +5,26 @@
|
|||
<script src="/file_uploader/js/vendor/jquery.ui.widget.js"></script>
|
||||
<script src="/file_uploader/js/jquery.iframe-transport.js"></script>
|
||||
<script src="/file_uploader/js/jquery.fileupload.js"></script>
|
||||
|
||||
<script>
|
||||
$(function () {
|
||||
$('#fileupload').fileupload({
|
||||
dataType: 'json',
|
||||
done: function (e, data) {
|
||||
$.each(data.result.files, function (index, file) {
|
||||
$('<p/>').text(file.name).appendTo('#lastUploadLog');
|
||||
});
|
||||
},
|
||||
sequentialUploads: true,
|
||||
progressall: function (e, data) {
|
||||
var progress = parseInt(data.loaded / data.total * 100, 10);
|
||||
$('#progress .progress-bar').css(
|
||||
'width',
|
||||
progress + '%'
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
<div id="progress" class="container">
|
||||
<div class="progress-bar" style="width: 0%;"></div>
|
||||
|
@ -90,6 +109,7 @@
|
|||
xhr.onload = function () {
|
||||
var result = JSON.parse(xhr.responseText);
|
||||
self.imagesList = result.images_list;
|
||||
console.dir(self.imagesList);
|
||||
self.pagesCount = result.pages_count;
|
||||
}
|
||||
xhr.send()
|
||||
|
@ -105,25 +125,4 @@
|
|||
}
|
||||
|
||||
});
|
||||
</script>
|
||||
<script>
|
||||
$(function () {
|
||||
$('#fileupload').fileupload({
|
||||
dataType: 'json',
|
||||
done: function (e, data) {
|
||||
$.each(data.result.files, function (index, file) {
|
||||
$('<p/>').text(file.name).appendTo('#lastUploadLog');
|
||||
$('#progress .progress-bar').css('width','0%');
|
||||
});
|
||||
},
|
||||
sequentialUploads: true,
|
||||
progressall: function (e, data) {
|
||||
var progress = parseInt(data.loaded / data.total * 100, 10);
|
||||
$('#progress .progress-bar').css(
|
||||
'width',
|
||||
progress + '%'
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</script>
|
|
@ -1,7 +1,7 @@
|
|||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" >
|
||||
<title>Simple image hosting</title>
|
||||
<title>Rough, Slow, Stupid, Contrary Photohosting</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<!-- Bootstrap styles -->
|
||||
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
|
||||
|
@ -14,7 +14,7 @@
|
|||
<link rel="stylesheet" href="/file_uploader/css/jquery.fileupload-ui.css">
|
||||
<link rel="stylesheet" href="/css/main.css">
|
||||
|
||||
<script src="/vendor/js/vue.min.js"></script>
|
||||
<script src="https://vuejs.org/js/vue.min.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
|
||||
|
@ -26,7 +26,7 @@
|
|||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="/">Tiny images hosting</a>
|
||||
<a class="navbar-brand" href="/">Rough, Slow, Stupid, Contrary Photohosting</a>
|
||||
</div>
|
||||
<div class="navbar-collapse collapse">
|
||||
<% if (!is_user_authenticated()) { %>
|
||||
|
|
Loading…
Reference in a new issue