zoukankan      html  css  js  c++  java
  • laravel 文件上传

    /**
         * 储存上传文件.
         *
         * @param hiyiPlusHttpRequestsAPI2StoreUploadFile $request
         * @param IlluminateContractsRoutingResponseFactory $response
         * @param CarbonCarbon $dateTime
         * @param hiyiPlusModelsFile $fileModel
         * @param hiyiPlusModelsFileWith $fileWith
         * @return mixed
         * @author Seven Du <shiweidu@outlook.com>
         */
        public function store(StoreUploadFileRequest $request, ResponseContract $response, Carbon $dateTime, FileModel $fileModel, FileWithModel $fileWith)
        {
    
            $clientHeight = $request->input('height', 0);
            $clientWidth = $request->input('width', 0);
    
            //判断文件hash(是否上传过)
            $fileModel = $this->validateFileInDatabase($fileModel, $file = $request->file('file'), function (UploadedFile $file, string $md5) use ($fileModel, $dateTime, $clientWidth, $clientHeight, $response): FileModel {
    
                // 图片做旋转处理
                if (! in_array($file->getClientMimeType(), ['video/mp4', 'image/gif'])) {
                    ini_set('memory_limit', '-1');
                    Image::make($file->getRealPath())->orientate()->save($file->getRealPath(), 100);
                }
    
                list($width, $height) = ($imageInfo = @getimagesize($file->getRealPath())) === false ? [null, null] : $imageInfo;
    
                //按照日期生成路径
                $path = $dateTime->format('Y/m/d/Hi');
                //去除本地上传
    //            if (($filename = $file->store($path, config('cdn.generators.filesystem.disk'))) === false) {
    //                return $response->json(['message' => '上传失败'], 500);
    //            }
    
                //oss上传开始
                $files=new FilesImagesController();
                //生成文件路径
                $oss_path = $path.'/'.$file->hashName();
                $result=$files->uploadsOss($file->getRealPath(), $oss_path);
                //oss上传结束
    
                //保存file表
                $fileModel->filename = $oss_path;
                $fileModel->hash = $md5;
                $fileModel->origin_filename = $file->getClientOriginalName();
                $fileModel->mime = $file->getClientMimeType();
                $fileModel->width = $width ?? $clientWidth;
                $fileModel->height = $height ?? $clientHeight;
                $fileModel->saveOrFail();
    
                return $fileModel;
            });
            //保存files_with表
            $fileWith = $this->resolveFileWith($fileWith, $request->user(), $fileModel);
    
            //log::info('store_id'.$fileWith->id);
            return $response->json(['message' => ['上传成功'], 'id' => $fileWith->id,])->setStatusCode(201);
        }
    
        /**
         * Get or create a uploaded file with id.
         *
         * @param IlluminateHttpRequest $request
         * @param IlluminateContractsRoutingResponseFactory $response
         * @param hiyiPlusModelsFile $file
         * @param hiyiPlusModelsFileWith $fileWith
         * @param string $hash
         * @return mixed
         * @author Seven Du <shiweidu@outlook.com>
         */
        public function uploaded(Request $request, ResponseContract $response, FileModel $file, FileWithModel $fileWith, string $hash)
        {
            $file = $file->where('hash', strtolower($hash))->firstOr(function () {
                abort(406);
            });
    
            // 复用空类型数据~减少资源浪费.
            $fileWith = $this->resolveFileWith($fileWith, $request->user(), $file);
    
            return $response->json([
                'message' => ['success'],
                'id' => $fileWith->id,
            ])->setStatusCode(200);
        }
    
        /**
         * Validate and return the file database model instance.
         *
         * @param hiyiPlusModelsFile $fileModel
         * @param IlluminateHttpUploadedFile $file
         * @param callable $call
         * @return hiyiPlusModelsFile
         * @author Seven Du <shiweidu@outlook.com>
         */
        public function validateFileInDatabase(FileModel $fileModel, UploadedFile $file, callable $call): FileModel
        {
            $hash = md5_file($file->getRealPath());
    
            return $fileModel->where('hash', $hash)->firstOr(function () use ($file, $call, $hash): FileModel {
                return call_user_func_array($call, [$file, $hash]);
            });
        }
    
        /**
         * 解决数据模型非实例.
         *
         * @param hiyiPlusModelsFileWith $fileWith
         * @param hiyiPlusModelsUser $user
         * @param hiyiPlusModelsFile $file
         * @return hiyiPlusModelsFileWith
         * @author Seven Du <shiweidu@outlook.com>
         */
        public function resolveFileWith(FileWithModel $fileWith, UserModel $user, FileModel $file): FileWithModel
        {
            $fileWith->file_id = $file->id;
            $fileWith->channel = null;
            $fileWith->raw = null;
            $fileWith->size = ($size = sprintf('%sx%s', $file->width, $file->height)) === 'x' ? null : $size;
            $user->files()->save($fileWith);
    
            return $fileWith;
        }

     

     https://www.cnblogs.com/loveyoume/p/6099966.html 

    简单理解call_user_func和call_user_func_array两个函数

  • 相关阅读:
    How to build Linux system from kernel to UI layer
    Writing USB driver for Android
    Xposed Framework for Android 8.x Oreo is released (in beta)
    Linux Smartphone Operating Systems You Can Install Today
    Librem 5 Leads New Wave of Open Source Mobile Linux Contenders
    GUADEC: porting GNOME to Android
    Librem 5 – A Security and Privacy Focused Phone
    GNOME and KDE Join Librem 5 Linux Smartphone Party
    Purism计划推出安全开源的Linux Librem 5智能手机
    国产系统之殇:你知道的这些系统都是国外的
  • 原文地址:https://www.cnblogs.com/sgm4231/p/10876698.html
Copyright © 2011-2022 走看看