2
0

AttachmentController.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\File;
  4. use Illuminate\Support\Facades\Storage;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Str;
  7. use Illuminate\Support\Facades\App;
  8. use App\Http\Api\AuthApi;
  9. use App\Http\Api\StudioApi;
  10. use App\Http\Resources\AttachmentResource;
  11. use App\Models\Attachment;
  12. use Intervention\Image\ImageManagerStatic as Image;
  13. use FFMpeg\FFMpeg;
  14. class AttachmentController extends Controller
  15. {
  16. /**
  17. * Display a listing of the resource.
  18. *
  19. * @return \Illuminate\Http\Response
  20. */
  21. public function index(Request $request)
  22. {
  23. //
  24. switch ($request->input('view')) {
  25. case 'studio':
  26. $user = AuthApi::current($request);
  27. if (!$user) {
  28. return $this->error(__('auth.failed'));
  29. }
  30. //判断当前用户是否有指定的studio的权限
  31. if ($user['user_uid'] !== StudioApi::getIdByName($request->input('studio'))) {
  32. return $this->error(__('auth.failed'));
  33. }
  34. $table = Attachment::where('owner_uid', $user["user_uid"]);
  35. break;
  36. default:
  37. return $this->error("error view", [], 200);
  38. break;
  39. }
  40. if ($request->has('search')) {
  41. $table = $table->where('title', 'like', $request->input('search') . "%");
  42. }
  43. if ($request->has('content_type')) {
  44. $table = $table->where('content_type', 'like', $request->input('content_type') . "%");
  45. }
  46. $count = $table->count();
  47. $table = $table->orderBy(
  48. $request->input('order', 'updated_at'),
  49. $request->input('dir', 'desc')
  50. );
  51. $table = $table->skip($request->input('offset', 0))
  52. ->take($request->input('limit', 1000));
  53. $result = $table->get();
  54. return $this->ok(["rows" => AttachmentResource::collection($result), "count" => $count]);
  55. }
  56. /**
  57. * Store a newly created resource in storage.
  58. *
  59. * @param \Illuminate\Http\Request $request
  60. * @return \Illuminate\Http\Response
  61. */
  62. public function store(Request $request)
  63. {
  64. $user = AuthApi::current($request);
  65. if (!$user) {
  66. return $this->error(__('auth.failed'), 401, 401);
  67. }
  68. $request->validate([
  69. 'file' => 'required',
  70. ]);
  71. $isCreate = true;
  72. if (Str::isUuid($request->input('id'))) {
  73. $attachment = Attachment::find($request->input('id'));
  74. if (!$attachment) {
  75. return $this->error('no res');
  76. }
  77. $fileId = $attachment->id;
  78. $isCreate = false;
  79. } else {
  80. $fileId = Str::uuid();
  81. }
  82. $file = $request->file('file');
  83. $bucket = config('mint.attachments.bucket_name.permanent');
  84. $ext = $file->getClientOriginalExtension();
  85. if ($request->input('type') === 'avatar') {
  86. $resize = Image::make($file)->fit(512);
  87. Storage::put($bucket . '/' . $fileId . '.jpg', $resize->stream());
  88. $resize = Image::make($file)->fit(256);
  89. Storage::put($bucket . '/' . $fileId . '_m.jpg', $resize->stream());
  90. $resize = Image::make($file)->fit(128);
  91. Storage::put($bucket . '/' . $fileId . '_s.jpg', $resize->stream());
  92. $name = $fileId . '.jpg';
  93. } else {
  94. //Move Uploaded File
  95. $name = $fileId . '.' . $ext;
  96. if (!$isCreate) {
  97. //替换模式,先删除旧文件
  98. Storage::delete($bucket . '/' . $name);
  99. }
  100. $filename = $file->storeAs($bucket, $name);
  101. }
  102. if ($isCreate) {
  103. $attachment = new Attachment;
  104. $attachment->id = $fileId;
  105. $attachment->bucket = $bucket;
  106. if ($request->has('studio')) {
  107. $owner_uid = StudioApi::getIdByName($request->input('studio'));
  108. } else {
  109. $owner_uid = $user['user_uid'];
  110. }
  111. if ($owner_uid) {
  112. $attachment->owner_uid = $owner_uid;
  113. }
  114. $attachment->status = 'public';
  115. $path_parts = pathinfo($file->getClientOriginalName());
  116. $attachment->title = $path_parts['filename'];
  117. }
  118. $attachment->user_uid = $user['user_uid'];
  119. $attachment->name = $name;
  120. $attachment->filename = $file->getClientOriginalName();
  121. $attachment->size = $file->getSize();
  122. $attachment->content_type = $file->getMimeType();
  123. $attachment->save();
  124. $type = explode('/', $file->getMimeType());
  125. switch ($type[0]) {
  126. case 'image':
  127. $thumbnail = Image::make($file);
  128. break;
  129. case 'video':
  130. $tmpFile = $file->storeAs($bucket, $name, 'local');
  131. $path = storage_path('app/' . $tmpFile);
  132. if (App::environment('local')) {
  133. $ffmpeg = FFMpeg::create();
  134. } else {
  135. $ffmpeg = FFMpeg::create(array(
  136. 'ffmpeg.binaries' => '/usr/bin/ffmpeg',
  137. 'ffprobe.binaries' => '/usr/bin/ffprobe',
  138. 'timeout' => 3600,
  139. 'ffmpeg.threads' => 1,
  140. ));
  141. }
  142. $video = $ffmpeg->open($path);
  143. $frame = $video->frame(\FFMpeg\Coordinate\TimeCode::fromSeconds(1));
  144. $screenShot = storage_path("app/tmp/{$fileId}.jpg");
  145. $frame->save($screenShot);
  146. $thumbnail = Image::make($screenShot);
  147. break;
  148. default:
  149. # code...
  150. break;
  151. }
  152. if (isset($thumbnail)) {
  153. //生成缩略图
  154. $thumbnail->resize(256, 256, function ($constraint) {
  155. $constraint->aspectRatio();
  156. });
  157. Storage::put($bucket . '/' . $fileId . '_m.jpg', $thumbnail->stream());
  158. $thumbnail->resize(128, 128, function ($constraint) {
  159. $constraint->aspectRatio();
  160. });
  161. Storage::put($bucket . '/' . $fileId . '_s.jpg', $thumbnail->stream());
  162. //销毁图片资源
  163. $thumbnail->destroy();
  164. }
  165. return $this->ok(new AttachmentResource($attachment));
  166. }
  167. /**
  168. * Display the specified resource.
  169. *
  170. * @param \App\Models\Attachment $attachment
  171. * @return \Illuminate\Http\Response
  172. */
  173. public function show(Attachment $attachment)
  174. {
  175. //
  176. return $this->ok(new AttachmentResource($attachment));
  177. }
  178. /**
  179. * Update the specified resource in storage.
  180. *
  181. * @param \Illuminate\Http\Request $request
  182. * @param \App\Models\Attachment $attachment
  183. * @return \Illuminate\Http\Response
  184. */
  185. public function update(Request $request, Attachment $attachment)
  186. {
  187. //
  188. $user = AuthApi::current($request);
  189. if (!$user) {
  190. return $this->error(__('auth.failed'), 401, 401);
  191. }
  192. $attachment->title = $request->input('title');
  193. $attachment->save();
  194. return $this->ok(new AttachmentResource($attachment));
  195. }
  196. /**
  197. * Remove the specified resource from storage.
  198. *
  199. * @param string $id
  200. * @return \Illuminate\Http\Response
  201. */
  202. public function destroy(Request $request, string $id)
  203. {
  204. //
  205. $user = AuthApi::current($request);
  206. if (!$user) {
  207. return $this->error(__('auth.failed'), 401, 401);
  208. }
  209. if (Str::isUuid($id)) {
  210. $res = Attachment::where('id', $id)->first();
  211. } else {
  212. /**
  213. * 从文件名获取bucket和name
  214. */
  215. $pos = mb_strrpos($request->input('name'), '/', 0, "UTF-8");
  216. if ($pos === false) {
  217. return $this->error('无效的文件名', 500, 500);
  218. }
  219. $bucket = mb_substr($request->input('name'), 0, $pos, 'UTF-8');
  220. $name = mb_substr($request->input('name'), $pos + 1, NULL, 'UTF-8');
  221. $res = Attachment::where('bucket', $bucket)
  222. ->where('name', $name)
  223. ->first();
  224. }
  225. if (!$res) {
  226. return $this->error('no res');
  227. }
  228. if ($user['user_uid'] !== $res->user_uid) {
  229. return $this->error(__('auth.failed'), 403, 403);
  230. }
  231. //删除文件
  232. $filename = $res->bucket . '/' . $res->name;
  233. $path_parts = pathinfo($res->name);
  234. Storage::delete($filename);
  235. Storage::delete($res->bucket . '/' . $path_parts['filename'] . '_m.jpg');
  236. Storage::delete($res->bucket . '/' . $path_parts['filename'] . '_s.jpg');
  237. $del = $res->delete();
  238. return $this->ok($del);
  239. }
  240. }