AttachmentController.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. $file = $request->file('file');
  69. $bucket = config('mint.attachments.bucket_name.permanent');
  70. $fileId = Str::uuid();
  71. $ext = $file->getClientOriginalExtension();
  72. if($request->get('type') === 'avatar'){
  73. $resize = Image::make($file)->fit(512);
  74. Storage::put($bucket.'/'.$fileId.'.jpg',$resize->stream());
  75. $resize = Image::make($file)->fit(256);
  76. Storage::put($bucket.'/'.$fileId.'_m.jpg',$resize->stream());
  77. $resize = Image::make($file)->fit(128);
  78. Storage::put($bucket.'/'.$fileId.'_s.jpg',$resize->stream());
  79. $name = $fileId.'.jpg';
  80. }else{
  81. //Move Uploaded File
  82. $name = $fileId.'.'.$ext;
  83. $filename = $file->storeAs($bucket,$name);
  84. }
  85. $attachment = new Attachment;
  86. $attachment->id = $fileId;
  87. $attachment->user_uid = $user['user_uid'];
  88. $attachment->bucket = $bucket;
  89. $attachment->name = $name;
  90. $attachment->filename = $file->getClientOriginalName();
  91. $path_parts = pathinfo($file->getClientOriginalName());
  92. $attachment->title = $path_parts['filename'];
  93. $attachment->size = $file->getSize();
  94. $attachment->content_type = $file->getMimeType();
  95. $attachment->status = 'public';
  96. if($request->has('studio')){
  97. $owner_uid = StudioApi::getIdByName($request->get('studio'));
  98. }else{
  99. $owner_uid = $user['user_uid'];
  100. }
  101. if($owner_uid){
  102. $attachment->owner_uid = $owner_uid;
  103. }
  104. $attachment->save();
  105. $type = explode('/',$file->getMimeType());
  106. switch ($type[0]) {
  107. case 'image':
  108. /*
  109. $resize = Image::make($file)->fit(128);
  110. Storage::disk('public')->put($bucket.'/'.$fileId.'_s.jpg',$resize->stream());
  111. $resize = Image::make($file)->fit(256);
  112. Storage::disk('public')->put($bucket.'/'.$fileId.'_m.jpg',$resize->stream());
  113. $resize = Image::make($file)->fit(512);
  114. Storage::disk('public')->put($bucket.'/'.$fileId.'_l.jpg',$resize->stream());
  115. */
  116. break;
  117. case 'video':
  118. //$path = public_path($filename);
  119. //$ffmpeg = FFMpeg::create();
  120. //$video = $ffmpeg->open(public_path($filename));
  121. //$frame = $video->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(1));
  122. //$frame->save('image.jpg');
  123. break;
  124. default:
  125. # code...
  126. break;
  127. }
  128. /*
  129. $json = array(
  130. 'name' => $filename,
  131. 'size' => $file->getSize(),
  132. 'type' => $file->getMimeType(),
  133. 'url' => Storage::url($bucket.'/'.$name),
  134. 'uid' => $attachment->id,
  135. );*/
  136. return $this->ok(new AttachmentResource($attachment));
  137. }
  138. /**
  139. * Display the specified resource.
  140. *
  141. * @param \App\Models\Attachment $attachment
  142. * @return \Illuminate\Http\Response
  143. */
  144. public function show(Attachment $attachment)
  145. {
  146. //
  147. return $this->ok(new AttachmentResource($attachment));
  148. }
  149. /**
  150. * Update the specified resource in storage.
  151. *
  152. * @param \Illuminate\Http\Request $request
  153. * @param \App\Models\Attachment $attachment
  154. * @return \Illuminate\Http\Response
  155. */
  156. public function update(Request $request, Attachment $attachment)
  157. {
  158. //
  159. $user = AuthApi::current($request);
  160. if(!$user){
  161. return $this->error(__('auth.failed'),401,401);
  162. }
  163. $attachment->title = $request->get('title');
  164. $attachment->save();
  165. return $this->ok(new AttachmentResource($attachment));
  166. }
  167. /**
  168. * Remove the specified resource from storage.
  169. *
  170. * @param string $id
  171. * @return \Illuminate\Http\Response
  172. */
  173. public function destroy(Request $request,string $id)
  174. {
  175. //
  176. $user = AuthApi::current($request);
  177. if(!$user){
  178. return $this->error(__('auth.failed'),401,401);
  179. }
  180. if(Str::isUuid($id)){
  181. $res = Attachment::where('id',$id)->first();
  182. }else{
  183. /**
  184. * 从文件名获取bucket和name
  185. */
  186. $pos = mb_strrpos($request->get('name'),'/',0,"UTF-8");
  187. if($pos === false){
  188. return $this->error('无效的文件名',500,500);
  189. }
  190. $bucket = mb_substr($request->get('name'),0,$pos,'UTF-8');
  191. $name = mb_substr($request->get('name'),$pos+1,NULL,'UTF-8');
  192. $res = Attachment::where('bucket',$bucket)
  193. ->where('name',$name)
  194. ->first();
  195. }
  196. if(!$res){
  197. return $this->error('no res');
  198. }
  199. if($user['user_uid'] !== $res->user_uid){
  200. return $this->error(__('auth.failed'),403,403);
  201. }
  202. //删除文件
  203. $filename = $res->bucket . '/' . $res->name;
  204. $path_parts = pathinfo($res->name);
  205. Storage::delete($filename);
  206. Storage::delete($res->bucket.'/'.$path_parts['filename'].'_m.jpg');
  207. Storage::delete($res->bucket.'/'.$path_parts['filename'].'_s.jpg');
  208. $del = $res->delete();
  209. return $this->ok($del);
  210. }
  211. }