* Finish download image.

This commit is contained in:
wangjianhua
2022-11-11 17:09:24 +08:00
parent 2a361532b4
commit 74beb1f75a
7 changed files with 119 additions and 45 deletions
+1
View File
@@ -3199,6 +3199,7 @@ EOD;
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($curl, CURLOPT_HEADER, FALSE);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 2);
curl_setopt($curl, CURLINFO_HEADER_OUT, TRUE);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_URL, $url);
+25 -1
View File
@@ -209,8 +209,32 @@ class zahost extends control
$image = $this->zahost->getImageByID($imageID);
$this->zahost->downloadImage($image);
if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => $this->lang->zahost->image->downloadImageFail));
return $this->send(array('result' => 'success', 'message' => $this->lang->zahost->image->startDowload));
return $this->send(array('result' => 'success', 'message' => $this->lang->zahost->image->downloadImageSuccess));
}
/**
* Query downloading progress of images of host.
*
* @param int $hostID
* @access public
* @return void
*/
public function ajaxImageDownloadProgress($hostID)
{
$statusList = array();
$imageList = $this->zahost->getImageList($hostID);
foreach($imageList as $image)
{
$statusCode = $this->zahost->queryDownloadImageStatus($image);
$statusName = zget($this->lang->zahost->image->statusList, $statusCode,'');
$statusList[$image->id] = array('statusCode' => $statusCode, 'statusName' => $statusName);
}
return $this->send(array('result' => 'success', 'message' => '', 'data' => $statusList));
}
/**
+18
View File
@@ -0,0 +1,18 @@
$(function()
{
setInterval(function()
{
$.get(createLink('zahost', 'ajaxImageDownloadProgress', 'hostID=' + hostID)).done(function(response)
{
var result = JSON.parse(response);
var statusList = result.data;
console.log(statusList);
for(var imageID in statusList)
{
$('.image-status-' + imageID).text(statusList[imageID].statusName);
}
});
}, 5000);
});
-33
View File
@@ -1,38 +1,5 @@
$(function()
{
if(typeof(hostID) != 'undefined')
{
var templateID = 0;
if(typeof(template) == 'object') templateID = template.id;
$.get(createLink('zahost', 'ajaxImageList', 'hostID=' + hostID + '&templateID=' + templateID), function(response)
{
var resultData = JSON.parse(response);
var options = '';
if(resultData.result == 'success')
{
resultData.data.forEach(function(item)
{
options += "<option value='" + item.name + "'>" + item.name + "</option>";
});
}
$('#imageName').replaceWith("<select name='imageName' id='imageName' class='form-control'>" + options + "</select>");
$("#imageName_chosen").remove();
$("#imageName").next('.picker').remove();
$('#imageName').chosen();
if(resultData.result == 'fail')
{
$('#imageName_chosen a:first-child').addClass('has-error');
if(resultData.message.imageName)
{
var errors = resultData.message.imageName.join('');
$('#imageName_chosen').after("<div id='imageNameLabel' class='text-danger helper-text'>" + errors + "</div>");
}
}
});
}
$('#osCategory').change(function()
{
var os = $('#osCategory').val();
+8 -6
View File
@@ -79,13 +79,15 @@ $lang->zahost->image->osVersion = $lang->zahost->osVersion;
$lang->zahost->image->osLang = $lang->zahost->osLang;
$lang->zahost->image->imageName = $lang->zahost->imageName;
$lang->zahost->image->imageEmpty = '无镜像';
$lang->zahost->image->statusList['waiting'] = '未下载';
$lang->zahost->image->statusList['downloading'] = '下载中';
$lang->zahost->image->statusList['finish'] = '已下载';
$lang->zahost->image->statusList['fail'] = '下载失败';
$lang->zahost->image->statusList['created'] = '已创建';
$lang->zahost->image->statusList['canceled'] = '已取消';
$lang->zahost->image->statusList['inprogress'] = '下载中';
$lang->zahost->image->statusList['completed'] = '已下载';
$lang->zahost->image->statusList['failed'] = '下载失败';
$lang->zahost->image->imageEmpty = '无镜像';
$lang->zahost->image->downloadImageFail = '创建下载镜像任务失败';
$lang->zahost->image->downloadImageSuccess = '创建下载镜像任务成功';
$lang->zahost->vmTemplate = new stdclass;
$lang->zahost->vmTemplate->name = '名称';
+65 -4
View File
@@ -230,19 +230,80 @@ class zahostModel extends model
*/
public function downloadImage($image)
{
$host = $this->getById($image->hostID);
$downloadApi = 'http://' . $host->address . '/api/v1/download/add';
$host = $this->getById($image->hostID);
$apiUrl = 'http://' . $host->publicIP. '/api/v1/download/add';
$apiParams['md5'] = $image->md5;
$apiParams['task'] = 1;
$apiParams['url'] = $image->address;
$apiParams['task'] = intval($image->id);
$response = json_decode(commonModel::http($downloadApi, $apiParams));
$response = json_decode(commonModel::http($apiUrl, array($apiParams), array(CURLOPT_CUSTOMREQUEST => 'POST'), array(), 'json'));
if($response and $response->code == 'success')
{
$this->dao->update(TABLE_IMAGE)
->set('status')->eq('created')
->where('id')->eq($image->id)->exec();
return true;
}
dao::$errors[] = $this->lang->zahost->image->downloadImageFail;
return false;
}
/**
* Query image download progress.
*
* @param object $image
* @access public
* @return string Return Status code.
*/
public function queryDownloadImageStatus($image)
{
$host = $this->getById($image->hostID);
$apiUrl = 'http://' . $host->publicIP. '/api/v1/task/status';
$result = json_decode(commonModel::http($apiUrl, array(), array(CURLOPT_CUSTOMREQUEST => 'GET'), array(), 'json'));
if(!$result or $result->code != 'success') return $image->status;
foreach($result->data as $status => $group)
{
$task = array_filter($group, function($task)
{
$task->id == $image->id;
});
if($task)
{
$image->status = $status;
break;
}
}
$this->dao->update(TABLE_IMAGE)->set('status')->eq($status)->where('id')->eq($image->id)->exec();
return $image->status;
}
/**
* Query download image status.
*
* @param object $image
* @access public
* @return object
*/
public function downloadImageStatus($image)
{
$host = $this->getById($image->hostID);
$statusApi = 'http://' . $host->address . '/api/v1/task/status';
$response = json_decode(commonModel::http($statusApi, array(), array(CURLOPT_CUSTOMREQUEST => 'GET'), array(), 'json'));
a($response);
if($response->code == 200) return true;
dao::$errors[] = $response->msg;
return false;
}
/**
+2 -1
View File
@@ -11,6 +11,7 @@
*/
?>
<?php include '../../common/view/header.html.php';?>
<?php js::set('hostID', $hostID);?>
<div id='mainMenu' class='clearfix'>
<div class='pull-left btn-toolbar'>
<?php echo html::a($this->createLink('zahost', 'browseimage', "hostID=$hostID"), "<span class='text'>{$lang->zahost->image->browseImage}</span>", '', "class='btn btn-link btn-active-text'");?>
@@ -63,7 +64,7 @@
<td><?php echo zget($config->zahost->os->type[$image->osCategory], $image->osType);?></td>
<td><?php echo zget($lang->zahost->versionList[$image->osType], $image->osVersion);?></td>
<td><?php echo zget($lang->zahost->langList, $image->osLang);?></td>
<td class='download-status'><?php echo zget($lang->zahost->image->statusList, $image->status, '');?></td>
<td class='image-status-<?php echo $image->id;?>'><?php echo zget($lang->zahost->image->statusList, $image->status, '');?></td>
<td class='c-actions'>
<?php common::printIcon('zahost', 'ajaxdownloadImage', "id={$image->id}", $image, 'list', 'download');?>
<?php //if(common::hasPriv('zahost', 'downloadImage')) echo html::a($this->createLink('zahost', 'downloadImage', "id={$image->id}"), '<i class="icon-trash"></i>', 'hiddenwin', "title='{$lang->zahost->image->downloadImage}' class='btn'");?>