Commit c5dac2d5 authored by Luc Didry's avatar Luc Didry
Browse files

Merge branch 'development' into 'master'

Merge development

Closes #40, #14, #27, #33, #39, and #9

See merge request !19
parents 5dffc402 88b77f91
Pipeline #16930 passed with stages
in 7 minutes and 42 seconds
*.swp
lutim.conf
lutim.db
*.db
*.db-shm
*.db-wal
script/hypnotoad.pid
local/*
files/*
......@@ -10,5 +12,6 @@ themes/*
!themes/default/*
themes/default/templates/data.html.ep
themes/default/templates/raw.html.ep
themes/default/templates/stats.json.ep
themes/default/public/packed/*
tmp/
tmp/*
image: hatsoftwares/test-ci:latest
stages:
- sqlite
- postgresql
before_script:
- carton install
- rm -f *db
sqlite1:
stage: sqlite
cache:
key: "$CI_BUILD_REF_NAME"
untracked: true
paths:
- local
script:
- make podcheck
- MOJO_CONFIG=t/sqlite1.conf make test-sqlite
- MOJO_CONFIG=t/sqlite1.conf make watch
- MOJO_CONFIG=t/sqlite1.conf make cleanbdd
- MOJO_CONFIG=t/sqlite1.conf make cleanfiles
- MOJO_CONFIG=t/sqlite1.conf make stats
sqlite2:
stage: sqlite
cache:
key: "$CI_BUILD_REF_NAME"
untracked: true
paths:
- local
script:
- make podcheck
- MOJO_CONFIG=t/sqlite2.conf make minion &
- MOJO_CONFIG=t/sqlite2.conf make test-sqlite
- MOJO_CONFIG=t/sqlite2.conf make watch
- MOJO_CONFIG=t/sqlite2.conf make cleanbdd
- MOJO_CONFIG=t/sqlite2.conf make cleanfiles
- MOJO_CONFIG=t/sqlite2.conf make stats
sqlite3:
stage: sqlite
cache:
key: "$CI_BUILD_REF_NAME"
untracked: true
paths:
- local
script:
- make podcheck
- service postgresql restart
- sleep 10
- service postgresql status
- make create-pg-test-db
- MOJO_CONFIG=t/sqlite3.conf make minion &
- sleep 3
- MOJO_CONFIG=t/sqlite3.conf make test-sqlite
- MOJO_CONFIG=t/sqlite3.conf make watch
- MOJO_CONFIG=t/sqlite3.conf make cleanbdd
- MOJO_CONFIG=t/sqlite3.conf make cleanfiles
- MOJO_CONFIG=t/sqlite3.conf make stats
tags:
- Debian
- Jessie
postgresql1:
stage: postgresql
cache:
key: "$CI_BUILD_REF_NAME"
untracked: true
paths:
- local
script:
- make podcheck
- service postgresql restart
- sleep 10
- service postgresql status
- make create-pg-test-db
- MOJO_CONFIG=t/postgresql1.conf make test-pg
- MOJO_CONFIG=t/postgresql1.conf make watch
- MOJO_CONFIG=t/postgresql1.conf make cleanbdd
- MOJO_CONFIG=t/postgresql1.conf make cleanfiles
- MOJO_CONFIG=t/postgresql1.conf make stats
postgresql2:
stage: postgresql
cache:
key: "$CI_BUILD_REF_NAME"
untracked: true
paths:
- local
script:
- make podcheck
- service postgresql restart
- sleep 10
- service postgresql status
- make create-pg-test-db
- MOJO_CONFIG=t/postgresql2.conf make minion &
- sleep 3
- MOJO_CONFIG=t/postgresql2.conf make test-pg
- MOJO_CONFIG=t/postgresql2.conf make watch
- MOJO_CONFIG=t/postgresql2.conf make cleanbdd
- MOJO_CONFIG=t/postgresql2.conf make cleanfiles
- MOJO_CONFIG=t/postgresql2.conf make stats
postgresql3:
stage: postgresql
cache:
key: "$CI_BUILD_REF_NAME"
untracked: true
paths:
- local
script:
- make podcheck
- service postgresql restart
- sleep 10
- service postgresql status
- make create-pg-test-db
- MOJO_CONFIG=t/postgresql3.conf make minion &
- sleep 3
- MOJO_CONFIG=t/postgresql3.conf make test-pg
- MOJO_CONFIG=t/postgresql3.conf make watch
- MOJO_CONFIG=t/postgresql3.conf make cleanbdd
- MOJO_CONFIG=t/postgresql3.conf make cleanfiles
- MOJO_CONFIG=t/postgresql3.conf make stats
tags:
- Debian
- Jessie
......@@ -14,3 +14,4 @@
* Laura Arjona Reina (<https://wiki.debian.org/LauraArjona>), spanish translation
* 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
......@@ -2,6 +2,22 @@ Revision history for Lutim
0.8 2017-?
- Improve statistics page
- Add database abstraction layer (#42)
- Add PostgreSQL support (#42)
- Asks for Mojolicious 7.31 minimum (to install it: `carton update`)
- Add Liberapay and Tipeee buttons
- Remove Flattr button
- Handle MOJO_CONFIG env variable (#44)
- Fix bug #39
- Add gallery constructor to "my files" list (#33)
- Handle too much images in zip download URL (#27)
- LocalStorage is now updated if an image's delay is modified
- Allow user to paste image from clipboard to upload images (#14 and !5)
- Fix #40
- Add stats in JSON format (GET /stats.json)
- Add Cache-control headers for static files
- Put almost all js/css stuff outside templates
- Allow to use Minion to increment counter (#43)
0.7.1 2016-06-21
- Fix dependency bug
......
EXTRACTFILES=utilities/locales_files.txt
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
......@@ -7,19 +7,58 @@ OC=themes/default/lib/Lutim/I18N/oc.po
XGETTEXT=carton exec local/bin/xgettext.pl
CARTON=carton exec
LUTIM=script/lutim
REAL_LUTIM=script/application
locales:
$(XGETTEXT) -W -f $(EXTRACTFILES) -o $(EN) 2>/dev/null
$(XGETTEXT) -W -f $(EXTRACTFILES) -o $(FR) 2>/dev/null
$(XGETTEXT) -W -f $(EXTRACTFILES) -o $(DE) 2>/dev/null
$(XGETTEXT) -W -f $(EXTRACTFILES) -o $(ES) 2>/dev/null
$(XGETTEXT) -W -f $(EXTRACTFILES) -o $(OC) 2>/dev/null
$(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
podcheck:
podchecker lib/Lutim/DB/Image.pm
test-sqlite:
$(CARTON) $(REAL_LUTIM) test
test-pg:
$(CARTON) $(REAL_LUTIM) test
test: podcheck test-sqlite test-pg
clean:
rm -rf lutim.db files/
dev:
rmassets:
rm -rf themes/default/public/packed/*
dev: rmassets
$(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
$(CARTON) hypnotoad -f $(LUTIM)
prodlog:
multitail log/production.log
minion:
$(CARTON) $(REAL_LUTIM) minion worker
create-pg-test-db:
sudo -u postgres psql -f t/create-pg-testdb.sql
stats:
$(CARTON) $(LUTIM) cron stats -m production
watch:
$(CARTON) $(LUTIM) cron watch -m production
cleanfiles:
$(CARTON) $(LUTIM) cron cleanfiles -m production
cleanbdd:
$(CARTON) $(LUTIM) cron cleanbdd -m production
requires 'Mojolicious';
requires 'Mojolicious', '>= 7.31';
requires 'EV';
requires 'IO::Socket::SSL';
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 'Mojolicious::Plugin::I18N';
requires 'Mojolicious::Plugin::AssetPack';
requires 'Mojolicious::Plugin::DebugDumperHelper';
requires 'Mojolicious::Plugin::PgURLHelper';
requires "Minion", "== 4.06";
requires 'Minion::Backend::SQLite', "== 0.009";
requires 'CSS::Minifier::XS';
requires 'JavaScript::Minifier::XS';
requires 'Mojolicious::Plugin::DebugDumperHelper';
requires 'ORLite';
requires 'Text::Unidecode';
requires 'DateTime';
......
This diff is collapsed.
# vim:set sw=4 ts=4 sts=4 ft=perl expandtab:
package Lutim;
use Mojo::Base 'Mojolicious';
use Mojo::Util qw(quote);
use LutimModel;
use Crypt::CBC;
use Data::Entropy qw(entropy_source);
use Lutim::DB::Image;
use vars qw($im_loaded);
BEGIN {
......@@ -25,6 +23,7 @@ sub startup {
$self->{wait_for_it} = {};
$self->plugin('DebugDumperHelper');
$self->plugin('PgURLHelper');
my $config = $self->plugin('Config', {
default => {
......@@ -42,6 +41,13 @@ sub startup {
crypto_key_length => 8,
thumbnail_size => 100,
theme => 'default',
dbtype => 'sqlite',
max_files_in_zip => 15,
minion => {
enabled => 0,
dbtype => 'sqlite',
db_path => 'minion.db'
},
}
});
......@@ -70,228 +76,30 @@ sub startup {
$self->plugin('AssetPack' => { pipes => [qw(Combine)] });
# Helpers
$self->helper(
render_file => sub {
my $c = shift;
my ($filename, $path, $mediatype, $dl, $expires, $nocache, $key, $thumb) = @_;
$dl = 'attachment' if ($mediatype =~ m/svg/);
$filename = quote($filename);
my $asset;
unless (-f $path && -r $path) {
$c->app->log->error("Cannot read file [$path]. error [$!]");
$c->flash(
msg => $c->l('Unable to find the image: it has been deleted.')
);
return 500;
}
$mediatype =~ s/x-//;
my $headers = Mojo::Headers->new();
if ($nocache) {
$headers->add('Cache-Control' => 'no-cache, no-store, max-age=0, must-revalidate');
} else {
$headers->add('Expires' => $expires);
}
$headers->add('Content-Type' => $mediatype.';name='.$filename);
$headers->add('Content-Disposition' => $dl.';filename='.$filename);
$c->res->content->headers($headers);
if ($key) {
$asset = $c->decrypt($key, $path);
} else {
$asset = Mojo::Asset::File->new(path => $path);
}
if (defined $thumb && $im_loaded && $mediatype ne 'image/svg+xml' && $mediatype !~ m#image/(x-)?xcf# && $mediatype ne 'image/webp') { # ImageMagick don't work in Debian with svg (for now?)
my $im = Image::Magick->new;
$im->BlobToImage($asset->slurp);
# Create the thumbnail
$im->Resize(geometry=>'x'.$c->config('thumbnail_size'));
# Replace the asset with the thumbnail
$asset = Mojo::Asset::Memory->new->add_chunk($im->ImageToBlob());
}
$c->res->content->asset($asset);
$headers->add('Content-Length' => $asset->size);
return $c->rendered(200);
$self->plugin('Lutim::Plugin::Helpers');
# Minion
if ($config->{minion}->{enabled}) {
$self->config->{minion}->{dbtype} = 'sqlite' unless defined $config->{minion}->{dbtype};
if ($config->{minion}->{dbtype} eq 'sqlite') {
$self->config('minion')->{db_path} = 'minion.db' unless defined $config->{minion}->{db_path};
$self->plugin('Minion' => { SQLite => 'sqlite:'.$config->{minion}->{db_path} });
} elsif ($config->{minion}->{dbtype} eq 'postgresql') {
$self->plugin('Minion' => { Pg => $self->pg_url($config->{minion}->{'pgdb'}) });
}
);
$self->helper(
ip => sub {
my $c = shift;
my $ip_only = shift || 0;
my $proxy = $c->req->headers->header('X-Forwarded-For');
my $ip = ($proxy) ? $proxy : $c->tx->remote_address;
my $remote_port = (defined($c->req->headers->header('X-Remote-Port'))) ? $c->req->headers->header('X-Remote-Port') : $c->tx->remote_port;
return ($ip_only) ? $ip : "$ip remote port:$remote_port";
}
);
$self->helper(
provisioning => sub {
my $c = shift;
# Create some short patterns for provisioning
if (LutimModel::Lutim->count('WHERE path IS NULL') < $c->config->{provisioning}) {
for (my $i = 0; $i < $c->config->{provis_step}; $i++) {
if (LutimModel->begin) {
my $short;
do {
$short= $c->shortener($c->config->{length});
} while (LutimModel::Lutim->count('WHERE short = ?', $short) || $short eq 'about' || $short eq 'stats' || $short eq 'd' || $short eq 'm' || $short eq 'gallery' || $short eq 'zip' || $short eq 'infos');
LutimModel::Lutim->create(
short => $short,
counter => 0,
enabled => 1,
delete_at_first_view => 0,
delete_at_day => 0,
mod_token => $c->shortener($c->config->{token_length})
);
LutimModel->commit;
}
}
$self->app->minion->add_task(
accessed => sub {
my $job = shift;
my $short = $job->args->[0];
my $time = $job->args->[1];
my $img = Lutim::DB::Image->new(app => $job->app, short => $short);
$img->accessed($time) if $img->path;
}
}
);
$self->helper(
shortener => sub {
my $c = shift;
my $length = shift;
my @chars = ('a'..'z','A'..'Z','0'..'9');
my $result = '';
foreach (1..$length) {
$result .= $chars[entropy_source->get_int(scalar(@chars))];
}
return $result;
}
);
$self->helper(
stop_upload => sub {
my $c = shift;
if (-f 'stop-upload' || -f 'stop-upload.manual') {
$c->stash(
stop_upload => $c->l('Uploading is currently disabled, please try later or contact the administrator (%1).', $config->{contact})
);
return 1;
}
return 0;
}
);
$self->helper(
max_delay => sub {
my $c = shift;
return $c->config->{max_delay} if ($c->config->{max_delay} >= 0);
warn "max_delay set to a negative value. Default to 0.";
return 0;
}
);
$self->helper(
default_delay => sub {
my $c = shift;
return $c->config->{default_delay} if ($c->config->{default_delay} >= 0);
warn "default_delay set to a negative value. Default to 0.";
return 0;
}
);
$self->helper(
is_selected => sub {
my $c = shift;
my $num = shift;
return ($num == $c->default_delay) ? 'selected="selected"' : '';
}
);
$self->helper(
crypt => sub {
my $c = shift;
my $upload = shift;
my $filename = shift;
my $key = $c->shortener($c->config('crypto_key_length'));
my $cipher = Crypt::CBC->new(
-key => $key,
-cipher => 'Blowfish',
-header => 'none',
-iv => 'dupajasi'
);
$cipher->start('encrypting');
my $crypt_asset = Mojo::Asset::File->new;
$crypt_asset->add_chunk($cipher->crypt($upload->slurp));
$crypt_asset->add_chunk($cipher->finish);
my $crypt_upload = Mojo::Upload->new;
$crypt_upload->filename($filename);
$crypt_upload->asset($crypt_asset);
return ($crypt_upload, $key);
}
);
$self->helper(
decrypt => sub {
my $c = shift;
my $key = shift;
my $file = shift;
my $cipher = Crypt::CBC->new(
-key => $key,
-cipher => 'Blowfish',
-header => 'none',
-iv => 'dupajasi'
);
$cipher->start('decrypting');
my $decrypt_asset = Mojo::Asset::File->new;
open(my $f, "<",$file) or die "Unable to read encrypted file: $!";
binmode $f;
while (read($f, my $buffer,1024)) {
$decrypt_asset->add_chunk($cipher->crypt($buffer));
}
$decrypt_asset->add_chunk($cipher->finish) ;
return $decrypt_asset;
}
);
$self->helper(
delete_image => sub {
my $c = shift;
my $image = shift;
unlink $image->path();
$image->update(enabled => 0);
}
);
);
}
# Hooks
$self->hook(
before_dispatch => sub {
my $c = shift;
......@@ -328,15 +136,20 @@ 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('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/jquery-2.1.0.min.js', 'js/bootstrap.min.js', 'js/lutim.js', 'js/dmuploader.min.js'));
$self->asset->process('stats.js' => ('js/jquery-2.1.0.min.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->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');
......@@ -367,6 +180,20 @@ sub startup {
to('Controller#stats')->
name('stats');
$r->get('/partial/:file' => sub {
my $c = shift;
$c->render(
template => 'partial/'.$c->param('file'),
format => 'js',
layout => undef,
d => {
delay_0 => $c->l('no time limit'),
delay_1 => $c->l('24 hours'),
delay_365 => $c->l('1 year')
}
);
})->name('partial');
$r->get('/gallery' => sub {
shift->render(
template => 'gallery',
......
# vim:set sw=4 ts=4 sts=4 ft=perl expandtab:
package Lutim::Command::cron;
use Mojo::Base 'Mojolicious::Commands';
......
# vim:set sw=4 ts=4 sts=4 ft=perl expandtab:
package Lutim::Command::cron::cleanbdd;
use Mojo::Base 'Mojolicious::Command';
use LutimModel;
use Mojo::Util qw(slurp decode);
use Mojo::File;
use Lutim::DB::Image;
use FindBin qw($Bin);
use File::Spec qw(catfile);
......@@ -11,20 +12,26 @@ has usage => sub { shift->extract_usage };
sub run {
my $c = shift;
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});
}
}
my $config = $c->app->plugin('Config', {
file => File::Spec->catfile($Bin,