An unfinished system to manage all your paper documentation in an easy way.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

autodoc.fcgi 12KB

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