An unfinished system to manage all your paper documentation in an easy way.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

autodoc.fcgi 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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 $original = sprintf("%s/%s/%s.jpeg",
  157. $conf->{path}{global}, $conf->{path}{original}, $id);
  158. my $cache_file = sprintf("%s/%s/%s-original.jpeg",
  159. $conf->{path}{global}, $conf->{path}{images}, $id);
  160. if ( defined $wh && defined $size ) {
  161. $cache_file = sprintf("%s/%s/%s-%s-%s.jpeg",
  162. $conf->{path}{global}, $conf->{path}{images}, $id, $wh, $size);
  163. }
  164. gen_thumbnail($original, $cache_file, $wh, $size) if !-r $cache_file;
  165. my $img='';
  166. if ( -r $cache_file ) {
  167. open(IMG, $cache_file) || fatal_api_error(500, "Thumbnail generation failed");
  168. binmode(IMG);
  169. while(<IMG>) { $img.=$_; }
  170. close(IMG);
  171. }
  172. else {
  173. $img = gen_error_img($wh, $size, "No Image");
  174. }
  175. return (200, "image/jpeg", $img);
  176. }
  177. sub gen_thumbnail {
  178. my($original, $cache, $wh, $size) = @_;
  179. if ( -r $original ) {
  180. system(sprintf("convert %s -resize %s%s %s",
  181. $original,
  182. $wh eq 'h' ? 'x' : '',
  183. $size,
  184. $cache
  185. ));
  186. }
  187. }
  188. sub gen_error_img {
  189. my($wh, $size, $text) = @_;
  190. my $w = $size;
  191. my $h = $size;
  192. $h = int($w*(sqrt(2))) if $wh eq 'w';
  193. $w = int($h/(sqrt(2))) if $wh eq 'h';
  194. my $img = GD::Simple->new($w, $h);
  195. $img->string($text);
  196. return $img->jpeg;
  197. }
  198. # create an empty document object.
  199. sub api_v1_POST_documents {
  200. my($id, $qs, $post, $user) = @_;
  201. my $q = sqlquery($dbh, "
  202. INSERT INTO documents
  203. SET
  204. owner = ?,
  205. status = 'nodata'
  206. ", $user);
  207. $q = sqlquery($dbh, "SELECT LAST_INSERT_ID()");
  208. while(my($lastid) = $q->fetchrow_array()) {
  209. $id = $lastid;
  210. }
  211. return db_get_document_object($id);
  212. }
  213. sub api_v1_POST_documents_id_data { return api_error(501,"Not yet implemented"); }
  214. sub api_v1_GET_documents_id_image {
  215. my($id, $qs, $post, $user) = @_;
  216. return api_error(501,"Not yet implemented");
  217. }
  218. sub api_v1_GET_pages_image {
  219. my($id, $qs, $post, $user) = @_;
  220. get_page_image($id, $qs);
  221. return api_error(501,"Not yet implemented");
  222. }
  223. # get a list of document objects
  224. sub api_v1_GET_documents {
  225. my($id, $qs, $post, $user) = @_;
  226. $qs->{pageSize} = $conf->{query}{pageSize} if !exists $qs->{pageSize};
  227. $qs->{pageIndex} = $conf->{query}{pageIndex} if !exists $qs->{pageIndex};
  228. my @out;
  229. my $q = sqlquery($dbh, "SELECT id FROM documents LIMIT ?,?",
  230. $qs->{pageSize} * $qs->{pageIndex},
  231. $qs->{pageSize});
  232. while(my ($id) = $q->fetchrow_array()) {
  233. my ($code, $ct, $body) = db_get_document_object($id);
  234. push @out, $body;
  235. }
  236. return (
  237. 200,
  238. "application/json",
  239. \@out
  240. );
  241. }
  242. # get a single document object
  243. sub api_v1_GET_documents_id {
  244. my($id, $qs, $post, $user) = @_;
  245. return db_get_document_object($id);
  246. }
  247. # get a specific page
  248. sub api_v1_GET_pages_id {
  249. my($id, $qs, $post, $user) = @_;
  250. return db_get_page_object($id);
  251. }
  252. # change document properties
  253. sub api_v1_PATCH_documents_id {
  254. my($id, $qs, $post, $user) = @_;
  255. if ( exists $qs->{addTags} ) {
  256. my $tags = get_array($qs->{addTags});
  257. foreach my $tag ( @{$tags} ) {
  258. sqlquery($dbh, "CALL tag_add(?,?)", $id, $tag);
  259. }
  260. }
  261. if ( exists $qs->{deleteTags} ) {
  262. my $tags = get_array($qs->{deleteTags});
  263. foreach my $tag ( @{$tags} ) {
  264. sqlquery($dbh, "CALL tag_del(?,?)", $id, $tag);
  265. }
  266. }
  267. if ( exists $qs->{name} ) {
  268. sqlquery($dbh, "UPDATE documents SET name = ? WHERE id = ?",
  269. $qs->{name}, $id);
  270. }
  271. return (200);
  272. }
  273. sub get_array {
  274. my($x) = @_;
  275. my @arr;
  276. if ( ref $x eq 'ARRAY' ) {
  277. @arr = @{$x};
  278. }
  279. else {
  280. @arr = [ $x ];
  281. }
  282. return \@arr;
  283. }
  284. sub api_error {
  285. my($code, $text)=@_;
  286. my %deftext = (
  287. "000" => "An error produced an internal error, cascading errors over errors",
  288. "404" => "No such API path"
  289. );
  290. $code = "000" if !defined $code;
  291. $text = $deftext{$code} if ( !defined $text || $code eq "000" );
  292. return ( $code, "text/plain", $text . "\r\n" );
  293. }
  294. sub parse_querystring {
  295. my($in) = @_;
  296. my %out;
  297. if ( defined $in && length $in ) {
  298. foreach(split(/&/,$in)) {
  299. my($name,$value) = split(/=/);
  300. $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  301. # handle arrays
  302. if ( exists $out{$name} ) {
  303. if ( ! ref $out{$name} ) {
  304. my $old = $out{$name};
  305. $out{$name} = [ $old ];
  306. }
  307. push @{$out{$name}}, $value;
  308. }
  309. else {
  310. $out{$name}=$value;
  311. }
  312. }
  313. }
  314. return \%out;
  315. }
  316. sub parse_post {
  317. my($fd, $len, $ct) = @_;
  318. my $data = '';
  319. while ( $len > 0 ) {
  320. my $buf;
  321. my $read = read($fd, $buf, $len);
  322. $len -= $read;
  323. $data .= $buf;
  324. }
  325. if ( $ct eq 'application/json' ) {
  326. my $tmp = from_json($data);
  327. $data = $tmp;
  328. }
  329. return $data;
  330. }
  331. sub sqlconnect {
  332. my($sql) = @_;
  333. my $dsn = "DBI:mysql:database=$sql->{base};host=$sql->{host}";
  334. my $dbh = DBI->connect($dsn, $sql->{user}, $sql->{pass}) || \\
  335. fatal_api_error(500,"Failed to connect to database");
  336. return $dbh;
  337. }
  338. sub sqlquery {
  339. my $dbh = shift;
  340. my $query = shift;
  341. my @args = @_;
  342. my $sth = $dbh->prepare($query) || fatal_api_error(500,"Failed to execute SQL query");
  343. $sth->execute(@args) || fatal_api_error(500,"Failed to execute SQL query");
  344. return $sth;
  345. }