shopwwi / webman-filesystem

Flysystem V2/V3 adapter for the webman
22 stars 3 forks source link

关于 支持 base64Upload 的说明 #3

Closed ayhome closed 1 year ago

ayhome commented 1 year ago

原本我我也是白票的,有问题反馈也态度好点, 但好歹作者写东西认真点吧。 看了下 base64Upload 这个方法 `

public function base64Upload($baseImg)
{
    // $baseImg = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcIAAAHCCAYAAAB8GMlFAAAAAXNSR0IArs4c6QAAAARnQU1BAACx...";
    // if($size = getimagesize($baseImg)){
    //     throw new \Exception('图片格式错误');
    // }
    preg_match('/^(data:\s*image\/(\w+);base64,)/',$baseImg,$res);
    if(!$res){
       throw new \Exception('格式错误');
    }
    if(!empty($this->extYes) && !in_array($size['mime'],$this->extYes)) {
        throw new \Exception('不允许上传文件类型'.$size['mime']);
    }
    if(!empty($this->extNo) &&in_array($size['mime'],$this->extNo)) {
        throw new \Exception('文件类型不被允许'.$size['mime']);
    }
    $img = base64_decode(str_replace($res[1],'', $baseImg));
    $filesystem = FilesystemFactory::get($this->adapterType);
    $storageKey = md5(uniqid());
    $fileName = $this->path.'/'.$storageKey.'.'.$res[2];
    $base_img = str_replace($res[1], '', $baseImg);
    $base_img = str_replace('=','',$baseImg);
    $img_len = strlen($base_img);
    $file_size = intval($img_len - ($img_len/8)*2);

    if($file_size > $this->size){
        throw new \Exception("上传文件过大(当前大小 {$file_size},需小于 {$this->size})");
    }

    $this->put(
        $path = trim($fileName, '/'), $img
    );

    $info = [
        'origin_name' => $fileName,
        'file_name' => $fileName,
        'storage_key' => $storageKey,
        'file_url' => $this->url($fileName),
        'size' => $file_size,
        'mime_type' => $size['mime'],
        'extension' => $res[2],
        'file_height' => $size[1],
        'file_width' => $size[0]
    ];

    return \json_decode(\json_encode($info));
}

`

多少个错误的问题, 我都不好说了

phcent commented 1 year ago

确实是大意了 之前测试好了 结果正式文件忘记改了 导致出现这种情况 虚心向你道歉