/application/controllers/api/gift.php

https://github.com/pinhao/giftweb · PHP · 171 lines · 141 code · 17 blank · 13 comment · 24 complexity · ac390009fd872ce74f3dabd2fb956919 MD5 · raw file

  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3. /**
  4. *
  5. * GIFT
  6. *
  7. * Class for raw interaction with gift server
  8. *
  9. */
  10. require APPPATH.'/libraries/REST_Controller.php';
  11. require APPPATH.'/libraries/GIFTLib.php';
  12. class gift extends REST_Controller {
  13. private $GIFTLib;
  14. private $rootWebPath;
  15. function __construct() {
  16. parent::__construct();
  17. $this->config->load('gift');
  18. $host = $this->config->item('gift_host');
  19. $port = $this->config->item('gift_port');
  20. $this->GIFTLib = new GIFTLib($host, $port);
  21. $this->rootWebPath = rtrim(realpath('.'), '/').'/';
  22. }
  23. /** api web methods **/
  24. /* function to test connectivity */
  25. function ping_get() {
  26. $this->response(array('ping' => 'pong'), 200);
  27. }
  28. function collections_get() {
  29. $this->validateSessionId();
  30. $collections = $this->GIFTLib->getCollections();
  31. if ( $collections === FALSE ) {
  32. $this->errorResponse($this->GIFTLib->getLastErrors());
  33. }
  34. $response = $this->build_response('collection', $collections);
  35. $this->response($response, 200);
  36. }
  37. function algorithms_get() {
  38. $this->validateSessionId();
  39. $algorithms = $this->GIFTLib->getAlgorithms();
  40. if ( $algorithms === FALSE ) {
  41. $this->errorResponse($this->GIFTLib->getLastErrors());
  42. }
  43. $response = $this->build_response('algorithm', $algorithms);
  44. $this->response($response, 200);
  45. }
  46. function random_images_get() {
  47. $this->validateSessionId();
  48. $collections = $this->GIFTLib->getCollections();
  49. if ( $collections === FALSE ) {
  50. $this->errorResponse($this->GIFTLib->getLastErrors());
  51. }
  52. $collectionRandKey = array_rand($collections);
  53. if ( $collectionRandKey === NULL ) {
  54. $this->errorResponse(array(array('msg'=>'No collections on server')));
  55. }
  56. if ( ($count = $this->input->get('count')) !== FALSE ) {
  57. $images = $this->GIFTLib->getImageSet($collections[$collectionRandKey], NULL, NULL, intval($count));
  58. } else {
  59. $images = $this->GIFTLib->getImageSet($collections[$collectionRandKey]);
  60. }
  61. if ( $images === FALSE ) {
  62. $this->errorResponse($this->GIFTLib->getLastErrors());
  63. }
  64. $images = $this->translate_path_to_url($images);
  65. $response = $this->build_response('image', $images);
  66. $this->response($response, 200);
  67. }
  68. function similar_images_post() {
  69. $uploadConfig = array();
  70. $uploadConfig['upload_path'] = $this->rootWebPath.'static/uploads/';
  71. $uploadConfig['allowed_types'] = 'jpg|jpeg';
  72. $uploadConfig['encrypt_name'] = TRUE;
  73. $this->load->library('upload', $uploadConfig);
  74. if ( $this->upload->do_upload() === FALSE ) {
  75. $this->errorResponse(array(array('msg'=>$this->upload->display_errors('',''))));
  76. }
  77. // upload file comes in userfile field encoded in multipart/form-data
  78. $uploadData = $this->upload->data();
  79. if ( empty($uploadData) ) {
  80. $this->errorResponse(array(array('msg'=>'File upload error')));
  81. }
  82. if ( $uploadData['file_type'] != 'image/jpeg' || $uploadData['is_image'] === 0 ) {
  83. $this->errorResponse(array(array('msg'=>'Image File invalid')));
  84. if ( is_file($uploadData['full_path']) === TRUE ) {
  85. unlink($uploadData['full_path']);
  86. }
  87. }
  88. $this->validateSessionId();
  89. $collections = $this->GIFTLib->getCollections();
  90. if ( $collections === FALSE ) {
  91. $this->errorResponse($this->GIFTLib->getLastErrors());
  92. }
  93. if ( empty($collections) ) {
  94. $this->errorResponse(array(array('msg'=>'No collections on server')));
  95. }
  96. // TODO:in which collection should the query run??
  97. // HARDCODED to first collection
  98. if ( ($count = $this->input->post('count')) !== FALSE ) {
  99. $images = $this->GIFTLib->getImageSet($collections[0], NULL, $uploadData['full_path'], intval($count));
  100. } else {
  101. $images = $this->GIFTLib->getImageSet($collections[0], NULL, $uploadData['full_path']);
  102. }
  103. if ( $images === FALSE ) {
  104. $this->errorResponse($this->GIFTLib->getLastErrors());
  105. }
  106. $images = $this->translate_path_to_url($images);
  107. $response = $this->build_response('image', $images);
  108. $this->response($response, 200);
  109. }
  110. function sessionid_get() {
  111. $sessionId = $this->GIFTLib->getSessionId();
  112. if ( $sessionId === FALSE ) {
  113. $this->errorResponse($this->GIFTLib->getLastErrors());
  114. }
  115. $response = $this->build_response(NULL, NULL);
  116. $this->response($response, 200);
  117. }
  118. /** internal methods **/
  119. private function translate_path_to_url($images) {
  120. $baseurl = $this->config->item('base_url');
  121. $filePathPatern = "#^(file:)?$this->rootWebPath#";
  122. foreach($images as $k => $image) {
  123. $images[$k]['image-location'] = preg_replace($filePathPatern, $baseurl, $image['image-location']);
  124. $images[$k]['thumbnail-location'] = preg_replace($filePathPatern, $baseurl, $image['thumbnail-location']);
  125. }
  126. return $images;
  127. }
  128. private function errorResponse( $msgs = NULL, $code = 400) {
  129. $this->response($this->build_response('error', $msgs), $code);
  130. }
  131. private function validateSessionId() {
  132. if ( ($sid = $this->input->get_post('session-id')) !== FALSE) {
  133. if ( $this->GIFTLib->setSessionId($sid) === FALSE ) {
  134. $this->errorResponse($this->GIFTLib->getLastErrors());
  135. }
  136. }
  137. }
  138. private function build_response($type, $items) {
  139. $response = array();
  140. $response['response'] = array();
  141. if ( ($sessionId = $this->GIFTLib->getSessionId()) !== FALSE ) {
  142. $response['response']['session-id'] = $sessionId;
  143. }
  144. if ( !empty($type) ) {
  145. $response['response']['type'] = $type;
  146. if ( empty($items) ) {
  147. $item = array();
  148. }
  149. $response['response']['count'] = count($items);
  150. $response['response']['items'] = $items;
  151. }
  152. return $response;
  153. }
  154. }