| 1 | package Net::FTPTurboSync::UploadedFiles; | ||
| 2 | |||
| 3 | |||
| 4 | # a wrapper around a db | ||
| 5 | use DBI; | ||
| 6 | sub new { | ||
| 7 | my ($class, $dbh ) = @_; | ||
| 8 | my $self = { dbh => $dbh }; | ||
| 9 | bless $self, $class; | ||
| 10 | return $self; | ||
| 11 | } | ||
| 12 | sub selectAllFiles(){ | ||
| 13 | my ( $self ) = @_; | ||
| 14 | return $self->{dbh}->selectall_arrayref( | ||
| 15 | 'select perms, uploaded as "date", fullname, objsize as "size"' | ||
| 16 | . ' from files where objtype = \'f\' order by length(fullname)', | ||
| 17 | { Slice => {} } | ||
| 18 | ); | ||
| 19 | } | ||
| 20 | sub selectAllDirs(){ | ||
| 21 | my ( $self ) = @_; | ||
| 22 | return $self->{dbh}->selectall_arrayref( | ||
| 23 | 'select fullname, perms from files where objtype = \'d\' order by length(fullname)', | ||
| 24 | { Slice => {} } | ||
| 25 | ); | ||
| 26 | } | ||
| 27 | #delete a file or a directory | ||
| 28 | sub deleteFile { | ||
| 29 | my ($self, $fileName) = @_; | ||
| 30 | my $sth = $self->{dbh}->prepare("delete from files where fullname = ?"); | ||
| 31 | $sth->bind_param( 1, $fileName, DBI::SQL_VARCHAR ); | ||
| 32 | $sth->execute(); | ||
| 33 | } | ||
| 34 | sub createDir () { | ||
| 35 | my ($self, $dirname, $perms ) = @_; | ||
| 36 | my $sth = $self->{dbh}->prepare("insert into files ( objtype, fullname, perms ) values ('d',?,?)" ); | ||
| 37 | $sth->bind_param ( 1, $dirname, DBI::SQL_VARCHAR ); | ||
| 38 | $sth->bind_param ( 2, $perms, DBI::SQL_INTEGER ); | ||
| 39 | $sth->execute() ; | ||
| 40 | } | ||
| 41 | sub setPerms() { | ||
| 42 | my ( $self, $fileName, $perms ) = @_; | ||
| 43 | my $sth = $self->{dbh}->prepare("update files set perms = ? where fullname = ?" ); | ||
| 44 | $sth->bind_param ( 1, $perms, DBI::SQL_INTEGER ); | ||
| 45 | $sth->bind_param ( 2, $fileName, DBI::SQL_VARCHAR ); | ||
| 46 | $sth->execute(); | ||
| 47 | } | ||
| 48 | sub uploadFile() { | ||
| 49 | my ($self, $fileName, $perms, $date, $size ) = @_ ; | ||
| 50 | my $sth = $self->{dbh}->prepare("insert into files ( objtype, fullname, perms, uploaded, objsize ) | ||
| 51 | values ('f',?,?,?,?)" ); | ||
| 52 | $sth->bind_param ( 1, $fileName, DBI::SQL_VARCHAR ); | ||
| 53 | $sth->bind_param ( 2, $perms, DBI::SQL_INTEGER ); | ||
| 54 | $sth->bind_param ( 3, $date, DBI::SQL_INTEGER ); | ||
| 55 | $sth->bind_param ( 4, $size, DBI::SQL_INTEGER ); | ||
| 56 | $sth->execute(); | ||
| 57 | } | ||
| 58 | sub getInfo() { | ||
| 59 | my ( $self, $fileName ) = @_; | ||
| 60 | my $fileinfo = $self->{dbh}->selectrow_arrayref( | ||
| 61 | 'select uploaded as "date", objsize as "size", perms from files where fullname = ?', | ||
| 62 | { Slice => {} }, | ||
| 63 | $fileName | ||
| 64 | ); | ||
| 65 | return $fileinfo; | ||
| 66 | } | ||
| 67 | sub reuploadFile (){ | ||
| 68 | my ($self, $fileName, $perms, $date, $size ) = @_ ; | ||
| 69 | my $sth = $self->{dbh}->prepare("update files set perms=?, uploaded=?, objsize=? | ||
| 70 | where objtype='f' and fullname=?") ; | ||
| 71 | $sth->bind_param ( 4, $fileName, DBI::SQL_VARCHAR ); | ||
| 72 | $sth->bind_param ( 1, $perms, DBI::SQL_INTEGER ); | ||
| 73 | $sth->bind_param ( 2, $date, DBI::SQL_INTEGER ); | ||
| 74 | $sth->bind_param ( 3, $size, DBI::SQL_INTEGER ); | ||
| 75 | $sth->execute(); | ||
| 76 | } | ||
| 77 | sub deployScheme () { | ||
| 78 | my ($self) = @_; | ||
| 79 | $self->{dbh}->do("create table files (fullname text primary key, | ||
| 80 | uploaded integer, objtype char, | ||
| 81 | objsize integer, perms integer)") | ||
| 82 | or die "cannot deploy db scheme"; | ||
| 83 | } | ||
| 84 | |||
| 85 | 1; |