An unfinished system to manage all your paper documentation in an easy way.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

autodoc.fcgi 11KB

5 anni fa
5 anni fa
5 anni fa
5 anni fa
5 anni fa
5 anni fa
5 anni fa
5 anni fa
5 anni fa
5 anni fa
5 anni fa
5 anni fa
5 anni fa
5 anni fa
5 anni fa
5 anni fa
5 anni fa
5 anni fa
5 anni fa
5 anni fa
5 anni fa
5 anni fa
5 anni fa
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. #!/usr/bin/perl
  2. use strict;
  3. use FCGI;
  4. use JSON;
  5. use DBI;
  6. use GD::Simple;
  7. use Data::Dumper;
  8. use warnings;
  9. $Data::Dumper::Sortkeys = 1;
  10. my $conf = load_conf("../etc/autodoc.json");
  11. my $dbh = sqlconnect($conf->{sql});
  12. my %map = (
  13. api_v1_POST_documents => \&api_v1_POST_documents,
  14. api_v1_POST_documents_id_data => \&api_v1_POST_documents_id_data,
  15. api_v1_GET_documents_id_image => \&api_v1_GET_documents_id_image,
  16. api_v1_GET_pages_image => \&api_v1_GET_pages_image,
  17. api_v1_GET_documents => \&api_v1_GET_documents,
  18. api_v1_GET_documents_id => \&api_v1_GET_documents_id,
  19. api_v1_GET_pages_id => \&api_v1_GET_pages_id,
  20. api_v1_PATCH_documents_id => \&api_v1_PATCH_documents_id,
  21. );
  22. my $request = FCGI::Request();
  23. while($request->Accept() >= 0) {
  24. my $user = $ENV{REMOTE_USER} || 'undefined';
  25. my $qs = parse_querystring($ENV{QUERY_STRING});
  26. my $method = $ENV{REQUEST_METHOD};
  27. # QS sanity check
  28. if ( exists $qs->{id} && $qs->{id} !~ /^\d+$/ ) {
  29. fatal_api_error(400,"invalid id");
  30. }
  31. if ( exists $qs->{maxWidth} && $qs->{maxWidth} !~ /^\d+$/ ) {
  32. fatal_api_error(400,"invalid maxWidth");
  33. }
  34. if ( exists $qs->{maxHeight} && $qs->{maxHeight} !~ /^\d+$/ ) {
  35. fatal_api_error(400,"invalid maxHeight");
  36. }
  37. if ( exists $qs->{pageSize} && $qs->{pageSize} !~ /^\d+$/ ) {
  38. fatal_api_error(400,"invalid pageSize");
  39. }
  40. if ( exists $qs->{pageIndex} && $qs->{pageIndex} !~ /^\d+$/ ) {
  41. fatal_api_error(400,"invalid pageIndex");
  42. }
  43. my $path = [ split(/\//,$ENV{SCRIPT_NAME}) ] if exists $ENV{SCRIPT_NAME};
  44. shift(@{$path});
  45. my $post = parse_post(
  46. \*STDIN,
  47. exists $ENV{CONTENT_LENGTH} ? $ENV{CONTENT_LENGTH} : 0,
  48. exists $ENV{CONTENT_TYPE} ? $ENV{CONTENT_TYPE} : 0
  49. );
  50. my($code, $hdr, $type, $data) = process_query($method, $path, $qs, $post, $user);
  51. $hdr = [ ] if !defined $hdr;
  52. if ( defined $type ) {
  53. if ( $type eq 'application/json' ) {
  54. $data = to_json($data, { utf8 => 1, pretty => 1 });
  55. }
  56. }
  57. send_response($code, $hdr, $type, $data);
  58. }
  59. sub send_response {
  60. my ($code, $hdr, $type, $data) = @_;
  61. printf("Status: %s\r\n", $code);
  62. printf("Content-type: %s\r\n", $type) if defined $type;
  63. printf("Content-length: %i\r\n", length($data)) if defined $data;
  64. foreach ( @{$hdr} ) { printf("%s\r\n",$_); }
  65. printf("\r\n");
  66. print $data if defined $data;
  67. }
  68. sub fatal_api_error {
  69. my($code,$type,$body)=api_error(@_);
  70. send_response($code, undef, $type, $body);
  71. exit;
  72. }
  73. sub load_conf {
  74. my($file) = @_;
  75. my $x='';
  76. open(F,"$file") || fatal_api_error(500,"Failed to load configuration file");
  77. while(<F>) { $x.=$_; }
  78. close(F);
  79. return from_json($x);
  80. }
  81. sub process_query {
  82. my($method, $path, $qs, $post, $user) = @_;
  83. my ($api_version, $path_main, $path_id, $path_sub) = @{$path};
  84. return api_error(404, "Unknown API version") if !defined $api_version;
  85. return api_error(405, "Unknown METHOD") if !defined $method;
  86. return api_error(404, "Unknown API function") if !defined $path_main;
  87. my $func = 'api_' . $api_version . '_' . $method . '_' . $path_main;
  88. $func .= '_id' if defined $path_id;
  89. $func .= '_'.$path_sub if defined $path_sub;
  90. return $map{$func}->($path_id, $qs, $post, $user) if exists $map{$func};
  91. return api_error(404, "Invalid path/method combination");
  92. }
  93. sub db_get_document_object {
  94. my($id) = @_;
  95. my $document;
  96. my @pages;
  97. my @pageids;
  98. my @tags;
  99. my $q = sqlquery($dbh, "SELECT * FROM documents WHERE id = ?", $id);
  100. while(my $hash = $q->fetchrow_hashref()) { $document = $hash; }
  101. $q = sqlquery($dbh, "SELECT * FROM pages WHERE documentId = ?", $id);
  102. while(my $hash = $q->fetchrow_hashref()) { push @pages, $hash; push @pageids, $hash->{id}; }
  103. $q = sqlquery($dbh, "
  104. SELECT tags.tag AS tag
  105. FROM documents_tags LEFT JOIN tags ON documents_tags.tagId = tags.id
  106. WHERE documentId = ? ORDER BY tag", $id);
  107. while(my ($tag) = $q->fetchrow_array()) { push @tags, $tag; }
  108. my %lang;
  109. foreach my $page ( @pages ) {
  110. $q = sqlquery($dbh, "SELECT * FROM pages_lang WHERE pageId = ?", $page->{id});
  111. while(my $hash = $q->fetchrow_hashref()) {
  112. $lang{$hash->{language}}++;
  113. }
  114. }
  115. if ( defined $document ) {
  116. my $out = {
  117. id => $document->{id},
  118. pageId => [ @pageids ],
  119. name => $document->{name},
  120. created => $document->{created},
  121. owner => $document->{owner},
  122. status => $document->{status},
  123. languages => [ keys %lang ],
  124. tags => [ @tags ],
  125. };
  126. return (200, "application/json", $out);
  127. }
  128. return (404, undef, "application/json", "");
  129. }
  130. sub db_get_page_object {
  131. my($id) = @_;
  132. my $out;
  133. my %lang;
  134. my $q = sqlquery($dbh, "SELECT * FROM pages_lang WHERE pageId = ?", $id);
  135. while(my $hash = $q->fetchrow_hashref()) {
  136. $lang{$hash->{language}}++;
  137. }
  138. $q = sqlquery($dbh, "SELECT * FROM pages WHERE documentId = ?", $id);
  139. while(my $hash = $q->fetchrow_hashref()) {
  140. $out = {
  141. id => $id,
  142. documentId => $hash->{documentId},
  143. name => $hash->{name},
  144. created => $hash->{created},
  145. owner => $hash->{owner},
  146. status => $hash->{status},
  147. language => [ keys %lang ],
  148. };
  149. }
  150. if ( exists $out->{id} ) {
  151. return (200, "application/json", $out);
  152. }
  153. return (404, undef, "application/json", "");
  154. }
  155. sub get_page_image {
  156. my($id, $qs) = @_;
  157. my $wh;
  158. my $size;
  159. if ( exists $qs->{maxWidth} ) {
  160. $wh = 'w';
  161. $size = $qs->{maxWidth};
  162. }
  163. elsif ( exists $qs->{maxHeight} ) {
  164. $wh = 'h';
  165. $size = $qs->{maxWidth};
  166. }
  167. my $src;
  168. my $dst;
  169. if ( !defined $id ) {
  170. $src = sprintf("%s/%s",
  171. $conf->{path}{global}, $conf->{path}{error_img});
  172. }
  173. else {
  174. $src = sprintf("%s/%s/%s",
  175. $conf->{path}{global}, $conf->{path}{original}, $id);
  176. }
  177. if ( defined $wh && defined $size ) {
  178. if ( defined $id ) {
  179. $dst = sprintf("%s/%s/%s-%s-%s.jpeg",
  180. $conf->{path}{global}, $conf->{path}{cache}, $id, $wh, $size);
  181. }
  182. else {
  183. $dst = sprintf("%s/%s/error_img-%s-%s.jpeg",
  184. $conf->{path}{global}, $conf->{path}{cache}, $wh, $size);
  185. }
  186. }
  187. my $imgfile;
  188. if ( !defined $dst ) {
  189. $imgfile = $src;
  190. }
  191. else {
  192. if ( ! -r $dst ) {
  193. my $cmd = sprintf("convert %s -resize %s%s %s",
  194. $src,
  195. $wh eq 'h' ? 'x' : '',
  196. $size,
  197. $dst
  198. );
  199. print STDERR "CMD=$cmd\n";
  200. system($cmd);
  201. }
  202. $imgfile = $dst;
  203. }
  204. my $url = $dst;
  205. $url =~ s/$conf->{path}{global}//;
  206. return $url;
  207. #my $img = '';
  208. #open(IMG, $imgfile) || fatal_api_error(500,"Failed to generate image");
  209. #binmode(IMG);
  210. #while(<IMG>) { $img.=$_; }
  211. #close(IMG);
  212. #return $img;
  213. }
  214. # create an empty document object.
  215. sub api_v1_POST_documents {
  216. my($id, $qs, $post, $user) = @_;
  217. my $q = sqlquery($dbh, "
  218. INSERT INTO documents
  219. SET
  220. owner = ?,
  221. status = 'nodata'
  222. ", $user);
  223. $q = sqlquery($dbh, "SELECT LAST_INSERT_ID()");
  224. while(my($lastid) = $q->fetchrow_array()) {
  225. $id = $lastid;
  226. }
  227. return db_get_document_object($id);
  228. }
  229. sub api_v1_POST_documents_id_data {
  230. my($id, $qs, $post, $user) = @_;
  231. my $pageid;
  232. sqlquery($dbh, "
  233. INSERT INTO
  234. pages
  235. SET
  236. owner = ?,
  237. documentId = ?,
  238. contenttype = ?,
  239. created = NOW(),
  240. status = 'inprogress'",
  241. $user, $id, $post->{ctype});
  242. my $q = sqlquery($dbh, "SELECT LAST_INSERT_ID()");
  243. while(my($last) = $q->fetchrow_array()) {
  244. $pageid = $last;
  245. }
  246. my $file = $conf->{path}{global} . '/' . $conf->{path}{originals} . '/' . $pageid;
  247. open(F,'>'.$file);
  248. print F $post->{data};
  249. close(F);
  250. return (200, undef, "application/json", { })
  251. }
  252. # get document image/thumbnail
  253. sub api_v1_GET_documents_id_image {
  254. my($id, $qs, $post, $user) = @_;
  255. my $pageid;
  256. my $q = sqlquery($dbh, "SELECT id FROM pages WHERE documentId = ? ORDER BY id DESC LIMIT 1", $id);
  257. while(my($myid)=$q->fetchrow_array()) {
  258. $pageid = $myid;
  259. }
  260. return api_v1_GET_pages_id_image($pageid, $qs, $post, $user);
  261. }
  262. # get page image/thumbnail
  263. sub api_v1_GET_pages_id_image {
  264. my($id, $qs, $post, $user) = @_;
  265. #return (200, "image/jpeg", get_page_image($id, $qs));
  266. my $url = get_page_image($id, $qs);
  267. return (302, [ "Location: $url" ]);
  268. }
  269. # get a list of document objects
  270. sub api_v1_GET_documents {
  271. my($id, $qs, $post, $user) = @_;
  272. $qs->{pageSize} = $conf->{query}{pageSize} if !exists $qs->{pageSize};
  273. $qs->{pageIndex} = $conf->{query}{pageIndex} if !exists $qs->{pageIndex};
  274. my @out;
  275. my $q = sqlquery($dbh, "SELECT id FROM documents LIMIT ?,?",
  276. $qs->{pageSize} * $qs->{pageIndex},
  277. $qs->{pageSize});
  278. while(my ($id) = $q->fetchrow_array()) {
  279. my ($code, $ct, $body) = db_get_document_object($id);
  280. push @out, $body;
  281. }
  282. return (
  283. 200,
  284. undef,
  285. "application/json",
  286. \@out
  287. );
  288. }
  289. # get a single document object
  290. sub api_v1_GET_documents_id {
  291. my($id, $qs, $post, $user) = @_;
  292. return db_get_document_object($id);
  293. }
  294. # get a specific page
  295. sub api_v1_GET_pages_id {
  296. my($id, $qs, $post, $user) = @_;
  297. return db_get_page_object($id);
  298. }
  299. # change document properties
  300. sub api_v1_PATCH_documents_id {
  301. my($id, $qs, $post, $user) = @_;
  302. if ( exists $qs->{addTags} ) {
  303. my $tags = get_array($qs->{addTags});
  304. foreach my $tag ( @{$tags} ) {
  305. sqlquery($dbh, "CALL tag_add(?,?)", $id, $tag);
  306. }
  307. }
  308. if ( exists $qs->{deleteTags} ) {
  309. my $tags = get_array($qs->{deleteTags});
  310. foreach my $tag ( @{$tags} ) {
  311. sqlquery($dbh, "CALL tag_del(?,?)", $id, $tag);
  312. }
  313. }
  314. if ( exists $qs->{name} ) {
  315. sqlquery($dbh, "UPDATE documents SET name = ? WHERE id = ?",
  316. $qs->{name}, $id);
  317. }
  318. return (200);
  319. }
  320. sub get_array {
  321. my($x) = @_;
  322. my @arr;
  323. if ( ref $x eq 'ARRAY' ) {
  324. @arr = @{$x};
  325. }
  326. else {
  327. @arr = [ $x ];
  328. }
  329. return \@arr;
  330. }
  331. sub api_error {
  332. my($code, $text)=@_;
  333. my %deftext = (
  334. "000" => "An error produced an internal error, cascading errors over errors",
  335. "404" => "No such API path"
  336. );
  337. $code = "000" if !defined $code;
  338. $text = $deftext{$code} if ( !defined $text || $code eq "000" );
  339. return ( $code, undef, "text/plain", $text . "\r\n" );
  340. }
  341. sub parse_querystring {
  342. my($in) = @_;
  343. my %out;
  344. if ( defined $in && length $in ) {
  345. foreach(split(/&/,$in)) {
  346. my($name,$value) = split(/=/);
  347. $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  348. # handle arrays
  349. if ( exists $out{$name} ) {
  350. if ( ! ref $out{$name} ) {
  351. my $old = $out{$name};
  352. $out{$name} = [ $old ];
  353. }
  354. push @{$out{$name}}, $value;
  355. }
  356. else {
  357. $out{$name}=$value;
  358. }
  359. }
  360. }
  361. return \%out;
  362. }
  363. sub parse_post {
  364. my($fd, $len, $ct) = @_;
  365. my $data = '';
  366. while ( $len > 0 ) {
  367. my $buf;
  368. my $read = read($fd, $buf, $len);
  369. $len -= $read;
  370. $data .= $buf;
  371. }
  372. if ( $ct eq 'application/json' ) {
  373. my $tmp = from_json($data);
  374. $data = $tmp;
  375. }
  376. return { ctype => $ct, len => $len, data => $data};
  377. }
  378. sub sqlconnect {
  379. my($sql) = @_;
  380. my $dsn = "DBI:mysql:database=$sql->{base};host=$sql->{host}";
  381. my $dbh = DBI->connect($dsn, $sql->{user}, $sql->{pass}) || \\
  382. fatal_api_error(500,"Failed to connect to database");
  383. return $dbh;
  384. }
  385. sub sqlquery {
  386. my $dbh = shift;
  387. my $query = shift;
  388. my @args = @_;
  389. my $sth = $dbh->prepare($query) || fatal_api_error(500,"Failed to execute SQL query");
  390. $sth->execute(@args) || fatal_api_error(500,"Failed to execute SQL query");
  391. return $sth;
  392. }