Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加支持自定义CDN #61

Open
dafa168 opened this issue May 6, 2019 · 0 comments
Open

增加支持自定义CDN #61

dafa168 opened this issue May 6, 2019 · 0 comments

Comments

@dafa168
Copy link

dafa168 commented May 6, 2019

配置文件: 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;

}

}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant