<?php
namespace Upload\Src\Store;

use Dever;

class Handle
{
    /**
     * __construct
     * 
     * @return mixed
     */
    public function __construct($data = array())
    {
        $this->yun = $this->local = false;
        $config = Dever::db('upload/upload')->one($data['key']);

        $this->save_type = $config['save_type'];

        $yun = $config['yun'];
        if ($this->save_type >= 2) {
            if ($yun == 1) {
                $yun = 'Upload\Src\Store\Qiniu';
            }
            
            $this->yun = new $yun($data);
        }

        if ($this->save_type <= 3) {
            $this->local = new Local($data);
        }
    }

    # 复制文件到服务器上
    public function copy()
    {
        return $this->get();
    }

    # 获取文件名
    public function name()
    {
        return $this->get('getName');
    }


    # 获取文件名
    public function get($type = 'copy')
    {
        $yun = $local = array();

        if ($this->save_type <= 3) {
            $local = $this->local->$type();
        }

        if ($local && $local['status'] == -1) {
            return $local;
        }

        if ($this->save_type >= 2) {
            $yun = $this->yun->$type();
        }

        if ($type == 'copy') {
            if ($this->local) {
                $this->local->delete();
            } elseif ($this->yun) {
                $this->yun->delete();
            }
        }

        if ($this->save_type <= 2) {
            return $local;
        } elseif ($this->save_type > 2) {
            return $yun;
        }
    }

    public function handle_t($id, $source, $dest, $path)
    {
        $yun = $local = array();

        if ($this->save_type <= 3) {
            $local = $this->local->handle_t($id, $source, $dest);
        }

        if ($local && $local['status'] == -1) {
            return $local;
        }

        if ($this->save_type >= 2) {
            $yun = $this->yun->handle_t($id, $source, $dest, $path);
        }

        if ($this->save_type <= 2) {
            return $local;
        } elseif ($this->save_type > 2) {
            return $yun;
        }
    }
}
