<?php

namespace lib;

class StorHelper
{
    private static function getConfig($storage){
        global $conf;
        switch($storage){
            case 'local':
                return $conf['filepath'];
                break;
            case 'sae':
            case 'ace':
                return $conf['storagename'];
                break;
            case 'oss':
                return ['accessKeyId' => $conf['oss_ak'], 'accessKeySecret' => $conf['oss_sk'], 'endpoint' => $conf['oss_endpoint'], 'bucket' => $conf['oss_bucket']];
                break;
            case 'qcloud':
                return ['secretId' => $conf['qcloud_id'], 'secretKey' => $conf['qcloud_key'], 'region' => $conf['qcloud_region'], 'bucket' => $conf['qcloud_bucket']];
                break;
            case 'obs':
                return ['accessKey' => $conf['obs_ak'], 'secretKey' => $conf['obs_sk'], 'endpoint' => $conf['obs_endpoint'], 'bucket' => $conf['obs_bucket']];
            case 'upyun':
                return ['operatorName' => $conf['upyun_user'], 'operatorPwd' => $conf['upyun_pwd'], 'serviceName' => $conf['upyun_name']];
            case 'qiniu':
                return ['accessKey' => $conf['qiniu_ak'], 'secretKey' => $conf['qiniu_sk'], 'bucket' => $conf['qiniu_bucket'], 'domain' => $conf['qiniu_domain']];
            case 'rainyun':
                return ['accessKey' => $conf['rainyun_ak'], 'secretKey' => $conf['rainyun_sk'], 'bucket' => $conf['rainyun_bucket'], 'endpoint' => $conf['rainyun_endpoint']];
            default:
                break;
        }
    }

    public static function getModel($storage)
    {
        if (!self::isConfigValid($storage)) {
            $storage = 'local';
        }
        $class = "\\lib\\Storage\\".ucwords($storage);
        $config = self::getConfig($storage);
        if(class_exists($class)){
            $model = new $class($config);
            return $model;
        }
        return false;
    }

    private static function isConfigValid($storage)
    {
        global $conf;
        switch ($storage) {
            case 'oss':
                return !empty($conf['oss_ak']) && !empty($conf['oss_sk']) && !empty($conf['oss_endpoint']) && !empty($conf['oss_bucket']);
            case 'qcloud':
                return !empty($conf['qcloud_id']) && !empty($conf['qcloud_key']) && !empty($conf['qcloud_region']) && !empty($conf['qcloud_bucket']);
            case 'obs':
                return !empty($conf['obs_ak']) && !empty($conf['obs_sk']) && !empty($conf['obs_endpoint']) && !empty($conf['obs_bucket']);
            case 'upyun':
                return !empty($conf['upyun_user']) && !empty($conf['upyun_pwd']) && !empty($conf['upyun_name']);
            case 'qiniu':
                return !empty($conf['qiniu_ak']) && !empty($conf['qiniu_sk']) && !empty($conf['qiniu_bucket']);
            case 'rainyun':
                return !empty($conf['rainyun_ak']) && !empty($conf['rainyun_sk']) && !empty($conf['rainyun_bucket']) && !empty($conf['rainyun_endpoint']);
            case 'local':
            case 'sae':
            case 'ace':
                return true;
            default:
                return false;
        }
    }

    //判断是否可以直接链接
    public static function is_cloud(){
        global $conf;
        $is_cloud = false;
        if(in_array($conf['storage'], ['oss','qcloud','obs','upyun','qiniu','rainyun'])) $is_cloud = true;
        return $is_cloud;
    }

    //判断是否可以断点续传
    public static function is_range(){
        global $conf;
        $is_range = false;
        if(in_array($conf['storage'], ['local','oss','qcloud','obs','qiniu','rainyun'])) $is_range = true;
        return $is_range;
    }
}