AttachmentController.php 8.2 KB

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