laravel-u-editor icon indicating copy to clipboard operation
laravel-u-editor copied to clipboard

增加支持自定义CDN

Open dafa168 opened this issue 5 years ago • 0 comments

配置文件: config/UEditorUpload.php 'mode'=>'cdn',//上传方式,local 为本地 qiniu 为七牛 cdn 为自定义位置

具体实现文件: vendor/stevenyangecho/laravel-u-editor/src/Uploader/UploadFile.php `<?php namespace Stevenyangecho\UEditor\Uploader;

use Stevenyangecho\UEditor\Uploader\Upload;

/** * *

  • Class UploadFile

  • 文件/图像普通上传

  • @package Stevenyangecho\UEditor\Uploader */ class UploadFile extends Upload { use UploadQiniu;

    public function doUpload() {

     $file = $this->request->file($this->fileField);
     if (empty($file)) {
         $this->stateInfo = $this->getStateInfo("ERROR_FILE_NOT_FOUND");
         return false;
     }
     if (!$file->isValid()) {
         $this->stateInfo = $this->getStateInfo($file->getError());
         return false;
    
     }
    
     $this->file = $file;
    
     $this->oriName = $this->file->getClientOriginalName();
    
     $this->fileSize = $this->file->getSize();
     $this->fileType = $this->getFileExt();
    
     $this->fullName = $this->getFullName();
    
    
     $this->filePath = $this->getFilePath();
    
     $this->fileName = basename($this->filePath);
    
    
     //检查文件大小是否超出限制
     if (!$this->checkSize()) {
         $this->stateInfo = $this->getStateInfo("ERROR_SIZE_EXCEED");
         return false;
     }
     //检查是否不允许的文件格式
     if (!$this->checkType()) {
         $this->stateInfo = $this->getStateInfo("ERROR_TYPE_NOT_ALLOWED");
         return false;
     }
    
     if (config('UEditorUpload.core.mode') == 'local') {
         try {
             $this->file->move(dirname($this->filePath), $this->fileName);
    
             $this->stateInfo = $this->stateMap[0];
    
         } catch (FileException $exception) {
             $this->stateInfo = $this->getStateInfo("ERROR_WRITE_CONTENT");
             return false;
         }
    
     } else if (config('UEditorUpload.core.mode') == 'cdn') {
    
         $data = \App\Library\Tools\Upload::curlFile($file, 'img');
         info(__METHOD__ . json_encode($data, JSON_UNESCAPED_UNICODE));
         if ($data['code'] == 0) {
             $this->stateInfo = $this->getStateInfo("ERROR_UNKNOWN");
             return false;
         }
    
         //目前只是重置了fullName
         $this->fullName = $data['img'] ?? $this->getFullName();
         $this->stateInfo = $this->stateMap[0];
    
         //需要重置这些属性
    

// "state" => $this->stateInfo, // "url" => $this->fullName, // "title" => $this->fileName, // "original" => $this->oriName, // "type" => $this->fileType, // "size" => $this->fileSize

    } else if (config('UEditorUpload.core.mode') == 'qiniu') {

        $content = file_get_contents($this->file->getPathname());
        return $this->uploadQiniu($this->filePath, $content);

    } else {
        $this->stateInfo = $this->getStateInfo("ERROR_UNKNOWN_MODE");
        return false;
    }


    return true;

}

} `

dafa168 avatar May 06 '19 07:05 dafa168