<div>
<h4>您所选择的文件列表：</h4>
<div id="ossfile">你的浏览器不支持flash,Silverlight或者HTML5！</div>

<br/>

<div id="container">
    <a id="selectfiles" href="javascript:void(0);" class='btn'>选择视频文件</a>
    <a id="postfiles" href="javascript:void(0);" class='btn'>开始上传</a>
</div>

<pre id="console"></pre>

<p>&nbsp;</p>
</div>

{!! admin_css(["css/upload.css"]) !!}
<script type="text/javascript" src="/js/plupload-2.1.2/js/plupload.full.min.js"></script>
<script type="text/javascript">
Dcat.ready(function () {
var ossData = ''
g_dirname = ''
g_object_name = ''
g_object_name_type = ''
var url = "<?php echo env('APP_URL')?>"
console.log(url)
$.ajax({
    type: "GET",
    url: url + "/api/oss/alioss/sign",
    data: "",
    dataType: "json",
    success: function(res){
        ossData = res.data
    },
})

function check_object_radio() {
    g_object_name_type = 'random_name'
}

function get_dirname()
{
    g_dirname = ossData.dir
}

function random_string(len) {
　　len = len || 32;
　　var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
　　var maxPos = chars.length;
　　var pwd = '';
　　for (i = 0; i < len; i++) {
    　　pwd += chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return pwd;
}

function get_suffix(filename) {
    pos = filename.lastIndexOf('.')
    suffix = ''
    if (pos != -1) {
        suffix = filename.substring(pos)
    }
    return suffix;
}

function calculate_object_name(filename)
{
    if (g_object_name_type == 'local_name')
    {
        g_object_name += "${filename}"
    }
    else if (g_object_name_type == 'random_name')
    {
        suffix = get_suffix(filename)
        g_object_name = g_dirname + random_string(10) + suffix
    }
    return ''
}

function get_uploaded_object_name(filename)
{
    if (g_object_name_type == 'local_name')
    {
        tmp_name = g_object_name
        tmp_name = tmp_name.replace("${filename}", filename);
        return tmp_name
    }
    else if(g_object_name_type == 'random_name')
    {
        return g_object_name
    }
}

function set_upload_param(up, filename, ret)
{
    g_object_name = ossData.dir;
    if (filename != '') {
        suffix = get_suffix(filename)
        calculate_object_name(filename)
    }
    new_multipart_params = {
        'key' : g_object_name,
        'policy': ossData.policy,
        'OSSAccessKeyId': ossData.accessid,
        'success_action_status' : '200', //让服务端返回200,不然，默认会返回204
        'signature': ossData.signature,
    };

    up.setOption({
        'url': ossData.host,
        'multipart_params': new_multipart_params
    });

    up.start();
}

var uploader = new plupload.Uploader({
        runtimes : 'html5,flash,silverlight,html4',
        browse_button : 'selectfiles',
        container: document.getElementById('container'),
        flash_swf_url : 'lib/plupload-2.1.2/js/Moxie.swf',
        silverlight_xap_url : 'lib/plupload-2.1.2/js/Moxie.xap',
        url : 'http://oss.aliyuncs.com',
        init: {
                PostInit: function() {
                        document.getElementById('ossfile').innerHTML = '';
                        document.getElementById('postfiles').onclick = function() {
                        set_upload_param(uploader, '', false);
                        return false;
                    };
                },

                FilesAdded: function(up, files) {
                    if(uploader.files.length>1) { // 最多上传3张图
                        uploader.removeFile(files[0])
                        Dcat.info('只能上传一个文件！');
                        return false
                    }

                    plupload.each(files, function(file) {
                        //&nbsp;&nbsp;<span class="text-danger pic_delete" onclick=deleteFile('+ file.id +')><i class="fa fa-recycle"></i></span>
                        document.getElementById('ossfile').innerHTML += '<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ')<b></b>'
                                +'<div class="progress"><div class="progress-bar" style="width: 0%"></div></div>'
                                +'</div>';
                    });
                },

                BeforeUpload: function(up, file) {
                    check_object_radio();
                    get_dirname();
                    set_upload_param(up, file.name, true);
                },

                UploadProgress: function(up, file) {
                        var d = document.getElementById(file.id);
                        d.getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>";
                        var prog = d.getElementsByTagName('div')[0];
                        var progBar = prog.getElementsByTagName('div')[0]
                        progBar.style.width= 2*file.percent+'px';
                        progBar.setAttribute('aria-valuenow', file.percent);
                },

                FileUploaded: function(up, file, info) {
                    if (info.status == 200)
                    {
                        $('[name="video"]').val(ossData.host + get_uploaded_object_name(file.name))
                    }
                    else
                    {
                        document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML = info.response;
                    }
                },

                Error: function(up, err) {
                    document.getElementById('console').appendChild(document.createTextNode("\nError xml:" + err.response));
                },
        },
});

uploader.init();
})
// function deleteFile(files) {
//     console.log(files)
// }

</script>
