1

Finished upload.pm, fixed cpanfile + bootstrap.sh

* Переименован конфиг
* Исправлены cpanfile и bootstrap.shб теперь они работают
* loadConf и saveConf перемещены в conf.pm из myapi.psgi
* Исправлен синтаксис README.md согласно форматированию GitHub
* Обновлён .gitignore
* Закончена работа над upload.pm
This commit is contained in:
eleksir
2019-09-21 21:23:30 +03:00
parent 9953600dae
commit 4495529710
8 changed files with 135 additions and 85 deletions

35
lib/conf.pm Normal file
View File

@@ -0,0 +1,35 @@
package conf;
use strict;
use warnings "all";
use diagnostics;
use vars qw/$VERSION/;
use JSON::PP;
use Exporter qw(import);
our @EXPORT = qw(loadConf saveConf);
$VERSION = "1.0";
sub loadConf() {
my $c = "data/myapi.json";
my $sep = $/;
$/ = '';
open (C, $c) or die "No conf at $c\n";
my $json = <C>;
close C;
return decode_json($json);
}
sub saveConf($) {
my $c = shift;
my $file = "data/myapi.json";
my $j = JSON::PP->new->pretty->canonical->indent_length(4);
my $json = $j->encode($c);
# TODO: make it transactional
open (C, ">", $file) or die "Unable to open $file\n";
print C $json;
close C;
}
1;

View File

@@ -19,9 +19,23 @@ sub upload {
my $len = shift;
my $name = shift;
my ($status, $content, $msg) = ('400', 'text/plain', "Bad Request?\n");
my $d;
($d, $name) = split(/\//, $name, 2);
my $match;
return ($status, $content, $msg) unless ($name =~ /^[A-Z|a-z|0-9|_|\-|\+]+$/);
foreach (keys(%{$c->{upload}->{dir}})) {
if ($d eq $_) {
$match = 1;
last;
}
}
return ($status, $content, $msg) unless($match); # incorrect destination in url
$name = sprintf("%s/%s", $c->{upload}->{dir}->{$d}, $name);
if ($len > 0) {
if (sysopen (F, sprintf("%s/%s", $c->{upload}->{dir}, $name), O_CREAT|O_TRUNC|O_WRONLY)) {
if (sysopen (F, $name, O_CREAT|O_TRUNC|O_WRONLY)) {
my $buf;
my $readlen = 0;
my $totalread = 0;
@@ -33,7 +47,13 @@ sub upload {
do {
$readlen = $input->read($buf, $buflen);
syswrite F, $buf, $readlen;
unless (defined(syswrite F, $buf, $readlen)) { # out of space?
close F;
unlink $name;
return ('500', $content, "An error has occured during upload: $!\n");
}
$totalread += $readlen;
} while ($readlen == $buflen);