An unfinished system to manage all your paper documentation in an easy way.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

autodoc.fcgi 10KB

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