|
|
@@ -13,14 +13,14 @@ my $conf = load_conf("../etc/autodoc.json"); |
|
|
|
my $dbh = sqlconnect($conf->{sql}); |
|
|
|
|
|
|
|
my %map = ( |
|
|
|
api_v1_POST_documents => &api_v1_POST_documents, |
|
|
|
api_v1_POST_documents_id_data => &api_v1_POST_documents_id_data, |
|
|
|
api_v1_GET_documents_id_image => &api_v1_GET_documents_id_image, |
|
|
|
api_v1_GET_pages_image => &api_v1_GET_pages_image, |
|
|
|
api_v1_GET_documents => &api_v1_GET_documents, |
|
|
|
api_v1_GET_documents_id => &api_v1_GET_documents_id, |
|
|
|
api_v1_GET_pages_id => &api_v1_GET_pages_id, |
|
|
|
api_v1_PATCH_documents_id => &api_v1_PATCH_documents_id, |
|
|
|
api_v1_POST_documents => \&api_v1_POST_documents, |
|
|
|
api_v1_POST_documents_id_data => \&api_v1_POST_documents_id_data, |
|
|
|
api_v1_GET_documents_id_image => \&api_v1_GET_documents_id_image, |
|
|
|
api_v1_GET_pages_image => \&api_v1_GET_pages_image, |
|
|
|
api_v1_GET_documents => \&api_v1_GET_documents, |
|
|
|
api_v1_GET_documents_id => \&api_v1_GET_documents_id, |
|
|
|
api_v1_GET_pages_id => \&api_v1_GET_pages_id, |
|
|
|
api_v1_PATCH_documents_id => \&api_v1_PATCH_documents_id, |
|
|
|
); |
|
|
|
|
|
|
|
my $request = FCGI::Request(); |
|
|
@@ -43,15 +43,17 @@ while($request->Accept() >= 0) { |
|
|
|
|
|
|
|
my($code, $type, $data) = process_query($method, $path, $qs, $post, $user); |
|
|
|
|
|
|
|
if ( $type eq 'application/json' ) { |
|
|
|
$data = to_json($data); |
|
|
|
if ( defined $type ) { |
|
|
|
if ( $type eq 'application/json' ) { |
|
|
|
$data = to_json($data, { utf8 => 1, pretty => 1 }); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
printf("Status: %s\r\n", $code); |
|
|
|
printf("Content-type: %s\r\n", $type); |
|
|
|
printf("Content-length: %i\r\n", length($data)); |
|
|
|
printf("Content-type: %s\r\n", $type) if defined $type; |
|
|
|
printf("Content-length: %i\r\n", length($data)) if defined $data; |
|
|
|
printf("\r\n"); |
|
|
|
print $data; |
|
|
|
print $data if defined $data; |
|
|
|
} |
|
|
|
|
|
|
|
sub load_conf { |
|
|
@@ -71,18 +73,20 @@ sub process_query { |
|
|
|
|
|
|
|
my ($api_version, $path_main, $path_id, $path_sub) = @{$path}; |
|
|
|
|
|
|
|
my $func = sprintf("api_%s_%s_%s", |
|
|
|
defined $api_version ? $api_version : "unknown", |
|
|
|
defined $method ? $method : "unknown", |
|
|
|
defined $path_main ? $path_main : "unknown" |
|
|
|
); |
|
|
|
return api_error(404, "Unknown API version") if !defined $api_version; |
|
|
|
return api_error(405, "Unknown METHOD") if !defined $method; |
|
|
|
return api_error(404, "Unknown API function") if !defined $path_main; |
|
|
|
|
|
|
|
my $func = 'api_' . $api_version . '_' . $method . '_' . $path_main; |
|
|
|
|
|
|
|
$func .= '_id' if defined $path_id; |
|
|
|
$func .= '_'.$path_sub if defined $path_sub; |
|
|
|
|
|
|
|
print STDERR "FUNC=$map{$func}\n"; |
|
|
|
|
|
|
|
return $map{$func}->($path_id, $qs, $post, $user) if exists $map{$func}; |
|
|
|
return api_error(404); |
|
|
|
|
|
|
|
return api_error(404, "Invalid path/method combination"); |
|
|
|
} |
|
|
|
|
|
|
|
sub db_get_document_object { |
|
|
@@ -174,7 +178,48 @@ sub api_v1_GET_documents_id { |
|
|
|
} |
|
|
|
|
|
|
|
sub api_v1_GET_pages_id { return api_error(501,"Not yet implemented"); } |
|
|
|
sub api_v1_PATCH_documents_id { return api_error(501,"Not yet implemented"); } |
|
|
|
|
|
|
|
# change document properties |
|
|
|
sub api_v1_PATCH_documents_id { |
|
|
|
my($id, $qs, $post, $user) = @_; |
|
|
|
|
|
|
|
if ( exists $qs->{addTags} ) { |
|
|
|
my $tags = get_array($qs->{addTags}); |
|
|
|
|
|
|
|
foreach my $tag ( @{$tags} ) { |
|
|
|
sqlquery($dbh, "CALL tag_add(?,?)", $id, $tag); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if ( exists $qs->{deleteTags} ) { |
|
|
|
my $tags = get_array($qs->{deleteTags}); |
|
|
|
|
|
|
|
foreach my $tag ( @{$tags} ) { |
|
|
|
sqlquery($dbh, "CALL tag_del(?,?)", $id, $tag); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if ( exists $qs->{name} ) { |
|
|
|
sqlquery($dbh, "UPDATE documents SET name = ? WHERE id = ?", |
|
|
|
$qs->{name}, $id); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return (200); |
|
|
|
} |
|
|
|
|
|
|
|
sub get_array { |
|
|
|
my($x) = @_; |
|
|
|
my @arr; |
|
|
|
|
|
|
|
if ( ref $x eq 'ARRAY' ) { |
|
|
|
@arr = @{$x}; |
|
|
|
} |
|
|
|
else { |
|
|
|
@arr = [ $x ]; |
|
|
|
} |
|
|
|
return \@arr; |
|
|
|
} |
|
|
|
|
|
|
|
sub fatal_api_error { |
|
|
|
my($code,$type,$body)=api_error(@_); |
|
|
@@ -198,7 +243,7 @@ sub api_error { |
|
|
|
$code = "000" if !defined $code; |
|
|
|
$text = $deftext{$code} if ( !defined $text || $code eq "000" ); |
|
|
|
|
|
|
|
return ( $code, "text/plain", $text ); |
|
|
|
return ( $code, "text/plain", $text . "\r\n" ); |
|
|
|
} |
|
|
|
|
|
|
|
sub parse_querystring { |
|
|
@@ -210,10 +255,20 @@ sub parse_querystring { |
|
|
|
foreach(split(/&/,$in)) { |
|
|
|
my($name,$value) = split(/=/); |
|
|
|
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; |
|
|
|
$out{$name}=$value; |
|
|
|
|
|
|
|
# handle arrays |
|
|
|
if ( exists $out{$name} ) { |
|
|
|
if ( ! ref $out{$name} ) { |
|
|
|
my $old = $out{$name}; |
|
|
|
$out{$name} = [ $old ]; |
|
|
|
} |
|
|
|
push @{$out{$name}}, $value; |
|
|
|
} |
|
|
|
else { |
|
|
|
$out{$name}=$value; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return \%out; |
|
|
|
} |
|
|
|
|