Skip to content
Commits on Source (55)
zanata.xml merge=ours
......@@ -13,5 +13,5 @@ themes/*
themes/default/templates/data.html.ep
themes/default/templates/raw.html.ep
themes/default/templates/stats.json.ep
themes/default/public/packed/*
tmp/*
.zanata-cache/*
......@@ -29,6 +29,7 @@ sqlite2:
script:
- make podcheck
- MOJO_CONFIG=t/sqlite2.conf make minion &
- sleep 3
- MOJO_CONFIG=t/sqlite2.conf make test-sqlite
- MOJO_CONFIG=t/sqlite2.conf make watch
- MOJO_CONFIG=t/sqlite2.conf make cleanbdd
......
......@@ -2,7 +2,7 @@
## Main developers
* Luc Didry, aka Sky (<http://www.fiat-tux.fr>), core developer, @framasky on [Twitter](https://twitter.com/framasky) and on [Diaspora*](https://framasphere.org/public/framasky)
* Luc Didry, aka Sky (<http://www.fiat-tux.fr>), core developer, @framasky on [Mastodon](https://framapiaf.org/@framasky) and on [Diaspora*](https://framasphere.org/public/framasky)
* Dattaz (<http://dattaz.fr>), webapp developer, [@dat_taz](https://twitter.com/dat_taz)
## Contributors
......@@ -15,3 +15,4 @@
* Thor77 (<http://thor77.org>), german translation, among other things
* Quentin Pagès, occitan translation
* Alexis Clairet (<https://github.com/Turboconnard>), paste image to upload ability
* ButterflyOfFire (<https://mastodon.tetaneutral.net/@BoF>), arabic translation
Revision history for Lutim
0.9.2 2018-03-09
- Fix langage drop-down
- Allow to use HTML in broadcast message
- Remove old targets from Makefile
0.9.1 2018-03-09
- Fix default setting bug (db_path)
0.9.0 2018-03-09
- Added partial arabic translation (thx to ButterflyOfFire)
- Default theme is now non-fluid (ie don't take all the width of the screen)
- Use Photoswipe for the gallery instead of Unite gallery
- Use Zanata for translations (https://trad.framasoft.org)
- Add an option to personnalize proposed retention delays
- Use random Initialization Vector for encryption
- Use Mojo::SQLite instead of ORLite
- Fix various bugs
0.8.8 2018-02-07
- Fix security issues, thanks to SecuNinja
0.8.7 2017-12-22
- Fix bug if dbtype not configured in lutim.conf
0.8.6 2017-11-18
- Fix bug resulting in no EXIF tags deletion
0.8.5 2017-07-09
- Fix Henny Penny font path in css
0.8.4 2017-06-24
- Mitigate a bug using the same empty record twice
0.8.3 2017-06-15
- Fix the donuts charts in the /stats page.
0.8.2 2017-06-14
- Enforce Mojolicious::Plugin::AssetPack version
0.8.1 2017-06-13
- Fix #46
0.8 2017-06-13
- Improve statistics page
- Add database abstraction layer (#42)
......
EXTRACTDIR=-D lib -D themes/default/templates
EN=themes/default/lib/Lutim/I18N/en.po
FR=themes/default/lib/Lutim/I18N/fr.po
DE=themes/default/lib/Lutim/I18N/de.po
ES=themes/default/lib/Lutim/I18N/es.po
OC=themes/default/lib/Lutim/I18N/oc.po
XGETTEXT=carton exec local/bin/xgettext.pl
POT=themes/default/lib/Lutim/I18N/lutim.pot
XGETTEXT=carton exec local/bin/xgettext.pl -u
CARTON=carton exec
LUTIM=script/lutim
REAL_LUTIM=script/application
locales:
$(XGETTEXT) $(EXTRACTDIR) -o $(EN) 2>/dev/null
$(XGETTEXT) $(EXTRACTDIR) -o $(FR) 2>/dev/null
$(XGETTEXT) $(EXTRACTDIR) -o $(DE) 2>/dev/null
$(XGETTEXT) $(EXTRACTDIR) -o $(ES) 2>/dev/null
$(XGETTEXT) $(EXTRACTDIR) -o $(OC) 2>/dev/null
$(XGETTEXT) $(EXTRACTDIR) -o $(POT) 2>/dev/null
push-locales: locales
zanata-cli -q -B push
pull-locales:
zanata-cli -q -B pull
stats-locales:
zanata-cli -q stats
podcheck:
podchecker lib/Lutim/DB/Image.pm
......@@ -30,16 +31,13 @@ test: podcheck test-sqlite test-pg
clean:
rm -rf lutim.db files/
rmassets:
rm -rf themes/default/public/packed/*
dev: rmassets
dev:
$(CARTON) morbo $(LUTIM) --listen http://0.0.0.0:3000 --watch lib/ --watch script/ --watch themes/ --watch lutim.conf
devlog:
multitail log/development.log
prod: rmassets
prod:
$(CARTON) hypnotoad -f $(LUTIM)
prodlog:
......
......@@ -64,6 +64,6 @@ It uses:
* [Moment.js](http://momentjs.com/) for displaying real dates instead of unix timestamps.
* [Fontello](http://fontello.com/) and the [markdown font](https://github.com/dcurtis/markdown-mark/) for the icons, licenses for the fontello icons fonts are in `public/font/LICENSE.txt`
* [Henny Penny](https://www.google.com/fonts/specimen/Henny+Penny) font designed by Olga Umpeleva for [Brownfox](http://brownfox.org)
* [Unite gallery](http://unitegallery.net/) for the gallery
* [PhotoSwipe](http://photoswipe.com/) for the gallery
* [JSZip](https://stuk.github.io/jszip/) for generating a zip containing all the images in the gallery
* [FileSaver.js](https://github.com/eligrey/FileSaver.js/) for saving the zip
requires 'Mojolicious', '>= 7.31';
requires 'EV';
requires 'IO::Socket::SSL';
requires 'Net::SSLeay', '>= 1.81';
requires 'Data::Validate::URI';
requires 'Net::Domain::TLD', '>= 1.73'; # Must have the last version to handle (at least) .xyz and .link
requires 'Mojo::Pg';
requires 'Mojo::SQLite';
requires 'Mojolicious::Plugin::I18N';
requires 'Mojolicious::Plugin::AssetPack';
requires 'Mojolicious::Plugin::DebugDumperHelper';
requires 'Mojolicious::Plugin::PgURLHelper';
requires 'Mojolicious::Plugin::StaticCache';
requires "Minion", "== 4.06";
requires 'Minion::Backend::SQLite', "== 0.009";
requires 'CSS::Minifier::XS';
requires 'JavaScript::Minifier::XS';
requires 'ORLite';
requires 'Text::Unidecode';
requires 'DateTime';
requires 'Filesys::DiskUsage';
......
This diff is collapsed.
......@@ -35,6 +35,7 @@ sub startup {
tweet_card_via => '@framasky',
max_file_size => 10*1024*1024,
https => 0,
proposed_delays => '0,1,7,30,365',
default_delay => 0,
max_delay => 0,
token_length => 24,
......@@ -42,8 +43,10 @@ sub startup {
thumbnail_size => 100,
theme => 'default',
dbtype => 'sqlite',
db_path => 'lutim.db',
max_files_in_zip => 15,
minion => {
prefix => '/',
minion => {
enabled => 0,
dbtype => 'sqlite',
db_path => 'minion.db'
......@@ -67,16 +70,18 @@ sub startup {
}
push @{$self->renderer->paths}, $self->home->rel_file('themes/default/templates');
push @{$self->static->paths}, $self->home->rel_file('themes/default/public');
# Internationalization
my $lib = $self->home->rel_file('themes/'.$config->{theme}.'/lib');
eval qq(use lib "$lib");
$self->plugin('I18N');
# Compressed assets
$self->plugin('AssetPack' => { pipes => [qw(Combine)] });
# Cache static files
$self->plugin('StaticCache');
# Helpers
$self->plugin('Lutim::Plugin::Helpers');
$self->plugin('Lutim::Plugin::Lang');
# Minion
if ($config->{minion}->{enabled}) {
......@@ -136,20 +141,6 @@ sub startup {
delete @{$wait_for_it}{grep { time - $wait_for_it->{$_} > $c->config->{anti_flood_delay} } keys %{$wait_for_it}} if (defined($wait_for_it));
}
);
$self->hook(after_static => sub {
my $c = shift;
$c->res->headers->cache_control('max-age=2592000, must-revalidate');
});
$self->asset->store->paths($self->static->paths);
$self->asset->process('index.css' => ('css/bootstrap.min.css', 'css/fontello-embedded.css', 'css/animation.css', 'css/uploader.css', 'css/hennypenny.css', 'css/lutim.css', 'css/markdown.css'));
$self->asset->process('stats.css' => ('css/bootstrap.min.css', 'css/fontello-embedded.css', 'css/morris-0.4.3.min.css', 'css/hennypenny.css', 'css/lutim.css'));
$self->asset->process('about.css' => ('css/bootstrap.min.css', 'css/fontello-embedded.css', 'css/hennypenny.css', 'css/lutim.css'));
$self->asset->process('gallery.css' => ('/gallery/css/unite-gallery.css', '/gallery/themes/default/ug-theme-default.css'));
$self->asset->process('index.js' => ('js/bootstrap.min.js', 'js/lutim.js', 'js/dmuploader.min.js'));
$self->asset->process('stats.js' => ('js/bootstrap.min.js', 'js/lutim.js', 'js/raphael-min.js', 'js/morris-0.4.3.min.js', 'js/stats.js'));
$self->asset->process('freeze.js' => ('js/jquery-2.1.0.min.js', 'js/freezeframe.min.js'));
$self->defaults(layout => 'default');
......@@ -180,6 +171,10 @@ sub startup {
to('Controller#stats')->
name('stats');
$r->get('/lang/:l')->
to('Controller#change_lang')->
name('lang');
$r->get('/partial/:file' => sub {
my $c = shift;
$c->render(
......@@ -230,14 +225,24 @@ sub startup {
to('Controller#get_counter')->
name('counter');
$r->get('/about/(:short).(:f)')->
to('Controller#about_img')->
name('about_img');
$r->get('/about/:short/(:key).(:f)')->
to('Controller#about_img')->
name('about_img');
$r->get('/(:short).(:f)')->
to('Controller#short')->
name('short');
$r->get('/:short')->
to('Controller#short');
$r->get('/:short/(:key).(:f)')->
to('Controller#short');
$r->get('/:short/:key')->
to('Controller#short');
}
......
......@@ -152,7 +152,7 @@ sub run {
% l('Total')
% );
<script>
Morris.Donut({
var enabled_donut = {
element: 'raw-enabled-holder',
data: [
{label: "<%= l('no time limit') %>", value: $unlimited_enabled},
......@@ -168,8 +168,8 @@ Morris.Donut({
'#427dc1',
'#455ac3',
]
});
Morris.Donut({
};
var disabled_donut = {
element: 'raw-disabled-holder',
data: [
{label: "<%= l('no time limit') %>", value: $unlimited_disabled},
......@@ -185,7 +185,7 @@ Morris.Donut({
'#427dc1',
'#455ac3',
]
});
};
</script>
$dom2
EOF
......
......@@ -41,10 +41,16 @@ use Locale::Maketext::Lexicon {
_style => 'gettext',
'*' => [
Gettext => dirname(__FILE__) . '/I18N/*.po',
Gettext => dirname(__FILE__) . '/../../../default/lib/Lutim/I18N/*.po',
Gettext => $app_dir . 'themes/default/lib/Lutim/I18N/*.po',
]
};
use vars qw($app_dir);
BEGIN {
use Cwd;
my $app_dir = getcwd;
}
1;
EOF
......@@ -58,6 +64,7 @@ FR=lib/Lutim/I18N/fr.po
DE=lib/Lutim/I18N/de.po
ES=lib/Lutim/I18N/es.po
OC=lib/Lutim/I18N/oc.po
AR=lib/Lutim/I18N/ar.po
SEDOPTS=-e "s\@SOME DESCRIPTIVE TITLE\@Lutim language file\@" \\
-e "s\@YEAR THE PACKAGE'S COPYRIGHT HOLDER\@2015 Luc Didry\@" \\
-e "s\@CHARSET\@utf8\@" \\
......@@ -74,6 +81,7 @@ locales:
\$(XGETTEXT) -D templates -D ../default/templates -o \$(DE) 2>/dev/null
\$(XGETTEXT) -D templates -D ../default/templates -o \$(ES) 2>/dev/null
\$(XGETTEXT) -D templates -D ../default/templates -o \$(OC) 2>/dev/null
\$(XGETTEXT) -D templates -D ../default/templates -o \$(AR) 2>/dev/null
sed \$(SEDOPTS) -i \$(EN)
sed \$(SEDOPTS2) -i \$(EN)
sed \$(SEDOPTS) -i \$(FR)
......@@ -83,7 +91,9 @@ locales:
sed \$(SEDOPTS) -i \$(ES)
sed \$(SEDOPTS2) -i \$(ES)
sed \$(SEDOPTS) -i \$(OC)
sed \$(SEDOPTS2) -i \$(OC)
sed \$(SEDOPTS) -i \$(OC)
sed \$(SEDOPTS2) -i \$(AR)
sed \$(SEDOPTS2) -i \$(AR)
EOF
open $f, '>', File::Spec->catfile($home, 'Makefile') or die "Unable to open $home/Makefile: $!";
......
# vim:set sw=4 ts=4 sts=4 expandtab:
package Lutim::Controller;
use Mojo::Base 'Mojolicious::Controller';
use Mojo::Util qw(url_escape url_unescape b64_encode);
use Mojo::Util qw(url_escape url_unescape b64_encode encode);
use Mojo::Asset::Memory;
use Mojo::JSON qw(true false);
use Lutim::DB::Image;
......@@ -45,6 +45,19 @@ sub about {
shift->render(template => 'about');
}
sub change_lang {
my $c = shift;
my $l = $c->param('l');
$c->cookie(lutim_lang => $l, { path => $c->config('prefix') });
if ($c->req->headers->referrer) {
return $c->redirect_to($c->req->headers->referrer);
} else {
return $c->redirect_to('/');
}
}
sub stats {
my $c = shift;
......@@ -71,6 +84,31 @@ sub infos {
);
}
sub about_img {
my $c = shift;
my $short = $c->param('short');
my $image = Lutim::DB::Image->new(app => $c->app, short => $short);
if ($image->enabled && $image->path) {
return $c->render(
json => {
success => true,
data => {
width => $image->width,
height => $image->height,
}
}
);
} else {
return $c->render(
json => {
success => false,
msg => $c->l('Unable to find the image %1.', $short)
}
);
}
}
sub webapp {
my $c = shift;
......@@ -234,7 +272,7 @@ sub delete {
}
},
any => sub {
shift->render_not_found;
$c->helpers->reply->not_found;
}
);
}
......@@ -384,29 +422,31 @@ sub add {
}
unless ((defined($keep_exif) && $keep_exif) || $mediatype eq 'image/svg+xml' || $mediatype !~ m#image/(x-)?xcf# || $mediatype ne 'image/webp') {
# Remove the EXIF tags
my $data = new IO::Scalar \$upload->slurp();
my $et = new Image::ExifTool;
# Use $data in Image::ExifTool object
$et->ExtractInfo($data);
# Remove all metadata
$et->SetNewValue('*', undef);
# Create a temporary IO::Scalar to write into
my $temp;
my $a = new IO::Scalar \$temp;
$et->WriteInfo($data, $a);
# Update the uploaded file with it's no-tags clone
$data = Mojo::Asset::Memory->new->add_chunk($temp);
$upload->asset($data);
unless (defined($keep_exif) && $keep_exif) {
if ($mediatype ne 'image/svg+xml' && $mediatype !~ m#image/(x-)?xcf# && $mediatype ne 'image/webp') {
# Remove the EXIF tags
my $data = new IO::Scalar \$upload->slurp();
my $et = new Image::ExifTool;
# Use $data in Image::ExifTool object
$et->ExtractInfo($data);
# Remove all metadata
$et->SetNewValue('*', undef);
# Create a temporary IO::Scalar to write into
my $temp;
my $a = new IO::Scalar \$temp;
$et->WriteInfo($data, $a);
# Update the uploaded file with it's no-tags clone
$data = Mojo::Asset::Memory->new->add_chunk($temp);
$upload->asset($data);
}
}
my $key;
my ($key, $iv);
if ($c->param('crypt') || $c->config('always_encrypt')) {
($upload, $key) = $c->crypt($upload, $filename);
($upload, $key, $iv) = $c->crypt($upload, $filename);
}
$upload->move_to($path);
......@@ -421,6 +461,7 @@ sub add {
->created_by($ip)
->width($width)
->height($height)
->iv($iv)
->write;
# Log image creation
......@@ -512,7 +553,9 @@ sub short {
my $short = $c->param('short');
my $touit = $c->param('t');
my $key = $c->param('key');
my $thumb = $c->param('thumb');
my $thumb;
$thumb = '' if defined $c->param('thumb');
$thumb = $c->param('width') if defined $c->param('width');
my $dl = (defined($c->param('dl'))) ? 'attachment' : 'inline';
my $image = Lutim::DB::Image->new(app => $c->app, short => $short);
......@@ -532,7 +575,7 @@ sub short {
}
my $test;
if (defined($touit)) {
if (defined($touit) && $image->mediatype !~ m/svg/) {
$test = 1;
my $short = $image->short;
$short .= '/'.$key if (defined($key));
......@@ -575,12 +618,7 @@ sub short {
);
return $c->redirect_to('/');
} else {
my $expires = ($image->delete_at_day) ? $image->delete_at_day : 360;
my $dt = DateTime->from_epoch( epoch => $expires * 86400 + $image->created_at);
$dt->set_time_zone('GMT');
$expires = $dt->strftime("%a, %d %b %Y %H:%M:%S GMT");
$test = $c->render_file($im_loaded, $image->filename, $image->path, $image->mediatype, $dl, $expires, $image->delete_at_first_view, $key, $thumb);
$test = $c->render_file($im_loaded, $image, $dl, $key, $thumb);
}
}
......@@ -618,7 +656,7 @@ sub short {
return $c->redirect_to('/');
} else {
# Image never existed
$c->render_not_found;
$c->helpers->reply->not_found;
}
}
......@@ -656,7 +694,7 @@ sub zip {
$c->delete_image($image);
# Warn user
$zip->addString($c->l('Unable to find the image: it has been deleted.'), 'images/'.$filename.'.txt');
$zip->addString(encode('UTF-8', $c->l('Unable to find the image: it has been deleted.')), 'images/'.$filename.'.txt');
next;
}
......@@ -668,7 +706,7 @@ sub zip {
# Delete image
$c->delete_image($image);
$zip->addString($c->l('Unable to find the image: it has been deleted.'), 'images/'.$filename.'.txt');
$zip->addString(encode('UTF-8', $c->l('Unable to find the image: it has been deleted.')), 'images/'.$filename.'.txt');
next;
} else {
my $expires = ($image->delete_at_day) ? $image->delete_at_day : 360;
......@@ -679,7 +717,7 @@ sub zip {
my $path = $image->path;
unless ( -f $path && -r $path ) {
$c->app->log->error("Cannot read file [$path]. error [$!]");
$zip->addString($c->l('Unable to find the image: it has been deleted.'), 'images/'.$filename.'.txt');
$zip->addString(encode('UTF-8', $c->l('Unable to find the image: it has been deleted.')), 'images/'.$filename.'.txt');
next;
}
......@@ -704,10 +742,10 @@ sub zip {
$c->app->log->info('[NOT FOUND] someone tried to view '.$short.' but it does\'nt exist anymore.');
# Warn user
$zip->addString($c->l('Unable to find the image: it has been deleted.'), 'images/'.$image->filename.'.txt');
$zip->addString(encode('UTF-8', $c->l('Unable to find the image: it has been deleted.')), 'images/'.$image->filename.'.txt');
next;
} else {
$zip->addString($c->l('Image not found.'), 'images/'.$short.'.txt');
$zip->addString(encode('UTF-8', $c->l('Image not found.')), 'images/'.$short.'.txt');
next;
}
}
......
......@@ -17,6 +17,7 @@ has 'last_access_at';
has 'mod_token';
has 'width';
has 'height';
has 'iv';
has 'app';
=head1 NAME
......@@ -64,6 +65,8 @@ Have a look at Lutim::DB::Image::SQLite's code: it's simple and may be more unde
=item B<height> : integer
=item B<iv> : initialization vector for the file encryption
=item B<app> : a mojolicious object
=back
......@@ -125,7 +128,8 @@ sub to_hash {
last_access_at => $c->last_access_at,
mod_token => $c->mod_token,
width => $c->width,
height => $c->height
height => $c->height,
height => $c->iv
};
}
......
......@@ -69,7 +69,8 @@ sub select_created_after {
sub select_empty {
my $c = shift;
my $record = $c->app->pg->db->query('SELECT * FROM lutim WHERE path IS NULL LIMIT 1')->hashes->first;
my $record = $c->app->pg->db->query('SELECT * FROM lutim WHERE path IS NULL')->hashes->shuffle->first;
$c->app->pg->db->query('UPDATE lutim SET path = ? WHERE short = ?', 'used', $record->{short});
$c = $c->_slurp($record);
......@@ -80,9 +81,9 @@ sub write {
my $c = shift;
if ($c->record) {
$c->app->pg->db->query('UPDATE lutim SET counter = ?, created_at = ?, created_by = ?, delete_at_day = ?, delete_at_first_view = ?, enabled = ?, filename = ?, footprint = ?, height = ?, last_access_at = ?, mediatype = ?, mod_token = ?, path = ?, short = ?, width = ? WHERE short = ?', $c->counter, $c->created_at, $c->created_by, $c->delete_at_day, $c->delete_at_first_view, $c->enabled, $c->filename, $c->footprint, $c->height, $c->last_access_at, $c->mediatype, $c->mod_token, $c->path, $c->short, $c->width, $c->short);
$c->app->pg->db->query('UPDATE lutim SET counter = ?, created_at = ?, created_by = ?, delete_at_day = ?, delete_at_first_view = ?, enabled = ?, filename = ?, footprint = ?, height = ?, last_access_at = ?, mediatype = ?, mod_token = ?, path = ?, short = ?, width = ?, iv = ? WHERE short = ?', $c->counter, $c->created_at, $c->created_by, $c->delete_at_day, $c->delete_at_first_view, $c->enabled, $c->filename, $c->footprint, $c->height, $c->last_access_at, $c->mediatype, $c->mod_token, $c->path, $c->short, $c->width, $c->iv, $c->short);
} else {
$c->app->pg->db->query('INSERT INTO lutim (counter, created_at, created_by, delete_at_day, delete_at_first_view, enabled, filename, footprint, height, last_access_at, mediatype, mod_token, path, short, width) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', $c->counter, $c->created_at, $c->created_by, $c->delete_at_day, $c->delete_at_first_view, $c->enabled, $c->filename, $c->footprint, $c->height, $c->last_access_at, $c->mediatype, $c->mod_token, $c->path, $c->short, $c->width);
$c->app->pg->db->query('INSERT INTO lutim (counter, created_at, created_by, delete_at_day, delete_at_first_view, enabled, filename, footprint, height, last_access_at, mediatype, mod_token, path, short, width, iv) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', $c->counter, $c->created_at, $c->created_by, $c->delete_at_day, $c->delete_at_first_view, $c->enabled, $c->filename, $c->footprint, $c->height, $c->last_access_at, $c->mediatype, $c->mod_token, $c->path, $c->short, $c->width, $c->iv);
$c->record(1);
}
......@@ -219,6 +220,7 @@ sub _slurp {
$c->mod_token($image->{mod_token});
$c->width($image->{width});
$c->height($image->{height});
$c->iv($image->{iv});
$c->record(1);
}
......
# vim:set sw=4 ts=4 sts=4 ft=perl expandtab:
package Lutim::DB::Image::SQLite;
use Mojo::Base 'Lutim::DB::Image';
use Lutim::DB::SQLite;
use Mojo::Collection 'c';
has 'record';
has 'record' => 0;
sub new {
my $c = shift;
......@@ -19,13 +18,10 @@ sub accessed {
my $c = shift;
my $time = shift;
$c->record->update(
counter => $c->counter + 1,
last_access_at => $time
);
$c->counter($c->record->counter);
$c->last_access_at($c->record->last_access_at);
$c->app->sqlite->db->query('UPDATE lutim SET counter = counter + 1, last_access_at = ? WHERE short = ?', $time, $c->short);
my $h = $c->app->sqlite->db->query('SELECT counter, last_access_at FROM lutim WHERE short = ?', $c->short)->hashes->first;
$c->counter($h->{counter});
$c->last_access_at($h->{last_access_at});
return $c;
}
......@@ -37,9 +33,9 @@ sub count_delete_at_day_endis {
my $created = shift;
if (defined $created) {
return Lutim::DB::SQLite::Lutim->count('WHERE path IS NOT NULL AND delete_at_day = ? AND enabled = ? AND created_at < ?', $day, $enabled, $created);
return $c->app->sqlite->db->query('SELECT count(short) AS count FROM lutim WHERE path IS NOT NULL AND delete_at_day = ? AND enabled = ? AND created_at < ?', $day, $enabled, $created)->hashes->first->{count};
} else {
return Lutim::DB::SQLite::Lutim->count('WHERE path IS NOT NULL AND delete_at_day = ? AND enabled = ?', $day, $enabled);
return $c->app->sqlite->db->query('SELECT count(short) AS count FROM lutim WHERE path IS NOT NULL AND delete_at_day = ? AND enabled = ?', $day, $enabled)->hashes->first->{count};
}
}
......@@ -47,7 +43,7 @@ sub count_created_before {
my $c = shift;
my $time = shift;
return Lutim::DB::SQLite::Lutim->count('WHERE path IS NOT NULL AND created_at < ?', $time);
return $c->app->sqlite->db->query('SELECT count(short) AS count FROM lutim WHERE path IS NOT NULL AND created_at < ?', $time)->hashes->first->{count};
}
sub select_created_after {
......@@ -56,15 +52,17 @@ sub select_created_after {
my @images;
my @records = Lutim::DB::SQLite::Lutim->select('WHERE path IS NOT NULL AND created_at >= ?', $time);
my $records = $c->app->sqlite->db->query('SELECT * FROM lutim WHERE path IS NOT NULL AND created_at >= ?', $time)->hashes;
for my $e (@records) {
my $i = Lutim::DB::Image->new(app => $c->app);
$i->record($e);
$i->_slurp;
$records->each(
sub {
my ($e, $num) = @_;
my $i = Lutim::DB::Image->new(app => $c->app);
$i->_slurp($e);
push @images, $i;
}
push @images, $i;
}
);
return c(@images);
}
......@@ -72,10 +70,10 @@ sub select_created_after {
sub select_empty {
my $c = shift;
my @records = Lutim::DB::SQLite::Lutim->select('WHERE path IS NULL LIMIT 1');
my $record = $c->app->sqlite->db->query('SELECT * FROM lutim WHERE path IS NULL')->hashes->shuffle->first;
$c->app->sqlite->db->query('UPDATE lutim SET path = ? WHERE short = ?', 'used', $record->{short});
$c->record($records[0]);
$c = $c->_slurp;
$c = $c->_slurp($record);
return $c;
}
......@@ -84,42 +82,10 @@ sub write {
my $c = shift;
if ($c->record) {
$c->record->update(
counter => $c->counter,
created_at => $c->created_at,
created_by => $c->created_by,
delete_at_day => $c->delete_at_day,
delete_at_first_view => $c->delete_at_first_view,
enabled => $c->enabled,
filename => $c->filename,
footprint => $c->footprint,
height => $c->height,
last_access_at => $c->last_access_at,
mediatype => $c->mediatype,
mod_token => $c->mod_token,
path => $c->path,
short => $c->short,
width => $c->width
);
$c->app->sqlite->db->query('UPDATE lutim SET counter = ?, created_at = ?, created_by = ?, delete_at_day = ?, delete_at_first_view = ?, enabled = ?, filename = ?, footprint = ?, height = ?, last_access_at = ?, mediatype = ?, mod_token = ?, path = ?, short = ?, width = ?, iv = ? WHERE short = ?', $c->counter, $c->created_at, $c->created_by, $c->delete_at_day, $c->delete_at_first_view, $c->enabled, $c->filename, $c->footprint, $c->height, $c->last_access_at, $c->mediatype, $c->mod_token, $c->path, $c->short, $c->width, $c->iv, $c->short);
} else {
my $record = Lutim::DB::SQLite::Lutim->create(
counter => $c->counter,
created_at => $c->created_at,
created_by => $c->created_by,
delete_at_day => $c->delete_at_day,
delete_at_first_view => $c->delete_at_first_view,
enabled => $c->enabled,
filename => $c->filename,
footprint => $c->footprint,
height => $c->height,
last_access_at => $c->last_access_at,
mediatype => $c->mediatype,
mod_token => $c->mod_token,
path => $c->path,
short => $c->short,
width => $c->width
);
$c->record($record);
$c->app->sqlite->db->query('INSERT INTO lutim (counter, created_at, created_by, delete_at_day, delete_at_first_view, enabled, filename, footprint, height, last_access_at, mediatype, mod_token, path, short, width, iv) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', $c->counter, $c->created_at, $c->created_by, $c->delete_at_day, $c->delete_at_first_view, $c->enabled, $c->filename, $c->footprint, $c->height, $c->last_access_at, $c->mediatype, $c->mod_token, $c->path, $c->short, $c->width, $c->iv);
$c->record(1);
}
return $c;
......@@ -129,30 +95,26 @@ sub count_short {
my $c = shift;
my $short = shift;
return Lutim::DB::SQLite::Lutim->count('WHERE short IS ?', $short);
return $c->app->sqlite->db->query('SELECT count(short) AS count FROM lutim WHERE short = ?', $short)->hashes->first->{count};
}
sub count_empty {
my $c = shift;
return Lutim::DB::SQLite::Lutim->count('WHERE path IS NULL');
return $c->app->sqlite->db->query('SELECT count(short) AS count FROM lutim WHERE path IS NULL')->hashes->first->{count};
}
sub count_not_empty {
my $c = shift;
return Lutim::DB::SQLite::Lutim->count('WHERE path IS NOT NULL');
return $c->app->sqlite->db->query('SELECT count(short) AS count FROM lutim WHERE path IS NOT NULL')->hashes->first->{count};
}
sub clean_ips_until {
my $c = shift;
my $time = shift;
Lutim::DB::SQLite->do(
'UPDATE lutim SET created_by = "" WHERE path IS NOT NULL AND created_at < ?',
{},
$time
);
$c->app->sqlite->db->query('UPDATE lutim SET created_by = NULL WHERE path IS NOT NULL AND created_at < ?', $time);
return $c;
}
......@@ -163,15 +125,18 @@ sub get_no_longer_viewed_files {
my @images;
my @records = Lutim::DB::SQLite::Lutim->select('WHERE enabled = 1 AND last_access_at < ?', $time);
my $records = $c->app->sqlite->db->query('SELECT * FROM lutim WHERE enabled = 1 AND last_access_at < ?', $time)->{hashes};
for my $e (@records) {
my $i = Lutim::DB::Image->new(app => $c->app);
$i->record($e);
$i->_slurp;
$records->each(
sub {
my ($e, $num) = @_;
my $i = Lutim::DB::Image->new(app => $c->app);
$i->record(1);
$i->_slurp($e);
push @images, $i;
}
push @images, $i;
}
);
return c(@images);
}
......@@ -181,15 +146,17 @@ sub get_images_to_clean {
my @images;
my @records = Lutim::DB::SQLite::Lutim->select('WHERE enabled = 1 AND (delete_at_day * 86400) < (? - created_at) AND delete_at_day != 0', time());
my $records = $c->app->sqlite->db->query('SELECT * FROM lutim WHERE enabled = 1 AND (delete_at_day * 86400) < (? - created_at) AND delete_at_day != 0', time())->hashes;
for my $e (@records) {
my $i = Lutim::DB::Image->new(app => $c->app);
$i->record($e);
$i->_slurp;
$records->each(
sub {
my ($e, $num) = @_;
my $i = Lutim::DB::Image->new(app => $c->app);
$i->_slurp($e);
push @images, $i;
}
push @images, $i;
}
);
return c(@images);
}
......@@ -199,15 +166,17 @@ sub get_50_oldest {
my @images;
my @records = Lutim::DB::SQLite::Lutim->select('WHERE path IS NOT NULL AND enabled = 1 ORDER BY created_at ASC LIMIT 50');
my $records = $c->app->sqlite->db->query('SELECT * FROM lutim WHERE path IS NOT NULL AND enabled = 1 ORDER BY created_at ASC LIMIT 50')->hashes;
for my $e (@records) {
my $i = Lutim::DB::Image->new(app => $c->app);
$i->record($e);
$i->_slurp;
$records->each(
sub {
my ($e, $num) = @_;
my $i = Lutim::DB::Image->new(app => $c->app);
$i->_slurp($e);
push @images, $i;
}
push @images, $i;
}
);
return c(@images);
}
......@@ -215,7 +184,7 @@ sub get_50_oldest {
sub disable {
my $c = shift;
$c->record->update(enabled => 0);
$c->app->sqlite->db->query('UPDATE lutim SET enabled = 0 WHERE short = ?', $c->short);
$c->enabled(0);
return $c;
......@@ -223,34 +192,38 @@ sub disable {
sub _slurp {
my $c = shift;
my $r = shift;
my @images;
if ($c->record) {
@images = ($c->record);
} elsif ($c->short) {
@images = Lutim::DB::SQLite::Lutim->select('WHERE short = ?', $c->short);
my $image;
if (defined $r) {
$image = $r;
} else {
my $images = $c->app->sqlite->db->query('SELECT * FROM lutim WHERE short = ?', $c->short)->hashes;
if ($images->size) {
$image = $images->first;
}
}
if (scalar @images) {
my $image = $images[0];
$c->short($image->short);
$c->path($image->path);
$c->footprint($image->footprint);
$c->enabled($image->enabled);
$c->mediatype($image->mediatype);
$c->filename($image->filename);
$c->counter($image->counter);
$c->delete_at_first_view($image->delete_at_first_view);
$c->delete_at_day($image->delete_at_day);
$c->created_at($image->created_at);
$c->created_by($image->created_by);
$c->last_access_at($image->last_access_at);
$c->mod_token($image->mod_token);
$c->width($image->width);
$c->height($image->height);
$c->record($image) unless $c->record;
if ($image) {
$c->short($image->{short});
$c->path($image->{path});
$c->footprint($image->{footprint});
$c->enabled($image->{enabled});
$c->mediatype($image->{mediatype});
$c->filename($image->{filename});
$c->counter($image->{counter});
$c->delete_at_first_view($image->{delete_at_first_view});
$c->delete_at_day($image->{delete_at_day});
$c->created_at($image->{created_at});
$c->created_by($image->{created_by});
$c->last_access_at($image->{last_access_at});
$c->mod_token($image->{mod_token});
$c->width($image->{width});
$c->height($image->{height});
$c->iv($image->{iv});
$c->record(1);
}
return $c;
......
# vim:set sw=4 ts=4 sts=4 ft=perl expandtab:
package Lutim::DB::SQLite;
use Mojolicious;
use Mojo::File;
use FindBin qw($Bin);
BEGIN {
my $m = Mojolicious->new;
my $cfile = Mojo::File->new($Bin, '..' , 'lutim.conf');
if (defined $ENV{MOJO_CONFIG}) {
$cfile = Mojo::File->new($ENV{MOJO_CONFIG});
unless (-e $cfile->to_abs) {
$cfile = Mojo::File->new($Bin, '..', $ENV{MOJO_CONFIG});
}
}
our $config = $m->plugin('Config' =>
{
file => $cfile->to_abs->to_string,
default => {
db_path => 'lutim.db'
}
}
);
}
# Create database
use ORLite {
file => $config->{db_path},
unicode => 1,
create => sub {
my $dbh = shift;
$dbh->do(
'CREATE TABLE lutim (
short TEXT PRIMARY KEY,
path TEXT,
footprint TEXT,
enabled INTEGER,
mediatype TEXT,
filename TEXT,
counter INTEGER,
delete_at_first_view INTEGER,
delete_at_day INTEGER,
created_at INTEGER,
created_by TEXT,
last_access_at INTEGER,
mod_token TEXT,
width INTEGER,
height INTEGER)'
);
return 1;
}
};
1;
......@@ -4,6 +4,7 @@ use Mojo::Base 'Mojolicious::Plugin';
use Mojo::Util qw(quote);
use Crypt::CBC;
use Data::Entropy qw(entropy_source);
use DateTime;
sub register {
my ($self, $app) = @_;
......@@ -16,24 +17,36 @@ sub register {
# Database migration
my $migrations = Mojo::Pg::Migrations->new(pg => $app->pg);
if ($app->mode eq 'development') {
$migrations->from_file('utilities/migrations.sql')->migrate(0)->migrate(1);
if ($app->mode eq 'development' && $ENV{LUTIM_DEBUG}) {
$migrations->from_file('utilities/migrations/postgresql.sql')->migrate(0)->migrate(2);
} else {
$migrations->from_file('utilities/migrations.sql')->migrate(1);
$migrations->from_file('utilities/migrations/postgresql.sql')->migrate(2);
}
} elsif ($app->config('dbtype') eq 'sqlite') {
# SQLite database migration if needed
use Mojo::SQLite;
$app->helper(sqlite => \&_sqlite);
my $sql = Mojo::SQLite->new('sqlite:'.$app->config('db_path'));
my $migrations = $sql->migrations;
if ($app->mode eq 'development' && $ENV{LUTIM_DEBUG}) {
$migrations->from_file('utilities/migrations/sqlite.sql')->migrate(0)->migrate(2);
} else {
$migrations->from_file('utilities/migrations/sqlite.sql')->migrate(2);
}
}
$app->helper(render_file => \&_render_file);
$app->helper(ip => \&_ip);
$app->helper(provisioning => \&_provisioning);
$app->helper(shortener => \&_shortener);
$app->helper(stop_upload => \&_stop_upload);
$app->helper(max_delay => \&_max_delay);
$app->helper(default_delay => \&_default_delay);
$app->helper(is_selected => \&_is_selected);
$app->helper(crypt => \&_crypt);
$app->helper(decrypt => \&_decrypt);
$app->helper(delete_image => \&_delete_image);
$app->helper(render_file => \&_render_file);
$app->helper(ip => \&_ip);
$app->helper(provisioning => \&_provisioning);
$app->helper(shortener => \&_shortener);
$app->helper(stop_upload => \&_stop_upload);
$app->helper(max_delay => \&_max_delay);
$app->helper(default_delay => \&_default_delay);
$app->helper(is_selected => \&_is_selected);
$app->helper(crypt => \&_crypt);
$app->helper(decrypt => \&_decrypt);
$app->helper(delete_image => \&_delete_image);
}
sub _pg {
......@@ -43,9 +56,23 @@ sub _pg {
return $pg;
}
sub _sqlite {
my $c = shift;
state $sqlite = Mojo::SQLite->new('sqlite:'.$c->app->config('db_path'));
return $sqlite;
}
sub _render_file {
my $c = shift;
my ($im_loaded, $filename, $path, $mediatype, $dl, $expires, $nocache, $key, $thumb) = @_;
my ($im_loaded, $img, $dl, $key, $thumb) = @_;
my ($filename, $path, $iv, $mediatype, $no_cache) = ($img->filename, $img->path, $img->iv, $img->mediatype, $img->delete_at_first_view);
my $expires = ($img->delete_at_day) ? $img->delete_at_day : 360;
my $dt = DateTime->from_epoch( epoch => $expires * 86400 + $img->created_at);
$dt->set_time_zone('GMT');
$expires = $dt->strftime("%a, %d %b %Y %H:%M:%S GMT");
$dl = 'attachment' if ($mediatype =~ m/svg/);
$filename = quote($filename);
......@@ -62,7 +89,7 @@ sub _render_file {
$mediatype =~ s/x-//;
my $headers = Mojo::Headers->new();
if ($nocache) {
if ($no_cache || defined($thumb)) {
$headers->add('Cache-Control' => 'no-cache, no-store, max-age=0, must-revalidate');
} else {
$headers->add('Expires' => $expires);
......@@ -72,7 +99,7 @@ sub _render_file {
$c->res->content->headers($headers);
if ($key) {
$asset = $c->decrypt($key, $path);
$asset = $c->decrypt($key, $path, $iv);
} else {
$asset = Mojo::Asset::File->new(path => $path);
}
......@@ -82,7 +109,11 @@ sub _render_file {
$im->BlobToImage($asset->slurp);
# Create the thumbnail
$im->Resize(geometry=>'x'.$c->config('thumbnail_size'));
if ($thumb eq '') {
$im->Resize(geometry => 'x'.$c->config('thumbnail_size'));
} else {
$im->Resize(geometry => $thumb);
}
# Replace the asset with the thumbnail
$asset = Mojo::Asset::Memory->new->add_chunk($im->ImageToBlob());
......@@ -187,12 +218,13 @@ sub _crypt {
my $filename = shift;
my $key = $c->shortener($c->config('crypto_key_length'));
my $iv = $c->shortener(8);
my $cipher = Crypt::CBC->new(
-key => $key,
-cipher => 'Blowfish',
-header => 'none',
-iv => 'dupajasi'
-iv => $iv
);
$cipher->start('encrypting');
......@@ -206,19 +238,21 @@ sub _crypt {
$crypt_upload->filename($filename);
$crypt_upload->asset($crypt_asset);
return ($crypt_upload, $key);
return ($crypt_upload, $key, $iv);
}
sub _decrypt {
my $c = shift;
my $key = shift;
my $file = shift;
my $iv = shift;
$iv = 'dupajasi' unless $iv;
my $cipher = Crypt::CBC->new(
-key => $key,
-cipher => 'Blowfish',
-header => 'none',
-iv => 'dupajasi'
-iv => $iv
);
$cipher->start('decrypting');
......
# vim:set sw=4 ts=4 sts=4 ft=perl expandtab:
package Lutim::Plugin::Lang;
use Mojo::Base 'Mojolicious::Plugin';
use Mojo::Collection;
use Mojo::File;
sub register {
my ($self, $app) = @_;
$app->helper(available_langs => \&_available_langs);
$app->hook(
before_dispatch => sub {
my $c = shift;
$c->languages($c->cookie('lutim_lang')) if $c->cookie('lutim_lang');
}
);
}
sub _available_langs {
my $c = shift;
state $langs = Mojo::Collection->new(
glob($c->app->home->rel_file('themes/'.$c->config('theme').'/lib/Lutim/I18N/*po')),
glob($c->app->home->rel_file('themes/default/lib/Lutim/I18N/*po'))
)->map(
sub {
Mojo::File->new($_)->basename('.po');
}
)->uniq->sort(
sub {
$c->l($a) cmp $c->l($b)
}
)->to_array;
}
1;
......@@ -22,14 +22,34 @@ sub startup {
{
file => $cfile,
default => {
prefix => '/',
theme => 'default',
provisioning => 100,
provis_step => 5,
length => 8,
always_encrypt => 0,
anti_flood_delay => 5,
tweet_card_via => '@framasky',
max_file_size => 10*1024*1024,
https => 0,
proposed_delays => '0,1,7,30,365',
default_delay => 0,
max_delay => 0,
token_length => 24,
crypto_key_length => 8,
thumbnail_size => 100,
theme => 'default',
dbtype => 'sqlite',
db_path => 'lutim.db',
max_files_in_zip => 15,
prefix => '/',
minion => {
enabled => 0,
dbtype => 'sqlite',
db_path => 'minion.db'
},
}
}
);
$self->plugin('Lutim::Plugin::Helpers');
$config->{prefix} = $config->{url_sub_dir} if (defined($config->{url_sub_dir}) && $config->{prefix} eq '/');
$self->app->log->warn('"url_sub_dir" configuration option is deprecated. Use "prefix" instead. "url_sub_dir" will be removed in the future') if (defined($config->{url_sub_dir}));
......@@ -42,10 +62,12 @@ sub startup {
}
push @{$self->static->paths}, $self->home->rel_file('themes/default/public');
$self->hook(after_static => sub {
my $c = shift;
$c->res->headers->cache_control('max-age=2592000, must-revalidate');
});
# Cache static files
$self->plugin('StaticCache');
# Helpers
$self->plugin('Lutim::Plugin::Helpers');
$self->plugin('Mount' => {$config->{prefix} => File::Spec->catfile($Bin, '..', 'script', 'application')});
}
......