AizUploadController.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\Upload;
  5. use Response;
  6. use Auth;
  7. use Storage;
  8. use Image;
  9. class AizUploadController extends Controller
  10. {
  11. public function index(Request $request){
  12. $all_uploads = (auth()->user()->user_type == 'seller') ? Upload::where('user_id',auth()->user()->id) : Upload::query();
  13. $search = null;
  14. $sort_by = null;
  15. if ($request->search != null) {
  16. $search = $request->search;
  17. $all_uploads->where('file_original_name', 'like', '%'.$request->search.'%');
  18. }
  19. $sort_by = $request->sort;
  20. switch ($request->sort) {
  21. case 'newest':
  22. $all_uploads->orderBy('created_at', 'desc');
  23. break;
  24. case 'oldest':
  25. $all_uploads->orderBy('created_at', 'asc');
  26. break;
  27. case 'smallest':
  28. $all_uploads->orderBy('file_size', 'asc');
  29. break;
  30. case 'largest':
  31. $all_uploads->orderBy('file_size', 'desc');
  32. break;
  33. default:
  34. $all_uploads->orderBy('created_at', 'desc');
  35. break;
  36. }
  37. $all_uploads = $all_uploads->paginate(60)->appends(request()->query());
  38. return (auth()->user()->user_type == 'seller')
  39. ? view('seller.uploads.index', compact('all_uploads', 'search', 'sort_by'))
  40. : view('backend.uploaded_files.index', compact('all_uploads', 'search', 'sort_by'));
  41. }
  42. public function create(){
  43. return (auth()->user()->user_type == 'seller')
  44. ? view('seller.uploads.create')
  45. : view('backend.uploaded_files.create');
  46. }
  47. public function show_uploader(Request $request){
  48. return view('uploader.aiz-uploader');
  49. }
  50. public function upload(Request $request){
  51. $type = array(
  52. "jpg"=>"image",
  53. "jpeg"=>"image",
  54. "png"=>"image",
  55. "svg"=>"image",
  56. "webp"=>"image",
  57. "gif"=>"image",
  58. "mp4"=>"video",
  59. "mpg"=>"video",
  60. "mpeg"=>"video",
  61. "webm"=>"video",
  62. "ogg"=>"video",
  63. "avi"=>"video",
  64. "mov"=>"video",
  65. "flv"=>"video",
  66. "swf"=>"video",
  67. "mkv"=>"video",
  68. "wmv"=>"video",
  69. "wma"=>"audio",
  70. "aac"=>"audio",
  71. "wav"=>"audio",
  72. "mp3"=>"audio",
  73. "zip"=>"archive",
  74. "rar"=>"archive",
  75. "7z"=>"archive",
  76. "doc"=>"document",
  77. "txt"=>"document",
  78. "docx"=>"document",
  79. "pdf"=>"document",
  80. "csv"=>"document",
  81. "xml"=>"document",
  82. "ods"=>"document",
  83. "xlr"=>"document",
  84. "xls"=>"document",
  85. "xlsx"=>"document"
  86. );
  87. if($request->hasFile('aiz_file')){
  88. $upload = new Upload;
  89. $extension = strtolower($request->file('aiz_file')->getClientOriginalExtension());
  90. if(isset($type[$extension])){
  91. $upload->file_original_name = null;
  92. $arr = explode('.', $request->file('aiz_file')->getClientOriginalName());
  93. for($i=0; $i < count($arr)-1; $i++){
  94. if($i == 0){
  95. $upload->file_original_name .= $arr[$i];
  96. }
  97. else{
  98. $upload->file_original_name .= ".".$arr[$i];
  99. }
  100. }
  101. $path = $request->file('aiz_file')->store('uploads/all', 'local');
  102. $size = $request->file('aiz_file')->getSize();
  103. // echo 1111;exit;
  104. // Return MIME type ala mimetype extension
  105. $finfo = finfo_open(FILEINFO_MIME_TYPE);
  106. // Get the MIME type of the file
  107. $file_mime = finfo_file($finfo, base_path('public/').$path);
  108. if($type[$extension] == 'image' && get_setting('disable_image_optimization') != 1){
  109. try {
  110. $img = Image::make($request->file('aiz_file')->getRealPath())->encode();
  111. $height = $img->height();
  112. $width = $img->width();
  113. if($width > $height && $width > 1500){
  114. $img->resize(1500, null, function ($constraint) {
  115. $constraint->aspectRatio();
  116. });
  117. }elseif ($height > 1500) {
  118. $img->resize(null, 800, function ($constraint) {
  119. $constraint->aspectRatio();
  120. });
  121. }
  122. $img->save(base_path('public/').$path);
  123. clearstatcache();
  124. $size = $img->filesize();
  125. } catch (\Exception $e) {
  126. //dd($e);
  127. }
  128. }
  129. if (env('FILESYSTEM_DRIVER') == 's3') {
  130. Storage::disk('s3')->put(
  131. $path,
  132. file_get_contents(base_path('public/').$path),
  133. [
  134. 'visibility' => 'public',
  135. 'ContentType' => $extension == 'svg' ? 'image/svg+xml' : $file_mime
  136. ]
  137. );
  138. if($arr[0] != 'updates') {
  139. unlink(base_path('public/').$path);
  140. }
  141. }
  142. $upload->extension = $extension;
  143. $upload->file_name = $path;
  144. $upload->user_id = Auth::check()?Auth::user()->id:0;
  145. $upload->type = $type[$upload->extension];
  146. $upload->file_size = $size;
  147. $upload->save();
  148. }
  149. return '{}';
  150. }
  151. }
  152. public function get_uploaded_files(Request $request)
  153. {
  154. $uploads = Upload::where('user_id', 0);
  155. if(Auth::check()){//用户未登录
  156. $uploads = Upload::where('user_id', Auth::user()->id);
  157. }
  158. if ($request->search != null) {
  159. $uploads->where('file_original_name', 'like', '%'.$request->search.'%');
  160. }
  161. if ($request->sort != null) {
  162. switch ($request->sort) {
  163. case 'newest':
  164. $uploads->orderBy('created_at', 'desc');
  165. break;
  166. case 'oldest':
  167. $uploads->orderBy('created_at', 'asc');
  168. break;
  169. case 'smallest':
  170. $uploads->orderBy('file_size', 'asc');
  171. break;
  172. case 'largest':
  173. $uploads->orderBy('file_size', 'desc');
  174. break;
  175. default:
  176. $uploads->orderBy('created_at', 'desc');
  177. break;
  178. }
  179. }
  180. return $uploads->paginate(60)->appends(request()->query());
  181. }
  182. public function destroy(Request $request,$id)
  183. {
  184. $upload = Upload::findOrFail($id);
  185. if(auth()->user()->user_type == 'seller' && $upload->user_id != auth()->user()->id){
  186. flash(translate("You don't have permission for deleting this!"))->error();
  187. return back();
  188. }
  189. try{
  190. if(env('FILESYSTEM_DRIVER') == 's3'){
  191. Storage::disk('s3')->delete($upload->file_name);
  192. if (file_exists(public_path().'/'.$upload->file_name)) {
  193. unlink(public_path().'/'.$upload->file_name);
  194. }
  195. }
  196. else{
  197. unlink(public_path().'/'.$upload->file_name);
  198. }
  199. $upload->delete();
  200. flash(translate('File deleted successfully'))->success();
  201. }
  202. catch(\Exception $e){
  203. $upload->delete();
  204. flash(translate('File deleted successfully'))->success();
  205. }
  206. return back();
  207. }
  208. public function get_preview_files(Request $request){
  209. $ids = explode(',', $request->ids);
  210. $files = Upload::whereIn('id', $ids)->get();
  211. $new_file_array = [];
  212. foreach($files as $file){
  213. $file['file_name'] = my_asset($file->file_name);
  214. if($file->external_link) {
  215. $file['file_name'] = $file->external_link;
  216. }
  217. $new_file_array[] = $file;
  218. }
  219. // dd($new_file_array);
  220. return $new_file_array;
  221. // return $files;
  222. }
  223. //Download project attachment
  224. public function attachment_download($id)
  225. {
  226. $project_attachment = Upload::find($id);
  227. try{
  228. $file_path = public_path($project_attachment->file_name);
  229. return Response::download($file_path);
  230. }catch(\Exception $e){
  231. flash(translate('File does not exist!'))->error();
  232. return back();
  233. }
  234. }
  235. //Download project attachment
  236. public function file_info(Request $request)
  237. {
  238. $file = Upload::findOrFail($request['id']);
  239. return (auth()->user()->user_type == 'seller')
  240. ? view('seller.uploads.info',compact('file'))
  241. : view('backend.uploaded_files.info',compact('file'));
  242. }
  243. }