Explorar el Código

img support

master
Pascal Gloor hace 5 años
padre
commit
9f50d1bbce
Se han modificado 2 ficheros con 90 adiciones y 2 borrados
  1. 1
    0
      .gitignore
  2. 89
    2
      cgi/autodoc.fcgi

+ 1
- 0
.gitignore Ver fichero

@@ -1,2 +1,3 @@
.DS_Store
etc/autodoc.json
.vstags

+ 89
- 2
cgi/autodoc.fcgi Ver fichero

@@ -4,6 +4,7 @@ use strict;
use FCGI;
use JSON;
use DBI;
use GD::Simple;
use Data::Dumper;
use warnings;

@@ -31,6 +32,13 @@ while($request->Accept() >= 0) {
my $qs = parse_querystring($ENV{QUERY_STRING});
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};
shift(@{$path});

@@ -183,6 +191,76 @@ sub db_get_page_object {
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.
sub api_v1_POST_documents {
my($id, $qs, $post, $user) = @_;
@@ -202,8 +280,17 @@ sub api_v1_POST_documents {
}

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
sub api_v1_GET_documents {

Cargando…
Cancelar
Guardar