|
|
|
|
|
|
|
|
use FCGI; |
|
|
use FCGI; |
|
|
use JSON; |
|
|
use JSON; |
|
|
use DBI; |
|
|
use DBI; |
|
|
|
|
|
use GD::Simple; |
|
|
use Data::Dumper; |
|
|
use Data::Dumper; |
|
|
use warnings; |
|
|
use warnings; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
my $qs = parse_querystring($ENV{QUERY_STRING}); |
|
|
my $qs = parse_querystring($ENV{QUERY_STRING}); |
|
|
my $method = $ENV{REQUEST_METHOD}; |
|
|
my $method = $ENV{REQUEST_METHOD}; |
|
|
|
|
|
|
|
|
|
|
|
# QS sanity check |
|
|
|
|
|
if ( exists $qs->{id} && $qs->{id} !~ /^\d+$/ ) { fatal_api_error(400,"invalid id"); } |
|
|
|
|
|
if ( exists $qs->{maxWidth} && $qs->{maxWidth} !~ /^\d+$/ ) { fatal_api_error(400,"invalid maxWidth"); } |
|
|
|
|
|
if ( exists $qs->{maxHeight} && $qs->{maxHeight} !~ /^\d+$/ ) { fatal_api_error(400,"invalid maxHeight"); } |
|
|
|
|
|
if ( exists $qs->{pageSize} && $qs->{pageSize} !~ /^\d+$/ ) { fatal_api_error(400,"invalid pageSize"); } |
|
|
|
|
|
if ( exists $qs->{pageIndex} && $qs->{pageIndex} !~ /^\d+$/ ) { fatal_api_error(400,"invalid pageIndex"); } |
|
|
|
|
|
|
|
|
my $path = [ split(/\//,$ENV{SCRIPT_NAME}) ] if exists $ENV{SCRIPT_NAME}; |
|
|
my $path = [ split(/\//,$ENV{SCRIPT_NAME}) ] if exists $ENV{SCRIPT_NAME}; |
|
|
shift(@{$path}); |
|
|
shift(@{$path}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return (404, "application/json", ""); |
|
|
return (404, "application/json", ""); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
sub get_page_image { |
|
|
|
|
|
my($id, $qs) = @_; |
|
|
|
|
|
|
|
|
|
|
|
my $wh; |
|
|
|
|
|
my $size; |
|
|
|
|
|
if ( exists $qs->{maxWidth} ) { |
|
|
|
|
|
$wh = 'w'; |
|
|
|
|
|
$size = $qs->{maxWidth}; |
|
|
|
|
|
} |
|
|
|
|
|
elsif ( exists $qs->{maxHeight} ) { |
|
|
|
|
|
$wh = 'h'; |
|
|
|
|
|
$size = $qs->{maxWidth}; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
my $original = sprintf("%s/%s/%s.jpeg", |
|
|
|
|
|
$conf->{path}{global}, $conf->{path}{original}, $id); |
|
|
|
|
|
|
|
|
|
|
|
my $cache_file = sprintf("%s/%s/%s-original.jpeg", |
|
|
|
|
|
$conf->{path}{global}, $conf->{path}{images}, $id); |
|
|
|
|
|
|
|
|
|
|
|
if ( defined $wh && defined $size ) { |
|
|
|
|
|
$cache_file = sprintf("%s/%s/%s-%s-%s.jpeg", |
|
|
|
|
|
$conf->{path}{global}, $conf->{path}{images}, $id, $wh, $size); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gen_thumbnail($original, $cache_file, $wh, $size) if !-r $cache_file; |
|
|
|
|
|
|
|
|
|
|
|
my $img=''; |
|
|
|
|
|
|
|
|
|
|
|
if ( -r $cache_file ) { |
|
|
|
|
|
open(IMG, $cache_file) || fatal_api_error(500, "Thumbnail generation failed"); |
|
|
|
|
|
binmode(IMG); |
|
|
|
|
|
while(<IMG>) { $img.=$_; } |
|
|
|
|
|
close(IMG); |
|
|
|
|
|
} |
|
|
|
|
|
else { |
|
|
|
|
|
$img = gen_error_img($wh, $size, "No Image"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return (200, "image/jpeg", $img); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
sub gen_thumbnail { |
|
|
|
|
|
my($original, $cache, $wh, $size) = @_; |
|
|
|
|
|
|
|
|
|
|
|
if ( -r $original ) { |
|
|
|
|
|
system(sprintf("convert %s -resize %s%s %s", |
|
|
|
|
|
$original, |
|
|
|
|
|
$wh eq 'h' ? 'x' : '', |
|
|
|
|
|
$size, |
|
|
|
|
|
$cache |
|
|
|
|
|
)); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
sub gen_error_img { |
|
|
|
|
|
my($wh, $size, $text) = @_; |
|
|
|
|
|
|
|
|
|
|
|
my $w = $size; |
|
|
|
|
|
my $h = $size; |
|
|
|
|
|
|
|
|
|
|
|
$h = int($w*(sqrt(2))) if $wh eq 'w'; |
|
|
|
|
|
$w = int($h/(sqrt(2))) if $wh eq 'h'; |
|
|
|
|
|
|
|
|
|
|
|
my $img = GD::Simple->new($w, $h); |
|
|
|
|
|
$img->string($text); |
|
|
|
|
|
return $img->jpeg; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
# create an empty document object. |
|
|
# create an empty document object. |
|
|
sub api_v1_POST_documents { |
|
|
sub api_v1_POST_documents { |
|
|
my($id, $qs, $post, $user) = @_; |
|
|
my($id, $qs, $post, $user) = @_; |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
sub api_v1_POST_documents_id_data { return api_error(501,"Not yet implemented"); } |
|
|
sub api_v1_POST_documents_id_data { return api_error(501,"Not yet implemented"); } |
|
|
sub api_v1_GET_documents_id_image { return api_error(501,"Not yet implemented"); } |
|
|
|
|
|
sub api_v1_GET_pages_image { return api_error(501,"Not yet implemented"); } |
|
|
|
|
|
|
|
|
sub api_v1_GET_documents_id_image { |
|
|
|
|
|
my($id, $qs, $post, $user) = @_; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return api_error(501,"Not yet implemented"); |
|
|
|
|
|
} |
|
|
|
|
|
sub api_v1_GET_pages_image { |
|
|
|
|
|
my($id, $qs, $post, $user) = @_; |
|
|
|
|
|
get_page_image($id, $qs); |
|
|
|
|
|
return api_error(501,"Not yet implemented"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
# get a list of document objects |
|
|
# get a list of document objects |
|
|
sub api_v1_GET_documents { |
|
|
sub api_v1_GET_documents { |