An unfinished system to manage all your paper documentation in an easy way.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

autodoc.fcgi 10KB

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