An unfinished system to manage all your paper documentation in an easy way.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

autodoc.fcgi 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. return $map{$func}->($path_id, $qs, $post, $user) if exists $map{$func};
  79. return api_error(404, "Invalid path/method combination");
  80. }
  81. sub db_get_document_object {
  82. my($id) = @_;
  83. my $document;
  84. my @pages;
  85. my @pageids;
  86. my @tags;
  87. my $q = sqlquery($dbh, "SELECT * FROM documents WHERE id = ?", $id);
  88. while(my $hash = $q->fetchrow_hashref()) { $document = $hash; }
  89. $q = sqlquery($dbh, "SELECT * FROM pages WHERE documentId = ?", $id);
  90. while(my $hash = $q->fetchrow_hashref()) { push @pages, $hash; push @pageids, $hash->{id}; }
  91. $q = sqlquery($dbh, "
  92. SELECT tags.tag AS tag
  93. FROM documents_tags LEFT JOIN tags ON documents_tags.tagId = tags.id
  94. WHERE documentId = ? ORDER BY tag", $id);
  95. while(my ($tag) = $q->fetchrow_array()) { push @tags, $tag; }
  96. my %lang;
  97. foreach my $page ( @pages ) {
  98. $q = sqlquery($dbh, "SELECT * FROM pages_lang WHERE pageId = ?", $page->{id});
  99. while(my $hash = $q->fetchrow_hashref()) {
  100. $lang{$hash->{language}}++;
  101. }
  102. }
  103. if ( defined $document ) {
  104. my $out = {
  105. id => $document->{id},
  106. pageId => [ @pageids ],
  107. name => $document->{name},
  108. created => $document->{created},
  109. owner => $document->{owner},
  110. status => $document->{status},
  111. languages => [ keys %lang ],
  112. tags => [ @tags ],
  113. };
  114. return (200, "application/json", $out);
  115. }
  116. return (404, "application/json", "");
  117. }
  118. sub db_get_page_object {
  119. my($id) = @_;
  120. my $out;
  121. my %lang;
  122. my $q = sqlquery($dbh, "SELECT * FROM pages_lang WHERE pageId = ?", $id);
  123. while(my $hash = $q->fetchrow_hashref()) {
  124. $lang{$hash->{language}}++;
  125. }
  126. $q = sqlquery($dbh, "SELECT * FROM pages WHERE documentId = ?", $id);
  127. while(my $hash = $q->fetchrow_hashref()) {
  128. $out = {
  129. id => $id,
  130. documentId => $hash->{documentId},
  131. name => $hash->{name},
  132. created => $hash->{created},
  133. owner => $hash->{owner},
  134. status => $hash->{status},
  135. language => [ keys %lang ],
  136. };
  137. }
  138. if ( exists $out->{id} ) {
  139. return (200, "application/json", $out);
  140. }
  141. return (404, "application/json", "");
  142. }
  143. sub get_page_image {
  144. my($id, $qs) = @_;
  145. my $wh;
  146. my $size;
  147. if ( exists $qs->{maxWidth} ) {
  148. $wh = 'w';
  149. $size = $qs->{maxWidth};
  150. }
  151. elsif ( exists $qs->{maxHeight} ) {
  152. $wh = 'h';
  153. $size = $qs->{maxWidth};
  154. }
  155. my $src;
  156. my $dst;
  157. if ( !defined $id ) {
  158. $src = sprintf("%s/%s",
  159. $conf->{path}{global}, $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. my $cmd = sprintf("convert %s -resize %s%s %s",
  182. $src,
  183. $wh eq 'h' ? 'x' : '',
  184. $size,
  185. $dst
  186. );
  187. print STDERR "CMD=$cmd\n";
  188. system($cmd);
  189. }
  190. $imgfile = $dst;
  191. }
  192. my $img = '';
  193. open(IMG, $imgfile) || fatal_api_error(500,"Failed to generate image");
  194. binmode(IMG);
  195. while(<IMG>) { $img.=$_; }
  196. close(IMG);
  197. return $img;
  198. }
  199. # create an empty document object.
  200. sub api_v1_POST_documents {
  201. my($id, $qs, $post, $user) = @_;
  202. my $q = sqlquery($dbh, "
  203. INSERT INTO documents
  204. SET
  205. owner = ?,
  206. status = 'nodata'
  207. ", $user);
  208. $q = sqlquery($dbh, "SELECT LAST_INSERT_ID()");
  209. while(my($lastid) = $q->fetchrow_array()) {
  210. $id = $lastid;
  211. }
  212. return db_get_document_object($id);
  213. }
  214. sub api_v1_POST_documents_id_data {
  215. my($id, $qs, $post, $user) = @_;
  216. open(TMP,">/tmp/upload.meta");
  217. print TMP $post->{ctype} . "\n";
  218. print TMP $post->{len} . "\n";
  219. print TMP Dumper(\%ENV);
  220. close(TMP);
  221. open(TMP,">/tmp/upload.data");
  222. print TMP $post->{data} . "\n";
  223. close(TMP);
  224. return (200, "application/json", { })
  225. }
  226. # get document image/thumbnail
  227. sub api_v1_GET_documents_id_image {
  228. my($id, $qs, $post, $user) = @_;
  229. my $pageid;
  230. my $q = sqlquery($dbh, "SELECT id FROM pages WHERE documentId = ? ORDER BY id DESC LIMIT 1", $id);
  231. while(my($myid)=$q->fetchrow_array()) {
  232. $pageid = $myid;
  233. }
  234. return api_v1_GET_pages_id_image($pageid, $qs, $post, $user);
  235. }
  236. # get page image/thumbnail
  237. sub api_v1_GET_pages_id_image {
  238. my($id, $qs, $post, $user) = @_;
  239. return (200, "image/jpeg", get_page_image($id, $qs));
  240. }
  241. # get a list of document objects
  242. sub api_v1_GET_documents {
  243. my($id, $qs, $post, $user) = @_;
  244. $qs->{pageSize} = $conf->{query}{pageSize} if !exists $qs->{pageSize};
  245. $qs->{pageIndex} = $conf->{query}{pageIndex} if !exists $qs->{pageIndex};
  246. my @out;
  247. my $q = sqlquery($dbh, "SELECT id FROM documents LIMIT ?,?",
  248. $qs->{pageSize} * $qs->{pageIndex},
  249. $qs->{pageSize});
  250. while(my ($id) = $q->fetchrow_array()) {
  251. my ($code, $ct, $body) = db_get_document_object($id);
  252. push @out, $body;
  253. }
  254. return (
  255. 200,
  256. "application/json",
  257. \@out
  258. );
  259. }
  260. # get a single document object
  261. sub api_v1_GET_documents_id {
  262. my($id, $qs, $post, $user) = @_;
  263. return db_get_document_object($id);
  264. }
  265. # get a specific page
  266. sub api_v1_GET_pages_id {
  267. my($id, $qs, $post, $user) = @_;
  268. return db_get_page_object($id);
  269. }
  270. # change document properties
  271. sub api_v1_PATCH_documents_id {
  272. my($id, $qs, $post, $user) = @_;
  273. if ( exists $qs->{addTags} ) {
  274. my $tags = get_array($qs->{addTags});
  275. foreach my $tag ( @{$tags} ) {
  276. sqlquery($dbh, "CALL tag_add(?,?)", $id, $tag);
  277. }
  278. }
  279. if ( exists $qs->{deleteTags} ) {
  280. my $tags = get_array($qs->{deleteTags});
  281. foreach my $tag ( @{$tags} ) {
  282. sqlquery($dbh, "CALL tag_del(?,?)", $id, $tag);
  283. }
  284. }
  285. if ( exists $qs->{name} ) {
  286. sqlquery($dbh, "UPDATE documents SET name = ? WHERE id = ?",
  287. $qs->{name}, $id);
  288. }
  289. return (200);
  290. }
  291. sub get_array {
  292. my($x) = @_;
  293. my @arr;
  294. if ( ref $x eq 'ARRAY' ) {
  295. @arr = @{$x};
  296. }
  297. else {
  298. @arr = [ $x ];
  299. }
  300. return \@arr;
  301. }
  302. sub api_error {
  303. my($code, $text)=@_;
  304. my %deftext = (
  305. "000" => "An error produced an internal error, cascading errors over errors",
  306. "404" => "No such API path"
  307. );
  308. $code = "000" if !defined $code;
  309. $text = $deftext{$code} if ( !defined $text || $code eq "000" );
  310. return ( $code, "text/plain", $text . "\r\n" );
  311. }
  312. sub parse_querystring {
  313. my($in) = @_;
  314. my %out;
  315. if ( defined $in && length $in ) {
  316. foreach(split(/&/,$in)) {
  317. my($name,$value) = split(/=/);
  318. $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  319. # handle arrays
  320. if ( exists $out{$name} ) {
  321. if ( ! ref $out{$name} ) {
  322. my $old = $out{$name};
  323. $out{$name} = [ $old ];
  324. }
  325. push @{$out{$name}}, $value;
  326. }
  327. else {
  328. $out{$name}=$value;
  329. }
  330. }
  331. }
  332. return \%out;
  333. }
  334. sub parse_post {
  335. my($fd, $len, $ct) = @_;
  336. my $data = '';
  337. while ( $len > 0 ) {
  338. my $buf;
  339. my $read = read($fd, $buf, $len);
  340. $len -= $read;
  341. $data .= $buf;
  342. }
  343. if ( $ct eq 'application/json' ) {
  344. my $tmp = from_json($data);
  345. $data = $tmp;
  346. }
  347. return { ctype => $ct, len => $len, data => $data};
  348. }
  349. sub sqlconnect {
  350. my($sql) = @_;
  351. my $dsn = "DBI:mysql:database=$sql->{base};host=$sql->{host}";
  352. my $dbh = DBI->connect($dsn, $sql->{user}, $sql->{pass}) || \\
  353. fatal_api_error(500,"Failed to connect to database");
  354. return $dbh;
  355. }
  356. sub sqlquery {
  357. my $dbh = shift;
  358. my $query = shift;
  359. my @args = @_;
  360. my $sth = $dbh->prepare($query) || fatal_api_error(500,"Failed to execute SQL query");
  361. $sth->execute(@args) || fatal_api_error(500,"Failed to execute SQL query");
  362. return $sth;
  363. }