PageRenderTime 21ms CodeModel.GetById 46ms RepoModel.GetById 1ms app.codeStats 0ms

/app/Services/FileServ.php

https://bitbucket.org/jasechen/ea-alfa-api
PHP | 286 lines | 138 code | 68 blank | 80 comment | 12 complexity | 1e816bc3078aa18861b8c9d9ffbee772 MD5 | raw file
  1. <?php
  2. namespace App\Services;
  3. use Carbon\Carbon;
  4. use Intervention\Image\ImageManagerStatic as Image;
  5. use Illuminate\Support\Collection;
  6. use Illuminate\Support\Facades\File;
  7. use Illuminate\Support\Facades\Storage;
  8. use App\Repositories\FileRepo;
  9. /**
  10. * Class FileServ
  11. *
  12. * @package namespace App\Services;
  13. */
  14. class FileServ
  15. {
  16. public function __construct()
  17. {
  18. $this->fileRepo = new FileRepo();
  19. } // END function
  20. public function moveFileTo($filename, $target, $source = '')
  21. {
  22. $fileDatum = $this->findByFilename($filename);
  23. if ($fileDatum->isEmpty()) {
  24. return false;
  25. } // END if
  26. $sizes = config('image.sizes');
  27. $sizes['o'] = 0;
  28. $fileParentType = $fileDatum->first()->parent_type;
  29. $fileExtension = $fileDatum->first()->extension;
  30. $source .= empty($source) ? '' : '/';
  31. foreach ($sizes as $id => $size) {
  32. $file = $filename . '_' . $id . '.' . $fileExtension;
  33. $exists = Storage::disk('gallery')->exists($source . $file);
  34. if (empty($exists)) {continue;}
  35. Storage::disk('gallery')->move($source . $file, $target . '/'. $file);
  36. } // END foreach
  37. return true;
  38. } // END function
  39. /**
  40. * Covert image
  41. *
  42. * @param $parentType
  43. * @param \Illuminate\Filesystem\Filesystem $file
  44. *
  45. * @throws
  46. * @return string $filename
  47. */
  48. public function convertImage($parentType, $file, $extension = null)
  49. {
  50. $sizes = config('image.sizes');
  51. $savePath = public_path('gallery/' . $parentType);
  52. $filename = Carbon::now()->timestamp . mt_rand(100000, 999999);
  53. $fileExtension = empty($extension) ? $file->extension() : $extension;
  54. $convertFile = file_get_contents($file);
  55. if ($convertFile === FALSE) {
  56. return false;
  57. } // END if
  58. $img = Image::make($convertFile);
  59. $filePath = $savePath . '/' . $filename . '_o.' . $fileExtension;
  60. $img->save($filePath);
  61. $imgWidth = $img->width();
  62. foreach ($sizes as $id => $size) {
  63. if ($imgWidth < $size['width']) {
  64. continue;
  65. } // END if
  66. if ($size['width'] == $size['height']) {
  67. $img->fit($size['width']);
  68. } else {
  69. $img->resize($size['width'], $size['height'], function ($constraint) {
  70. $constraint->aspectRatio();
  71. });
  72. } // END if else
  73. $filePath = $savePath . '/' . $filename . '_' . $id . '.' . $fileExtension;
  74. $img->save($filePath);
  75. } // END foreach
  76. return $filename;
  77. } // END function
  78. public function findLinks($filename)
  79. {
  80. $fileDatum = $this->findByFilename($filename);
  81. if ($fileDatum->isEmpty()) {
  82. return [];
  83. } // END if
  84. $links = [];
  85. $sizes = config('image.sizes');
  86. $sizes['o'] = 0;
  87. $parentType = $fileDatum->first()->parent_type;
  88. $parentId = intval($fileDatum->first()->parent_id);
  89. $filename = $fileDatum->first()->filename;
  90. $fileExtension = $fileDatum->first()->extension;
  91. $path = $parentType .'/';
  92. $path .= empty($parentId) ? '' : $parentId . '/';
  93. foreach ($sizes as $id => $size) {
  94. $file = $filename . '_' . $id . '.' . $fileExtension;
  95. $exists = Storage::disk('gallery')->exists($path . $file);
  96. if (empty($exists)) {continue;}
  97. $links[$id] = 'gallery/' . $path . $file;
  98. } // END foreach
  99. return $links;
  100. } // END function
  101. /*
  102. * delete
  103. *
  104. * @param $where
  105. *
  106. * @return
  107. */
  108. public function delete($where = [])
  109. {
  110. if (!array_key_exists('id', $where)) {
  111. return false;
  112. } // END if
  113. return $this->fileRepo->deleteData($where);
  114. } // END function
  115. /*
  116. * update
  117. *
  118. * @param $data
  119. * @param $where
  120. *
  121. * @return
  122. */
  123. public function update($data, $where = [])
  124. {
  125. if (!is_array($data) OR !array_key_exists('id', $where)) {
  126. return false;
  127. } // END if
  128. return $this->fileRepo->updateData($data, $where);
  129. } // END function
  130. /*
  131. * create
  132. *
  133. * @param $parentType
  134. * @param $parentId
  135. * @param $sessionId
  136. * @param $filename
  137. * @param $extension
  138. * @param $mimeType
  139. * @param $size
  140. * @param $type
  141. * @param $status
  142. *
  143. * @return
  144. */
  145. public function create($parentType, $parentId, $sessionId, $filename, $extension, $mimeType, $size, $type = 'image', $status = 'enable')
  146. {
  147. $data = ['parent_type' => $parentType,
  148. 'parent_id' => $parentId,
  149. 'session_id' => $sessionId,
  150. 'filename' => $filename,
  151. 'extension' => $extension,
  152. 'mime_type' => $mimeType,
  153. 'size' => $size,
  154. 'type' => $type,
  155. 'status' => $status
  156. ];
  157. return $this->fileRepo->createData($data);
  158. } // END function
  159. /*
  160. * findByParentTypeAndParentId
  161. *
  162. * @param $parentType
  163. * @param $parentId
  164. * @param $status
  165. * @param $orderby
  166. * @param $page
  167. * @param $numItems
  168. *
  169. * @return
  170. */
  171. public function findByParentTypeAndParentId($parentType, $parentId, $status = '', $orderby = [], $page = -1, $numItems = 10)
  172. {
  173. $where = ['parent_type' => $parentType, 'parent_id' => $parentId];
  174. if (!empty($status)) {
  175. $where['status'] = $status;
  176. } // END if
  177. return $this->fileRepo->fetchData($where, $orderby, $page, $numItems);
  178. } // END function
  179. /*
  180. * findByStatus
  181. *
  182. * @param $status
  183. * @param $orderby
  184. * @param $page
  185. * @param $numItems
  186. *
  187. * @return
  188. */
  189. public function findByStatus($status, $orderby = [], $page = -1, $numItems = 10)
  190. {
  191. $where = ['status' => $status];
  192. return $this->fileRepo->fetchData($where, $orderby, $page, $numItems);
  193. } // END function
  194. /*
  195. * findByFilename
  196. *
  197. * @param $filename
  198. *
  199. * @return
  200. */
  201. public function findByFilename($filename)
  202. {
  203. $where = ['filename' => $filename];
  204. return $this->fileRepo->fetchDatum($where);
  205. } // END function
  206. /*
  207. * findById
  208. *
  209. * @param $id
  210. *
  211. * @return
  212. */
  213. public function findById($id)
  214. {
  215. $where = ['id' => $id];
  216. return $this->fileRepo->fetchDatum($where);
  217. } // END function
  218. }