Release LaySite 1.4.0 canvas editing and layers

This commit is contained in:
2026-11-26 01:28:51 +08:00
parent d5bedabaa1
commit 2e9b5fa17c
19 changed files with 915 additions and 227 deletions
+124 -13
View File
@@ -10,26 +10,133 @@ use RuntimeException;
final class Updates
{
public const REPOSITORY = 'https://git.laysense.com/laysense/laysite';
private const TAGS = 'https://git.laysense.com/api/v1/repos/laysense/laysite/tags';
public static function current(): string
{
return trim(file_get_contents(dirname(__DIR__, 2) . '/VERSION'));
$version = trim((string) @file_get_contents(dirname(__DIR__, 2) . '/VERSION'));
return preg_match('/^\d+\.\d+\.\d+$/D', $version) ? $version : '0.0.0';
}
public static function check(): array
{
$curl = curl_init(self::REPOSITORY . '/raw/branch/main/VERSION');
$directory = dirname(__DIR__, 2) . '/runtime';
if (!is_dir($directory) && !@mkdir($directory, 0750, true) && !is_dir($directory)) {
throw new RuntimeException('无法创建版本检查缓存。');
}
$path = $directory . '/laysite-update.json';
$record = self::cached($path);
if (($record['until'] ?? 0) <= time()) {
$lock = @fopen($path . '.lock', 'c');
if (!$lock) {
throw new RuntimeException('无法打开版本检查缓存。');
}
try {
if (!flock($lock, LOCK_EX | LOCK_NB)) {
if (!array_key_exists('latest', $record)) {
throw new RuntimeException('版本检查正在进行,请稍后重试。');
}
} else {
$record = self::cached($path);
if (($record['until'] ?? 0) <= time()) {
try {
$record = ['latest' => self::latestTag(), 'until' => time() + 300];
} catch (RuntimeException $exception) {
$record = ['error' => $exception->getMessage(), 'until' => time() + 30];
}
self::writeCache($path, $record);
}
}
} finally {
fclose($lock);
}
}
if (isset($record['error'])) {
throw new RuntimeException($record['error']);
}
$current = self::current();
$latest = $record['latest'];
return ['current' => $current, 'latest' => $latest, 'comparison' => $latest === null ? null : version_compare($latest, $current)];
}
private static function cached(string $path): array
{
$record = json_decode((string) @file_get_contents($path), true);
if (!is_array($record) || !is_int($record['until'] ?? null)) {
return [];
}
if (is_string($record['error'] ?? null)) {
return $record;
}
$latest = $record['latest'] ?? null;
return array_key_exists('latest', $record) && ($latest === null || (is_string($latest)
&& preg_match('/^\d+\.\d+\.\d+$/D', $latest))) ? $record : [];
}
private static function writeCache(string $path, array $record): void
{
$temporary = @tempnam(dirname($path), 'laysite-update-');
if ($temporary === false) {
throw new RuntimeException('无法写入版本检查缓存。');
}
try {
if (file_put_contents($temporary, Database::json($record)) === false || !rename($temporary, $path)) {
throw new RuntimeException('无法写入版本检查缓存。');
}
} finally {
if (is_file($temporary)) {
unlink($temporary);
}
}
}
private static function latestTag(): ?string
{
$latest = null;
$deadline = hrtime(true) + 3_000_000_000;
$page = 1;
do {
$remaining = (int) (($deadline - hrtime(true)) / 1_000_000);
if ($remaining <= 0) {
throw new RuntimeException('读取上游版本超时,请稍后重试。');
}
$tags = self::tags($page++, $remaining);
foreach ($tags as $tag) {
$name = $tag['name'] ?? null;
if (!is_string($name)) {
throw new RuntimeException('上游仓库返回的版本列表无效。');
}
if (preg_match('/^v(\d+\.\d+\.\d+)$/D', $name, $match)
&& ($latest === null || version_compare($match[1], $latest, '>'))) {
$latest = $match[1];
}
}
} while (count($tags) === 50);
return $latest;
}
private static function tags(int $page, int $timeout): array
{
$curl = curl_init(self::TAGS . '?limit=50&page=' . $page);
if ($curl === false) {
throw new RuntimeException('无法连接上游仓库,请稍后重试。');
}
$body = '';
$overflow = false;
curl_setopt_array($curl, [
CURLOPT_CONNECTTIMEOUT_MS => 2500,
CURLOPT_TIMEOUT_MS => 6000,
CURLOPT_CONNECTTIMEOUT_MS => min(1500, $timeout),
CURLOPT_TIMEOUT_MS => $timeout,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_HTTPHEADER => ['Accept: application/json'],
CURLOPT_USERAGENT => 'LaySite version check',
CURLOPT_WRITEFUNCTION => static function (CurlHandle $handle, string $chunk) use (&$body): int {
if (strlen($body) + strlen($chunk) > 128) {
CURLOPT_WRITEFUNCTION => static function (CurlHandle $handle, string $chunk) use (&$body, &$overflow): int {
if (strlen($body) + strlen($chunk) > 256 * 1024) {
$overflow = true;
return 0;
}
$body .= $chunk;
@@ -37,15 +144,19 @@ final class Updates
return strlen($chunk);
},
]);
if (curl_exec($curl) === false || curl_getinfo($curl, CURLINFO_RESPONSE_CODE) !== 200) {
throw new RuntimeException('无法读取上游版本,请稍后重试。');
if (curl_exec($curl) === false) {
throw new RuntimeException($overflow ? '上游版本数据超出长度限制。'
: (curl_errno($curl) === CURLE_OPERATION_TIMEDOUT ? '读取上游版本超时,请稍后重试。' : '无法连接上游仓库,请稍后重试。'));
}
if (!preg_match('/^v?(\d+\.\d+\.\d+)$/D', trim($body), $version)) {
throw new RuntimeException('上游仓库返回的版本格式无效。');
$status = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
if ($status !== 200) {
throw new RuntimeException('上游版本列表暂不可用(HTTP ' . $status . ')。');
}
$tags = json_decode($body);
if (!is_array($tags) || !array_is_list($tags)) {
throw new RuntimeException('上游仓库返回的版本列表无效。');
}
$current = self::current();
$latest = $version[1];
return ['current' => $current, 'latest' => $latest, 'comparison' => version_compare($latest, $current)];
return array_map(static fn ($tag): array => is_object($tag) ? get_object_vars($tag) : [], $tags);
}
}