<?php

namespace app\extra\service\basic;

use app\model\system\SystemConfig;
use Tinywan\Storage\Adapter\CosAdapter;
use Tinywan\Storage\Adapter\LocalAdapter;
use Tinywan\Storage\Adapter\QiniuAdapter;

class UploadService
{
    /**
     * 配置
     * @return array
     */
    public function setConfigVal(string $type = ""): array
    {
        $data = (new SystemConfig)->where("type","storage")->column("value","name");
        $config = [];
        $config['storage']['default'] = $data['type'];
        $config['storage']['include'] = !empty($data['allow_exts']) ? explode(",",$data['allow_exts']) : [];
        $config['storage']['exclude'] = !empty($data['noallow_exts']) ? explode(",",$data['noallow_exts']) : [];
        $config['storage']['nums'] = 10;
        $config['storage']['single_limit'] = 1024 * 1024 * 200;
        $config['storage']['total_limit'] = 1024 * 1024 * 200;
        if (empty($type)) $type = $data['type'];
        switch ($type)
        {
            case "local":
                $config['storage']['local'] = [
                    'adapter' => LocalAdapter::class,
                    'root' => public_path().'/uploads/storage/',
                    'dirname' => function () {
                        return date('Ymd');
                    },
                    'domain' => "",
                    'uri' => '/uploads/storage/', // 如果 domain + uri 不在 public 目录下，请做好软链接，否则生成的url无法访问
                    'algo' => 'sha1',
                ];
                break;
            case "qiniu":
                $config['storage']['qiniu'] = [
                    'adapter' => QiniuAdapter::class,
                    'accessKey' => $data['qiniu_access_key'],
                    'secretKey' => $data['qiniu_secret_key'],
                    'bucket' => $data['qiniu_bucket'],
                    'dirname' => 'storage/'.date("Ymd"),
                    'domain' => $this->setDomain($data['oss_http_protocol'],$data['qiniu_http_domain']),
                ];
                break;
            case "oss":
                $config['storage']['oss'] = [
                    'adapter' => \Tinywan\Storage\Adapter\OssAdapter::class,
                    'accessKeyId' => $data['oss_access_key'],
                    'accessKeySecret' => $data['oss_secret_key'],
                    'bucket' => $data['oss_bucket'],
                    'dirname' => function () {
                        return 'storage/'.date("Ymd");
                    },
                    'domain' => $this->setDomain($data['oss_http_protocol'],$data['oss_http_domain']),
                    'endpoint' => $data['oss_region'],
                    'algo' => 'sha1',

                ];
                break;
            case "cos":
                $config['storage']['cos'] = [
                    'adapter' => CosAdapter::class,
                    'secretId' => $data['cos_access_key'],
                    'secretKey' => $data['cos_secret_key'],
                    'bucket' => $data['cos_bucket'],
                    'dirname' => 'storage/'.date("Ymd"),
                    'domain' => $this->setDomain($data['oss_http_protocol'],$data['cos_http_domain']),
                    'region' => $data['cos_region'],
                ];
                break;
            default:
                break;
        }
        return $config;
    }

    /**
     * 协议域名
     * @param string $type
     * @param string $domain
     * @return string
     */
    protected function setDomain(string $type,string $domain): string
    {
        if ($type == "auto") {
            return "//".$domain;
        }
        return $type."://".$domain;
    }


}