<?php
/**
 * JYmusic音乐管理系统
 *
 * @version     2.0
 * @author      战神~~巴蒂 <jyuucn@163.com>
 * @license     http://jyuu.cn [未经授权严禁私自出售，否者承担法律责任]
 * @copyright   2014 - 2017 JYmusic
 */

namespace addons\Upload\controller;

use app\common\model\File;
use app\common\model\Picture;

class LocalController extends BaseController
{
    protected $uploadConf;

    public function _initialize()
    {
        $this->uploadConf = get_addon_config('Upload');
    }

    //用户上传需要验证token
    public function index()
    {
        $fileType   = text_filter($this->request->param('file_type', 'image'));
        $uploadType = text_filter($this->request->param('upload_type', ''));
        
        $validation = $this->getValid($fileType);
        
        if (false === $validation) {
            $this->response['code'] = 40029;
            $this->response['error'] = lang(40029);
        } else {
            $this->validation = $validation;
            $this->$fileType($uploadType);
        }
        
        return json($this->response, 200);
    }

    /**
     * 监听音频上传文件
     * @param string $type
     */
    public function audio($type = '')
    {
        $this->fileModel = new File();
        
        if ('down' == $type) {
            $savePath = $this->uploadConf['local_audio_down_path'];
        } else {
            $savePath = $this->uploadConf['local_audio_listen_path'];
        }
        
        $maxSize = intval($this->uploadConf['local_audio_max_size']);
        $maxSize = $maxSize ? $maxSize  : 10; //单位M
    
        //设置验证类型
        $this->validation = $this->getValid('audio');
        
        $config['maxSize'] = $maxSize * 1024 * 1024;
        $config['saveDir'] = trim(str_replace('\\', '/', $savePath) , '/'). '/' . date('Y-m-d') . '/';
        
        $this->uploader($config);
    }
    

    /**
     * 监听图片上传文件
     * @param string $type
     * @return mixed
     */
    public function image($type = '')
    {
        $this->fileModel = new Picture();
    
        $maxSize = intval($this->uploadConf['local_image_max_size']);
        $maxSize = $maxSize ? $maxSize  : 2; //单位M
        
        $savePath = rtrim($this->uploadConf['local_image_path'], '/');
        
        if ('attach' == $type) {
            $savePath = $savePath . '/attach';
        } elseif ('icon' == $type) {
            $savePath = $savePath . '/icons';
        } elseif ('cover' == $type) {
            $savePath = $savePath . '/cover';
        } elseif ('article' == $type) {
            $savePath = $savePath . '/article';
        }
        
        //设置验证类型
        $this->validation = $this->getValid('image');

        $config['maxSize'] = $maxSize * 1024 * 1024;
        $config['saveDir'] = trim(str_replace('\\', '/', $savePath) , '/'). '/' . date('Y-m-d') . '/';
    
        $this->uploader($config);
    
        if ($type == 'wangEdit2') {
            return $this->retWangEdit2();
        } elseif ($type == 'wangEdit3') {
            return $this->retWangEdit3();
        }
    }
}
