91 lines
2.2 KiB
PHP
91 lines
2.2 KiB
PHP
<?php
|
|
declare (strict_types = 1);
|
|
|
|
namespace app\admin\controller\api;
|
|
|
|
use think\admin\Controller;
|
|
|
|
/**
|
|
* 程序更新接口
|
|
*/
|
|
class Program extends Controller
|
|
{
|
|
/**
|
|
* 程序目录配置
|
|
*/
|
|
private $programPath = 'program/';
|
|
|
|
/**
|
|
* 获取程序信息
|
|
*/
|
|
public function info()
|
|
{
|
|
$path = public_path() . $this->programPath;
|
|
if (!is_dir($path)) {
|
|
return json([
|
|
'code' => 1,
|
|
'msg' => '程序目录不存在'
|
|
]);
|
|
}
|
|
|
|
// 获取目录中最新的文件
|
|
$files = glob($path . '*');
|
|
if (empty($files)) {
|
|
return json([
|
|
'code' => 1,
|
|
'msg' => '暂无可用程序'
|
|
]);
|
|
}
|
|
|
|
// 获取最新文件
|
|
$latest = array_reduce($files, function($carry, $file) {
|
|
return (!$carry || filemtime($file) > filemtime($carry)) ? $file : $carry;
|
|
});
|
|
|
|
$name = basename($latest);
|
|
$size = filesize($latest);
|
|
$md5 = md5_file($latest);
|
|
|
|
return json([
|
|
'code' => 0,
|
|
'msg' => '获取成功',
|
|
'data' => [
|
|
'name' => $name,
|
|
'size' => $size,
|
|
'md5' => $md5,
|
|
'url' => request()->domain() . '/' . $this->programPath . $name,
|
|
'time' => date('Y-m-d H:i:s', filemtime($latest))
|
|
]
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 下载程序
|
|
*/
|
|
public function down()
|
|
{
|
|
$path = public_path() . $this->programPath;
|
|
if (!is_dir($path)) {
|
|
return json([
|
|
'code' => 1,
|
|
'msg' => '程序目录不存在'
|
|
]);
|
|
}
|
|
|
|
// 获取目录中最新的文件
|
|
$files = glob($path . '*');
|
|
if (empty($files)) {
|
|
return json([
|
|
'code' => 1,
|
|
'msg' => '暂无可用程序'
|
|
]);
|
|
}
|
|
|
|
// 获取最新文件
|
|
$latest = array_reduce($files, function($carry, $file) {
|
|
return (!$carry || filemtime($file) > filemtime($carry)) ? $file : $carry;
|
|
});
|
|
|
|
return download($latest, basename($latest));
|
|
}
|
|
}
|