Verified Commit 1646980c authored by Luc Didry's avatar Luc Didry

Use Mojo::SQLite instead of ORLite + update deps

parent 413e13a8
local/*
files/*
lufi.conf
lufi.db
*.db
*.db-shm
*.db-wal
*.bak
*.swp
script/hypnotoad.pid
stop-upload
......
Revision history for Lufi
0.03 2018-??-??
- Use Mojo::SQLite instead of ORLite
0.02.2 2017-09-18
- Fix cron tasks bug
......
......@@ -24,7 +24,7 @@ podcheck:
podchecker lib/Lufi/DB/File.pm lib/Lufi/DB/Slice.pm
test:
$(CARTON) $(REAL_LUFI) test
$(CARTON) prove -l -f -o t/basic.t
clean:
rm -rf lufi.db files/
......
......@@ -28,5 +28,5 @@ feature 'postgresql', 'PostgreSQL support' => sub {
requires 'Mojolicious::Plugin::PgURLHelper';
};
feature 'sqlite', 'SQLite support' => sub {
requires 'ORLite';
requires 'Mojo::SQLite', '>= 3.000';
}
This diff is collapsed.
......@@ -31,6 +31,7 @@ sub startup {
session_duration => 3600,
allow_pwd_on_files => 0,
dbtype => 'sqlite',
db_path => 'lufi.db',
}
});
......
# vim:set sw=4 ts=4 sts=4 ft=perl expandtab:
package Lufi::DB::File;
use Mojo::Base -base;
use Mojo::Collection;
use Mojo::File;
use Mojo::Collection 'c';
use Lufi::DB::Slice;
has 'short';
has 'deleted' => 0;
......@@ -23,6 +25,7 @@ has 'slices' => sub {
return Mojo::Collection->new();
};
has 'passwd';
has 'record' => 0;
has 'app';
=head1 NAME
......@@ -160,6 +163,21 @@ sub delete {
=back
=cut
sub write {
my $c = shift;
if ($c->record) {
$c->app->dbi->db->query('UPDATE files SET short = ?, deleted = ?, mediatype = ?, filename = ?, filesize = ?, counter = ?, delete_at_first_view = ?, delete_at_day = ?, created_at = ?, created_by = ?, last_access_at = ?, mod_token = ?, nbslices = ?, complete = ?, passwd = ? WHERE short = ?', $c->short, $c->deleted, $c->mediatype, $c->filename, $c->filesize, $c->counter, $c->delete_at_first_view, $c->delete_at_day, $c->created_at, $c->created_by, $c->last_access_at, $c->mod_token, $c->nbslices, $c->complete, $c->passwd, $c->short);
} else {
$c->app->dbi->db->query('INSERT INTO files (short, deleted, mediatype, filename, filesize, counter, delete_at_first_view, delete_at_day, created_at, created_by, last_access_at, mod_token, nbslices, complete, passwd) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', $c->short, $c->deleted, $c->mediatype, $c->filename, $c->filesize, $c->counter, $c->delete_at_first_view, $c->delete_at_day, $c->created_at, $c->created_by, $c->last_access_at, $c->mod_token, $c->nbslices, $c->complete, $c->passwd);
$c->record(1);
}
return $c;
}
=head2 count_empty
=over 1
......@@ -174,6 +192,14 @@ sub delete {
=back
=cut
sub count_empty {
my $c = shift;
return $c->app->dbi->db->query('SELECT count(short) AS count FROM files WHERE created_at IS NULL')->hashes->first->{count};
}
=head2 already_exists
=over 1
......@@ -188,6 +214,15 @@ sub delete {
=back
=cut
sub already_exists {
my $c = shift;
my $short = shift;
return $c->app->dbi->db->query('SELECT count(short) AS count FROM files WHERE short = ?', $short)->hashes->first->{count};
}
=head2 get_empty
=over 1
......@@ -202,6 +237,16 @@ sub delete {
=back
=cut
sub get_empty {
my $c = shift;
my $r = $c->app->dbi->db->query('SELECT * FROM files WHERE created_at IS NULL')->hashes->shuffle->first;
return $c->_slurp($r)->created_at(time)->write;
}
=head2 get_stats
=over 1
......@@ -216,6 +261,18 @@ sub delete {
=back
=cut
sub get_stats {
my $c = shift;
my $files = $c->app->dbi->db->query('SELECT count(short) AS count FROM files WHERE created_at IS NOT null AND deleted = ?', 0)->hashes->first->{count};
my $deleted = $c->app->dbi->db->query('SELECT count(short) AS count FROM files WHERE created_at IS NOT null AND deleted = ?', 1)->hashes->first->{count};
my $empty = $c->app->dbi->db->query('SELECT count(short) AS count FROM files WHERE created_at IS null')->hashes->first->{count};
return { files => $files, deleted => $deleted, empty => $empty };
}
=head2 from_short
=over 1
......@@ -230,6 +287,21 @@ sub delete {
=back
=cut
sub from_short {
my $c = shift;
my $short = shift;
my $r = $c->app->dbi->db->query('SELECT * FROM files WHERE short = ?', $short)->hashes;
if ($r->size) {
return $c->_slurp($r->first)->record(1);
} else {
return undef;
}
}
=head2 get_oldest_undeleted_files
=over 1
......@@ -244,6 +316,26 @@ sub delete {
=back
=cut
sub get_oldest_undeleted_files {
my $c = shift;
my $num = shift;
my @files;
my $records = $c->app->dbi->db->query('SELECT * FROM files WHERE deleted = ? ORDER BY created_at ASC LIMIT ?', 0, $num)->hashes;
$records->each(
sub {
my ($e, $num) = @_;
my $i = Lufi::DB::File->new(app => $c->app);
push @files, $i->_slurp($e);
}
);
return c(@files);
}
=head2 get_expired
=over 1
......@@ -258,6 +350,27 @@ sub delete {
=back
=cut
sub get_expired {
my $c = shift;
my $time = shift;
my @files;
## Select only files expired since two days, to be sure that nobody is still downloading it
my $records = $c->app->dbi->db->query('SELECT * FROM files WHERE deleted = ? AND ((delete_at_day + 2) * 86400) < (? - created_at) AND delete_at_day != 0', 0, $time)->hashes;
$records->each(
sub {
my ($e, $num) = @_;
my $i = Lufi::DB::File->new(app => $c->app);
push @files, $i->_slurp($e);
}
);
return c(@files);
}
=head2 get_no_longer_viewed
=over 1
......@@ -272,6 +385,26 @@ sub delete {
=back
=cut
sub get_no_longer_viewed {
my $c = shift;
my $time = shift;
my @files;
my $records = $c->app->dbi->db->query('SELECT * FROM files WHERE deleted = ? AND last_access_at < ?', 0, $time)->hashes;
$records->each(
sub {
my ($e, $num) = @_;
my $i = Lufi::DB::File->new(app => $c->app);
push @files, $i->_slurp($e);
}
);
return c(@files);
}
=head2 delete_creator_before
=over 1
......@@ -288,4 +421,67 @@ sub delete {
=cut
sub delete_creator_before {
my $c = shift;
my $separation = shift;
$c->app->dbi->db->query('UPDATE files SET created_by = NULL WHERE created_by IS NOT NULL AND created_at < ?', $separation);
}
=head2 _slurp
=over 1
=item B<Usage> : C<$c-E<gt>_slurp>
=item B<Arguments> : none
=item B<Purpose> : put a database record's columns into the Lufi::DB::File object's attributes
=item B<Returns> : the Lufi::DB::File object
=back
=cut
sub _slurp {
my $c = shift;
my $r = shift;
my $file;
if (defined $r) {
$file = $r;
} else {
my $files = $c->app->dbi->db->query('SELECT * FROM files WHERE short = ?', $c->short)->hashes;
if ($files->size) {
$file = $files->first;
}
}
if ($file) {
$c->short($file->{short});
$c->deleted($file->{deleted});
$c->mediatype($file->{mediatype});
$c->filename($file->{filename});
$c->filesize($file->{filesize});
$c->counter($file->{counter});
$c->delete_at_first_view($file->{delete_at_first_view});
$c->delete_at_day($file->{delete_at_day});
$c->created_at($file->{created_at});
$c->created_by($file->{created_by});
$c->last_access_at($file->{last_access_at});
$c->mod_token($file->{mod_token});
$c->nbslices($file->{nbslices});
$c->complete($file->{complete});
$c->passwd($file->{passwd});
$c->record(1) unless $c->record;
}
$c->slices(Lufi::DB::Slice->new(app => $c->app)->get_slices_of_file($c->short));
return $c;
}
1;
# vim:set sw=4 ts=4 sts=4 ft=perl expandtab:
package Lufi::DB::File::Pg;
use Mojo::Base 'Lufi::DB::File';
use Mojo::File;
use Mojo::Collection 'c';
use Lufi::DB::Slice;
has 'record' => 0;
sub new {
my $c = shift;
......@@ -15,163 +10,4 @@ sub new {
return $c;
}
sub write {
my $c = shift;
if ($c->record) {
$c->app->pg->db->query('UPDATE files SET short = ?, deleted = ?, mediatype = ?, filename = ?, filesize = ?, counter = ?, delete_at_first_view = ?, delete_at_day = ?, created_at = ?, created_by = ?, last_access_at = ?, mod_token = ?, nbslices = ?, complete = ?, passwd = ? WHERE short = ?', $c->short, $c->deleted, $c->mediatype, $c->filename, $c->filesize, $c->counter, $c->delete_at_first_view, $c->delete_at_day, $c->created_at, $c->created_by, $c->last_access_at, $c->mod_token, $c->nbslices, $c->complete, $c->passwd, $c->short);
} else {
$c->app->pg->db->query('INSERT INTO files (short, deleted, mediatype, filename, filesize, counter, delete_at_first_view, delete_at_day, created_at, created_by, last_access_at, mod_token, nbslices, complete, passwd) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', $c->short, $c->deleted, $c->mediatype, $c->filename, $c->filesize, $c->counter, $c->delete_at_first_view, $c->delete_at_day, $c->created_at, $c->created_by, $c->last_access_at, $c->mod_token, $c->nbslices, $c->complete, $c->passwd);
$c->record(1);
}
return $c;
}
sub count_empty {
my $c = shift;
return $c->app->pg->db->query('SELECT count(short) FROM files WHERE created_at IS NULL')->hashes->first->{count};
}
sub already_exists {
my $c = shift;
my $short = shift;
return $c->app->pg->db->query('SELECT count(short) FROM files WHERE short = ?', $short)->hashes->first->{count};
}
sub get_empty {
my $c = shift;
my $r = $c->app->pg->db->query('SELECT * FROM files WHERE created_at IS NULL')->hashes->shuffle->first;
return $c->_slurp($r)->created_at(time)->write;
}
sub get_stats {
my $c = shift;
my $files = $c->app->pg->db->query('SELECT count(short) FROM files WHERE created_at IS NOT null AND deleted = false')->hashes->first->{count};
my $deleted = $c->app->pg->db->query('SELECT count(short) FROM files WHERE created_at IS NOT null AND deleted = true')->hashes->first->{count};
my $empty = $c->app->pg->db->query('SELECT count(short) FROM files WHERE created_at IS null')->hashes->first->{count};
return { files => $files, deleted => $deleted, empty => $empty };
}
sub from_short {
my $c = shift;
my $short = shift;
my $r = $c->app->pg->db->query('SELECT * FROM files WHERE short = ?', $short)->hashes;
if ($r->size) {
return $c->_slurp($r->first)->record(1);
} else {
return undef;
}
}
sub get_oldest_undeleted_files {
my $c = shift;
my $num = shift;
my @files;
my $records = $c->app->pg->db->query('SELECT * FROM files WHERE deleted = false ORDER BY created_at ASC LIMIT ?', $num)->hashes;
$records->each(
sub {
my ($e, $num) = @_;
my $i = Lufi::DB::File->new(app => $c->app);
push @files, $i->_slurp($e);
}
);
return c(@files);
}
sub get_expired {
my $c = shift;
my $time = shift;
my @files;
## Select only files expired since two days, to be sure that nobody is still downloading it
my $records = $c->app->pg->db->query('SELECT * FROM files WHERE deleted = false AND ((delete_at_day + 2) * 86400) < (? - created_at) AND delete_at_day != 0', $time)->hashes;
$records->each(
sub {
my ($e, $num) = @_;
my $i = Lufi::DB::File->new(app => $c->app);
push @files, $i->_slurp($e);
}
);
return c(@files);
}
sub get_no_longer_viewed {
my $c = shift;
my $time = shift;
my @files;
my $records = $c->app->pg->db->query('SELECT * FROM files WHERE deleted = false AND last_access_at < ?', $time)->hashes;
$records->each(
sub {
my ($e, $num) = @_;
my $i = Lufi::DB::File->new(app => $c->app);
push @files, $i->_slurp($e);
}
);
return c(@files);
}
sub delete_creator_before {
my $c = shift;
my $separation = shift;
$c->app->pg->db->query('UPDATE files SET created_by = NULL WHERE created_by IS NOT NULL AND created_at < ?', $separation);
}
sub _slurp {
my $c = shift;
my $r = shift;
my $file;
if (defined $r) {
$file = $r;
} else {
my $files = $c->app->pg->db->query('SELECT * FROM files WHERE short = ?', $c->short)->hashes;
if ($files->size) {
$file = $files->first;
}
}
if ($file) {
$c->short($file->{short});
$c->deleted($file->{deleted});
$c->mediatype($file->{mediatype});
$c->filename($file->{filename});
$c->filesize($file->{filesize});
$c->counter($file->{counter});
$c->delete_at_first_view($file->{delete_at_first_view});
$c->delete_at_day($file->{delete_at_day});
$c->created_at($file->{created_at});
$c->created_by($file->{created_by});
$c->last_access_at($file->{last_access_at});
$c->mod_token($file->{mod_token});
$c->nbslices($file->{nbslices});
$c->complete($file->{complete});
$c->passwd($file->{passwd});
$c->record(1) unless $c->record;
}
$c->slices(Lufi::DB::Slice->new(app => $c->app)->get_slices_of_file($c->short));
return $c;
}
1;
# vim:set sw=4 ts=4 sts=4 ft=perl expandtab:
package Lufi::DB::File::SQLite;
use Mojo::Base 'Lufi::DB::File';
use Mojo::File;
use Mojo::Collection 'c';
use Lufi::DB::SQLite;
use Lufi::DB::Slice;
has 'record';
sub new {
my $c = shift;
......@@ -17,172 +12,4 @@ sub new {
return $c;
}
sub write {
my $c = shift;
if (defined $c->record) {
$c->record->update(
short => $c->short,
deleted => $c->deleted,
mediatype => $c->mediatype,
filename => $c->filename,
filesize => $c->filesize,
counter => $c->counter,
delete_at_first_view => $c->delete_at_first_view,
delete_at_day => $c->delete_at_day,
created_at => $c->created_at,
created_by => $c->created_by,
last_access_at => $c->last_access_at,
mod_token => $c->mod_token,
nbslices => $c->nbslices,
complete => $c->complete,
passwd => $c->passwd,
);
} else {
my $record = Lufi::DB::SQLite::Files->create(
short => $c->short,
deleted => $c->deleted,
mediatype => $c->mediatype,
filename => $c->filename,
filesize => $c->filesize,
counter => $c->counter,
delete_at_first_view => $c->delete_at_first_view,
delete_at_day => $c->delete_at_day,
created_at => $c->created_at,
created_by => $c->created_by,
last_access_at => $c->last_access_at,
mod_token => $c->mod_token,
nbslices => $c->nbslices,
complete => $c->complete,
passwd => $c->passwd,
);
$c->record($record);
}
return $c;
}
sub count_empty {
my $c = shift;
return Lufi::DB::SQLite::Files->count('WHERE created_at IS NULL');
}
sub already_exists {
my $c = shift;
my $short = shift;
return Lufi::DB::SQLite::Files->count('WHERE short = ?', $short);
}
sub get_empty {
my $c = shift;
my @records = Lufi::DB::SQLite::Files->select('WHERE created_at IS NULL LIMIT 1');
return $c->record($records[0])->_slurp->created_at(time)->write;
}
sub get_stats {
my $c = shift;
my $files = Lufi::DB::SQLite::Files->count('WHERE created_at IS NOT null AND deleted = 0');
my $deleted = Lufi::DB::SQLite::Files->count('WHERE created_at IS NOT null AND deleted = 1');
my $empty = Lufi::DB::SQLite::Files->count('WHERE created_at IS null');
return { files => $files, deleted => $deleted, empty => $empty };
}
sub from_short {
my $c = shift;
my $short = shift;
my @records = Lufi::DB::SQLite::Files->select('WHERE short = ?', $short);
if (scalar @records) {
$c->record($records[0]);
$c->_slurp;
return $c;
} else {
return undef;
}
}
sub get_oldest_undeleted_files {
my $c = shift;
my $num = shift;
my @files = Lufi::DB::SQLite::Files->select('WHERE deleted = 0 ORDER BY created_at ASC LIMIT ?', $num);
return c(map { Lufi::DB::File->new(app => $c->app, record => $_) } @files);
}
sub get_expired {
my $c = shift;
my $time = shift;
## Select only files expired since two days, to be sure that nobody is still downloading it
my @files = Lufi::DB::SQLite::Files->select('WHERE deleted = 0 AND ((delete_at_day + 2) * 86400) < (? - created_at) AND delete_at_day != 0', $time);
return c(map { Lufi::DB::File->new(app => $c->app, record => $_) } @files);
}
sub get_no_longer_viewed {
my $c = shift;
my $time = shift;
my @files = Lufi::DB::SQLite::Files->select('WHERE deleted = 0 AND last_access_at < ?', $time);
return c(map { Lufi::DB::File->new(app => $c->app, record => $_) } @files);
}
sub delete_creator_before {
my $c = shift;
my $separation = shift;
Lufi::DB::SQLite->do(
'UPDATE files SET created_by = NULL WHERE created_by IS NOT NULL AND created_at < ?',
{},
$separation
);
}
sub _slurp {
my $c = shift;
my @files;
if ($c->record) {
@files = ($c->record);
} elsif ($c->short) {
@files = Lufi::DB::SQLite::Files->select('WHERE short = ?', $c->short);
}
if (scalar @files) {
my $file = $files[0];
$c->short($file->short);
$c->deleted($file->deleted) if defined $file->deleted;
$c->mediatype($file->mediatype) if defined $file->mediatype;
$c->filename($file->filename) if defined $file->filename;
$c->filesize($file->filesize) if defined $file->filesize;
$c->counter($file->counter) if defined $file->counter;
$c->delete_at_first_view($file->delete_at_first_view) if defined $file->delete_at_first_view;
$c->delete_at_day($file->delete_at_day) if defined $file->delete_at_day;
$c->created_at($file->created_at) if defined $file->created_at;
$c->created_by($file->created_by) if defined $file->created_by;
$c->last_access_at($file->last_access_at) if defined $file->last_access_at;
$c->mod_token($file->mod_token) if defined $file->mod_token;
$c->nbslices($file->nbslices) if defined $file->nbslices;
$c->complete($file->complete) if defined $file->complete;
$c->passwd($file->passwd) if defined $file->passwd;
$c->record($file) unless $c->record;
}
$c->slices(Lufi::DB::Slice->new(app => $c->app)->get_slices_of_file($c->short));
return $c;
}
1;
# vim:set sw=4 ts=4 sts=4 ft=perl expandtab:
package Lufi::DB::SQLite;
use Mojolicious;
use Mojo::File;
use FindBin qw($Bin);
BEGIN {
my $m = Mojolicious->new;
my $cfile = Mojo::File->new($Bin, '..' , 'lufi.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 => 'lufi.db'
}
}
);
}
# Create database
use ORLite {
file => $config->{db_path},
unicode => 1,
create => sub {
my $dbh = shift;
$dbh->do(
'CREATE TABLE files (
short TEXT PRIMARY KEY,
deleted INTEGER,
mediatype TEXT,
filename TEXT,
filesize INTEGER,
counter INTEGER,
delete_at_first_view INTEGER,
delete_at_day INTEGER,
created_at INTEGER,
created_by TEXT,
last_access_at INTEGER,
mod_token TEXT,
nbslices INTEGER,