<?php
/**
 * 客户端热更新相关
 * Date: 2017/9/13 0013
 * Time: 12:00
 */

$subversion = trim($_POST['subversion']);//热更新名称
$actList = $_POST['act']; //act名称
$device = $_POST['device'] == 1 ? 'android' : 'ios'; //设备名称
$stagefile = $_FILES['stagefile']; //上传文件名称名称

//加了_old表示之前的
$act_old = $_POST['act_old'];
$size_old = $_POST['size_old'];
$name_old = $_POST['name_old'];
$code_old = $_POST['code_old'];
$silent_old = $_POST['silent_old'];

$luaFile = '';//生成的lua文件
$time = time();
$hotuploadPath = './public/assets/'.$device.'/';
deldir($hotuploadPath);
foreach ($stagefile['name'] as $k => $fileName){
    if (!$fileName) {
        continue;
    }
    $uploadName = $fileName;
    $filePath = $hotuploadPath.$uploadName;
    $tmpName = $stagefile['tmp_name'][$k];
    move_uploaded_file($tmpName, $filePath);

    $zip = new ZipArchive();
    if ($zip->open($filePath) === true) {
        //解压文件到获得的路径a文件夹下
        $zip->extractTo($hotuploadPath);
        //关闭
        $zip->close();
    }
}

die(json_encode(['code'=>1,'msg'=>'ok']));

function getTitle($version){
    $arr = explode('.', $version);
    array_pop($arr);
    $new = implode('.', $arr);
    return $new;
}

function deldir($path){
    //如果是目录则继续
    if(is_dir($path)){
        $p = scandir($path);
        foreach($p as $val){
            if($val !="." && $val !=".."){
                if(is_dir($path.$val)){
                    deldir($path.$val.'/');
                    @rmdir($path.$val.'/');
                }else{
                    unlink($path.$val);
                }
            }
        }
    }
}
