diff --git a/.gitignore b/.gitignore index c8e964aa..70fe63f2 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ .sass-cache vendor/* storage/textures/* +storage/update_cache/* node_modules/* resources/assets/src/bower_components/* koala-config.json diff --git a/app/Http/Controllers/UpdateController.php b/app/Http/Controllers/UpdateController.php index 833a855b..49d1ac87 100644 --- a/app/Http/Controllers/UpdateController.php +++ b/app/Http/Controllers/UpdateController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers; use Arr; use Log; use Utils; +use ZipArchive; use App\Services\Storage; use Illuminate\Http\Request; @@ -140,7 +141,43 @@ class UpdateController extends Controller break; case 'extract': - // + + if (!file_exists($tmp_path)) exit('No file available'); + + $extract_dir = storage_path("update_cache/{$this->latestVersion}"); + + $zip = new ZipArchive(); + $res = $zip->open($tmp_path); + + if ($res === true) { + Log::info("[ZipArchive] Extracting file $tmp_path"); + + try { + $zip->extractTo($extract_dir); + } catch (\Exception $e) { + exit('发生错误:'.$e->getMessage()); + } + + } else { + exit('更新包解压缩失败。错误代码:'.$res); + } + $zip->close(); + + if (Storage::copyDir($extract_dir, base_path()) !== true) { + + Storage::removeDir(storage_path('update_cache')); + exit('无法覆盖文件。'); + + } else { + + Log::info("[Extracter] Covering files"); + Storage::removeDir(storage_path('update_cache')); + + Log::info("[Extracter] Cleaning cache"); + } + + return json('更新完成', 0); + break; default: diff --git a/app/Services/Storage.php b/app/Services/Storage.php index aad521f8..105656d9 100644 --- a/app/Services/Storage.php +++ b/app/Services/Storage.php @@ -70,7 +70,7 @@ class Storage $size = 0; while($filename = @readdir($resource)) { if ($filename != "." && $filename != "..") { - $path = $dir.$filename; + $path = "$dir/$filename"; if (is_dir($path)) { // recursion self::removeDir($path."/"); @@ -96,7 +96,7 @@ class Storage $size = 0; while($filename = @readdir($resource)) { if ($filename != "." && $filename != "..") { - $path = $dir.$filename; + $path = "$dir/$filename"; if (is_dir($path)) { // recursion $size += self::getDirSize($path); @@ -121,7 +121,7 @@ class Storage $resource = opendir($dir); while($filename = readdir($resource)) { if ($filename != "." && $filename != "..") { - $path = $dir.$filename; + $path = "$dir/$filename"; if (is_dir($path)) { // recursion $file_num = self::getFileNum($path, $file_num); diff --git a/resources/assets/src/js/admin.js b/resources/assets/src/js/admin.js index 99d01b30..45915e7b 100644 --- a/resources/assets/src/js/admin.js +++ b/resources/assets/src/js/admin.js @@ -2,7 +2,7 @@ * @Author: printempw * @Date: 2016-07-22 14:02:44 * @Last Modified by: printempw - * @Last Modified time: 2016-11-25 12:45:28 + * @Last Modified time: 2016-11-25 13:10:36 */ 'use strict'; @@ -353,17 +353,33 @@ function downloadUpdates() { var interval_id = window.setInterval(function() { + $('#imported-progress').html(progress); + $('.progress-bar').css('width', progress+'%').attr('aria-valuenow', progress); + if (progress == 100) { clearInterval(interval_id); - $('#modal-start-download').modal('toggle'); + toastr.success('正在解压更新包'); + + console.log("Start extracting"); + $.ajax({ + url: './update/download?action=extract', + type: 'POST', + dataType: 'json' + }) + .done(function(json) { + console.log("Files covered"); + $('#modal-start-download').modal('toggle'); + + swal({ + type: 'success', + html: json.msg + }).then(function() { + window.location = "../"; + }); + }) + .fail(showAjaxError); - swal({ - type: 'success', - html: '下载完成!' - }).then(function(new_name) { - // - }); } else { $.ajax({ url: './update/download?action=get-file-size', @@ -372,9 +388,6 @@ function downloadUpdates() { .done(function(json) { progress = (json.size / file_size * 100).toFixed(2); - $('#imported-progress').html(progress); - $('.progress-bar').css('width', progress+'%').attr('aria-valuenow', progress); - console.log("Progress: "+progress); }) .fail(showAjaxError); diff --git a/storage/update_cache/.gitignore b/storage/update_cache/.gitignore deleted file mode 100644 index d6b7ef32..00000000 --- a/storage/update_cache/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -* -!.gitignore