reg2005/laravel-upload-manager

Upload, validate, storage, manage by API for Laravel 5.1/5.2/5.3/5.4

1.0.2 2017-01-22 21:11 UTC

This package is not auto-updated.

Last update: 2024-12-16 15:57:02 UTC


README

Upload, validate, storage, manage by API for Laravel 5.1/5.2

Requirement

  1. Laravel 5.1/5.2

Install

  1. composer require reg2005/laravel-upload-manager
  2. config/app.php
'providers' => [
    'reg2005\UploadManager\UploadManagerServiceProvider',
    Intervention\Image\ImageServiceProvider::class
]

...

'aliases' => [
    'Image' => Intervention\Image\Facades\Image::class
]
  1. php artisan vendor:publish --provider="reg2005\UploadManager\UploadManagerServiceProvider"
  2. php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravel5"
  3. php artisan migrate
  4. Done

Usage

  1. Upload and store a file.

     
        use reg2005\UploadManager\UploadManager;
        
        class UploadController extend Controller
        {
            public function postUpload(Request $request)
            {
                $file = $request->file('avatar');
                $manager = UploadManager::getInstance();
                $upload = $manager->upload($file);
                $upload->save();
                return $upload;
            }
        }
  2. Fetch and store a file from a URL

     
        use reg2005\UploadManager\UploadManager;
        
        class UploadController extend Controller
        {
            public function postUpload(Request $request)
            {
                $fileUrl = $request->input('url');
                $manager = UploadManager::getInstance();
                $upload = $manager->upload($fileUrl);
                $upload->save();
                return $upload;
            }
        }
  3. Update a upload object

     
        use App\Upload;
        use reg2005\UploadManager\UploadManager;
        
        class UploadController extend Controller
        {
            public function postUpload(Request $request)
            {
                $uploadId = $request->input('id');
                $file = $request->file('avatar');
                
                $manager = UploadManager::getInstance();
                $upload = Upload::find($uploadId);
                if($manager->update($upload, $file))
                {
                    $upload->save();
                    return $upload;
                }
                return ['result'=>false];
            }
        }
  4. Update a upload object from a URL

     
        use App\Upload;
        use reg2005\UploadManager\UploadManager;
        
        class UploadController extend Controller
        {
            public function postUpload(Request $request)
            {
                $uploadId = $request->input('id');
                $fileUrl = $request->input('url');
                
                $manager = UploadManager::getInstance();
                $upload = Upload::find($uploadId);
                if($manager->update($upload, $fileUrl))
                {
                    $upload->save();
                    return $upload;
                }
                return ['result'=>false];
            }
        }
  5. Validation

        use reg2005\UploadManager\UploadManager;
        
        class UploadController extend Controller
        {
            public function postUpload(Request $request)
            {
                $file = $request->file('avatar');
                $manager = UploadManager::getInstance();
                $upload = $manager->withValidator('image')->upload($file);    //加上验证组
                
                if($upload)
                {
                    $upload->save();
                    return $upload;
                }
                else
                {
                    $errorMessages = $manager->getErrors();                   //得到所有错误信息
                    $errorMessage = $manager->getFirstErrorMessage();         //得到第一条错误信息
                    throw new \Exception($errorMessage);
                }
            }
        }
  6. Disk

        use reg2005\UploadManager\UploadManager;
        
        class UploadController extend Controller
        {
            public function postUpload(Request $request)
            {
                $file = $request->file('avatar');
                $manager = UploadManager::getInstance();
                $upload = $manager
                    ->withValidator('image')
                    ->withDisk('selectel')         // 储存到七牛磁盘里
                    ->upload($file);
                $upload->save();
                return $upload;
            }
        }
  7. $upload

        use reg2005\UploadManager\UploadManager;
        
        class UploadController extend Controller
        {
            public function postUpload(Request $request)
            {
                $file = $request->file('avatar');
                $manager = UploadManager::getInstance();
                $upload = $manager
                    ->withValidator('image')
                    ->withDisk('localhost')
                    ->upload($file, function($upload){
                        if($upload->size > 1024*1024)
                        {
                            $upload->disk = 'selectel';
                        }
                        return $upload;
                    });
                $upload->save();
                return $upload;
            }
        }

Configuration

  1. config/upload.php

  2. App\Upload

  3. UploadStrategy.php

        
        <?php namespace App\Extensions;
        
        use reg2005\UploadManager\UploadStrategy as BaseUploadStrategy;
        use reg2005\UploadManager\UploadStrategyInterface;
        
        class UploadStrategy extends BaseUploadStrategy implements UploadStrategyInterface
        {
        
            /**
             * @param $filename
             * @return string
             */
            public function makeStorePath($filename)
            {
                $path = 'uploads/' . $filename;
                return $path;
            }
        
            /**
             * disk localuploads
             * @param $path
             * @return string
             */
            public function getLocaluploadsUrl($path)
            {
                $url = url('uploads/' . $path);
                return $url;
            }
        
            /**
             * disk selectel
             * @param $path
             * @return string
             */
            public function getselectelUrl($path)
            {
                $url = 'http://' . trim(\Config::get('filesystems.disks.selectel.domain'), '/') . '/' . trim($path, '/');
                return $url;
            }
        } 

    config/upload.php upload_strategy