Compare commits

...

6 commits

Author SHA1 Message Date
Denis Fedoseev
d08a19b7cb forget some files 2018-09-10 22:19:52 +03:00
Denis Fedoseev
664b26ddac title 2018-09-10 21:29:35 +03:00
Denis Fedoseev
455484f2b2 -debug 2018-09-10 21:27:35 +03:00
Denis Fedoseev
c3d690f98a Save exif_tags in one batch 2018-09-10 11:49:15 +03:00
Denis Fedoseev
8ff6d351c4 saving exif metadata 2018-09-09 16:56:33 +03:00
Denis Fedoseev
3d3e83d1d8 scheme update 2018-08-19 11:07:30 +03:00
13 changed files with 231 additions and 95 deletions

View file

@ -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,14 +196,13 @@ 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 };
@ -252,32 +251,52 @@ 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 $promise = store_image($image_file, $image->filename, $user_id);
my $imager = Imager->new();
$imager->read( file => $image_file ) or die $imager->errstr;
my $promise = store_image($imager , $filename, $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;
@ -295,7 +314,8 @@ sub get_path {
}
sub store_image {
my $image_file = shift;
my $imager = shift;
my $filename = shift;
my $original_filename = shift;
my $user_id = shift;
@ -303,12 +323,6 @@ 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;
@ -335,16 +349,17 @@ sub store_image {
$scaled->write( file =>
File::Spec->catfile( get_path( $user_id, $scale ), $filename ) )
or die $scaled->errstr;
or die $scaled->errstr;
}
if ( !$db->add_file( $user_id, $filename, $original_filename ) ) {
my $file_info = $db->add_file( $user_id, $filename, $original_filename );
if ( !defined $file_info ) {
$log->error(sprintf('Can\'t save file %s', $filename));
die sprintf('Can\'t save file %s', $filename);
}
return $filename;
return $file_info->file_id;
},
sub {
my ($subprocess, $err, @results) = @_;
@ -357,5 +372,16 @@ 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;

View file

@ -4,24 +4,30 @@ use v5.20;
use strict;
use warnings;
use feature qw(signatures);
use feature qw(signatures say);
no warnings qw(experimental::signatures);
use Data::Dumper;
use DBIx::Struct qw(connector);
sub new {
my $class = shift;
my $db_file = shift;
my $dbh = DBI->connect(sprintf('dbi:SQLite:dbname=%s', $db_file),"","");
my $dbix = DBIx::Struct::connect(sprintf('dbi:SQLite:dbname=%s', $db_file),"","");
my $self = {
dbh => $dbh,
dbix_connector => $dbix,
};
bless $self, $class;
return $self;
}
sub check_user ($self, $nickname, $password) {
my ($user_id) = $self->{'dbh'}->selectrow_array(q~select user_id from users where nickname=? and password=?~, undef, ($nickname, $password));
return $user_id;
my $row = one_row('users', { nickname => $nickname, password => $password });
return $row->user_id;
}
sub get_user ($self, $user_id) {
@ -33,31 +39,31 @@ sub get_user ($self, $user_id) {
}
sub _get_user_by_user_id ($self, $user_id) {
my $user_data = $self->{'dbh'}->selectrow_hashref(q~select user_id, nickname, fullname, timestamp from users where user_id=?~, {}, ($user_id));
return $user_data;
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};
}
sub _get_user_by_username($self, $username) {
my $user_data = $self->{'dbh'}->selectrow_hashref(q~select user_id, nickname, fullname, timestamp from users where nickname=?~, {}, ($username));
return $user_data;
my $row = one_row('users', {nickname => $username}) || return {};
return {user_id => $row->user_id, nickname => $row->nickname, fullname => $row->fullname, timestamp => $row->timestamp};
}
sub add_user($self, $username, $password, $fullname) {
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;
my $row = new_row('users', nickname => $username, password => $password, fullname => $fullname);
return $row;
}
sub add_file($self, $user_id, $filename, $original_filename) {
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;
my $row = new_row('images',
owner_id => $user_id,
file_name => $filename,
original_filename => $original_filename
);
return $row;
}
sub get_files($self, $user_id, $items_count=20, $page=1) {
@ -67,10 +73,48 @@ 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 $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 ($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
]);
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 Normal file

File diff suppressed because one or more lines are too long

View file

@ -2,13 +2,14 @@
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
);
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
);
COMMIT;

13
sql/deploy/exif_data.sql Normal file
View file

@ -0,0 +1,13 @@
-- 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;

View file

@ -0,0 +1,17 @@
-- Deploy fotostore:images to sqlite
BEGIN;
CREATE TABLE images (
file_id INTEGER PRIMARY KEY AUTOINCREMENT,
owner_id INTEGER NOT NULL,
file_name TEXT NOT NULL,
created_time DATETIME NOT NULL
DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (
owner_id
)
REFERENCES users (user_id) ON DELETE CASCADE
);
COMMIT;

7
sql/revert/exif_data.sql Normal file
View file

@ -0,0 +1,7 @@
-- Revert fotostore:exif_data from sqlite
BEGIN;
-- XXX Add DDLs here.
COMMIT;

View file

@ -0,0 +1,5 @@
BEGIN;
DROP TABLE images;
COMMIT;

View file

@ -11,3 +11,5 @@ 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

7
sql/verify/exif_data.sql Normal file
View file

@ -0,0 +1,7 @@
-- Verify fotostore:exif_data on sqlite
BEGIN;
-- XXX Add verifications here.
ROLLBACK;

View file

@ -0,0 +1,7 @@
-- Verify fotostore:images on sqlite
BEGIN;
select file_id, owner_id, file_name, created_time from images where 0;
ROLLBACK;

View file

@ -5,26 +5,7 @@
<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>
@ -109,7 +90,6 @@
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()
@ -125,4 +105,25 @@
}
});
</script>
</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>

View file

@ -1,7 +1,7 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" >
<title>Rough, Slow, Stupid, Contrary Photohosting</title>
<title>Simple image hosting</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="https://vuejs.org/js/vue.min.js"></script>
<script src="/vendor/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="/">Rough, Slow, Stupid, Contrary Photohosting</a>
<a class="navbar-brand" href="/">Tiny images hosting</a>
</div>
<div class="navbar-collapse collapse">
<% if (!is_user_authenticated()) { %>