15m44w 工单附件换思路: issue API不支持附件, 改走git推图仓
- gh_api.php 新增 ghImgRepoPush/ghGitAvailable/ghImgRepoPushViaGit/ghImgRepoPushViaContents
读 img.json repo.imgrepo(模式/用户名/仓库地址/key登录密码/分支), 运行时先 ghGitAvailable
有git走 commit+push(PAT拼认证URL), 没git走 Contents API 兜底, raw链接 raw.githubusercontent.com
仓内路径 {玩家名}/{工单号}_{YYYYMMDD}_{清洗标题}/{原文件名}; 删掉 ghReadGitCredential(改读img.json token)
- lsky.php loadImgJson 解析 imgrepo 段 + imgrepo_mode/owner/full/branch/token
- api/ticket.php uploadTicketFiles 分流: imgrepo_mode=gh 存临时文件返回 @ghimgrepo: 占位,
图床模式走图床拿真URL; ticketCreate 推仓后回写 images 拿 raw URL
- api/gh_git_probe.php 新增诊断端点(检测git/imgrepo配置/raw域名可达性)
- img.json 仓库版 imgrepo 段全占位符(真PAT在 .workbuddy 不推git)
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
/**
|
||||
* 15m44w: 服务端 git 可用性 + imgrepo 仓可达性 探测端点。
|
||||
* 用法:GET gh_git_probe.php (无鉴权,仅诊断;返回 git 版本 + imgrepo 配置 + 是否可访问仓)
|
||||
* 用于定位"附件 git 推图仓"功能是否能在服务器跑起来(git 二进制/exec 权限/仓可达性)。
|
||||
*/
|
||||
while (ob_get_level() > 0) { @ob_end_clean(); }
|
||||
ob_start();
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
header('Cache-Control: no-store');
|
||||
|
||||
require_once __DIR__ . '/../core.php';
|
||||
require_once __DIR__ . '/../lsky.php';
|
||||
require_once __DIR__ . '/../gh_api.php';
|
||||
|
||||
$cfg = loadImgJson();
|
||||
|
||||
// 1) git 二进制是否可用
|
||||
$gitOk = false;
|
||||
$gitVer = '';
|
||||
$gitCmd = 'git --version';
|
||||
if (function_exists('exec')) {
|
||||
$out = [];
|
||||
$ret = 0;
|
||||
@exec($gitCmd . ' 2>&1', $out, $ret);
|
||||
if ($ret === 0 && !empty($out)) {
|
||||
$gitOk = true;
|
||||
$gitVer = trim(implode(' ', $out));
|
||||
}
|
||||
} elseif (function_exists('shell_exec')) {
|
||||
$out = @shell_exec($gitCmd . ' 2>&1');
|
||||
if (is_string($out)) { $gitVer = trim($out); $gitOk = ($gitVer !== ''); }
|
||||
}
|
||||
|
||||
// 2) imgrepo 配置
|
||||
$ir = [
|
||||
'mode' => $cfg['imgrepo_mode'] ?? '',
|
||||
'owner' => $cfg['imgrepo_owner'] ?? '',
|
||||
'full' => $cfg['imgrepo_full'] ?? '',
|
||||
'branch' => $cfg['imgrepo_branch'] ?? '',
|
||||
'token' => (isset($cfg['imgrepo_token']) && $cfg['imgrepo_token'] !== '') ? substr($cfg['imgrepo_token'], 0, 4) . '****' : '',
|
||||
];
|
||||
|
||||
// 3) 仓可达性(带 token GET raw 域名,不实际推)
|
||||
$reach = '';
|
||||
$reachStatus = 0;
|
||||
if ($ir['full'] !== '' && $ir['owner'] !== '') {
|
||||
$rawUrl = 'https://raw.githubusercontent.com/' . $ir['full'] . '/' . $ir['branch'];
|
||||
if (function_exists('curl_init')) {
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $rawUrl);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
|
||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
|
||||
curl_exec($ch);
|
||||
$reachStatus = (int)curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
|
||||
$reach = 'HTTP ' . $reachStatus;
|
||||
curl_close($ch);
|
||||
}
|
||||
}
|
||||
|
||||
success([
|
||||
'git_available' => $gitOk,
|
||||
'git_version' => $gitVer,
|
||||
'exec_func' => function_exists('exec'),
|
||||
'proc_open' => function_exists('proc_open'),
|
||||
'imgrepo' => $ir,
|
||||
'raw_reach' => $reach,
|
||||
'hint' => $gitOk
|
||||
? 'git 可用,附件可走 git 推图仓'
|
||||
: '⚠ 服务器 git 不可用(未装 git 或 exec 被禁),附件 git 推图仓功能无法运行——需服务器装 git 并放开 PHP exec',
|
||||
], 'ok');
|
||||
+93
-6
@@ -293,14 +293,68 @@ function uploadTicketFiles() {
|
||||
return $GLOBALS['__ticket_dbg_urls'];
|
||||
}
|
||||
|
||||
// ★ 15m44n 互斥 → ★ 15m44v 变更(用户定调"附件要显示实际图片"):
|
||||
// GitHub 模式下附件【也走图床】拿真实 URL(图床配了真值时),ghSyncCreate 把 URL 用
|
||||
// markdown 图片语法  嵌进 issue body,GitHub 会把外链图片转存到自家 CDN 正常显示。
|
||||
// 这样 PHP 后台 + GitHub 都显示实际图片,不再是"只显示文件名"。
|
||||
// ★ 15m44w(用户定调新架构):GitHub 模式附件【走 git 推图片仓】,不走图床 API。
|
||||
// imgrepo.模式 = true/GitHub 变体(不区分大小写)→ git仓模式:把图片 commit+push 到
|
||||
// 专用 imgrepo 仓,拿 raw.githubusercontent.com 链接(省服务器内存,用户原话)。
|
||||
// imgrepo.模式 = false/img/图床/tuchuang → 图床模式:走 EasyImages2.0 拿真实 URL。
|
||||
// git仓模式需 ticketId+title(建单后才有)→ 这里先收"待推文件"占位标记
|
||||
// (@ghimgrepo:<临时路径>:<原文件名>),由 ticketCreate 入库后逐个 ghImgRepoPush 替换成 raw URL。
|
||||
if (!empty($cfg['gh_enabled']) && isset($cfg['imgrepo_mode']) && $cfg['imgrepo_mode'] === 'gh') {
|
||||
// 收集上传文件(暂不推仓,先校验存在 + 大小 + 后缀),返回占位标记
|
||||
$maxImages = max(1, (int)($cfg['max_images'] ?? 3));
|
||||
$maxBytes = max(1, (int)($cfg['max_size_mb'] ?? 2)) * 1024 * 1024;
|
||||
$allowedExts = array_map('strtolower', (array)($cfg['allowed_exts'] ?? ['jpg','jpeg','png']));
|
||||
$files = null;
|
||||
if (isset($_FILES['images[]']) && is_array($_FILES['images[]']) && !empty($_FILES['images[]']['name'])) {
|
||||
$files = $_FILES['images[]'];
|
||||
} elseif (isset($_FILES['images']) && is_array($_FILES['images'])) {
|
||||
$files = $_FILES['images'];
|
||||
if (!isset($files['name']) || !is_array($files['name'])) {
|
||||
$files = ['name' => [$files['name'] ?? ''], 'tmp_name' => [$files['tmp_name'] ?? ''], 'size' => [$files['size'] ?? 0], 'error' => [$files['error'] ?? UPLOAD_ERR_NO_FILE]];
|
||||
}
|
||||
} elseif (isset($_FILES['image']) && is_array($_FILES['image'])) {
|
||||
$f = $_FILES['image'];
|
||||
$files = ['name' => [$f['name'] ?? ''], 'tmp_name' => [$f['tmp_name'] ?? ''], 'size' => [$f['size'] ?? 0], 'error' => [$f['error'] ?? UPLOAD_ERR_NO_FILE]];
|
||||
}
|
||||
if (empty($files) || !is_array($files) || empty($files['name'])) {
|
||||
$GLOBALS['__ticket_dbg_uploaded'] = 0;
|
||||
$GLOBALS['__ticket_dbg_urls'] = normalizeTicketImages();
|
||||
return $GLOBALS['__ticket_dbg_urls'];
|
||||
}
|
||||
$out = [];
|
||||
for ($i = 0; $i < count($files['name']) && count($out) < $maxImages; $i++) {
|
||||
if ((int)$files['error'][$i] !== UPLOAD_ERR_OK) {
|
||||
if ((int)$files['error'][$i] === UPLOAD_ERR_NO_FILE) continue;
|
||||
$msgs = [UPLOAD_ERR_INI_SIZE=>'图片超过服务器单文件大小上限',UPLOAD_ERR_FORM_SIZE=>'图片超过表单大小上限',
|
||||
UPLOAD_ERR_PARTIAL=>'图片上传中断,请重试',UPLOAD_ERR_NO_TMP_DIR=>'服务器临时目录缺失',
|
||||
UPLOAD_ERR_CANT_WRITE=>'服务器写入临时文件失败'];
|
||||
error(isset($msgs[(int)$files['error'][$i]]) ? $msgs[(int)$files['error'][$i]] : ('图片上传错误码 '.$files['error'][$i]));
|
||||
}
|
||||
$orig = sanitizeImageFileName(basename((string)$files['name'][$i]));
|
||||
$ext = strtolower(pathinfo($orig, PATHINFO_EXTENSION));
|
||||
if (!in_array($ext, $allowedExts, true)) {
|
||||
// git仓模式放宽:允许 png/jpg/jpeg/jpe/jfif/bmp/webp(图床模式才严格 getimagesize 校验真格式)
|
||||
$okExts = array_merge($allowedExts, ['jfif']);
|
||||
if (!in_array($ext, $okExts, true)) error('仅支持后缀 ' . implode(' / ', $allowedExts));
|
||||
}
|
||||
if ((int)$files['size'][$i] > $maxBytes) error('单张图不能超过 ' . round($maxBytes / 1048576, 1) . 'MB');
|
||||
// 临时文件复制到项目可控目录(避免 PHP 上传临时文件被清)
|
||||
$keepDir = dirname(__DIR__) . '/db/tmp_uploads';
|
||||
if (!is_dir($keepDir)) @mkdir($keepDir, 0755, true);
|
||||
$keepPath = $keepDir . '/' . date('Ymd_His') . '_' . mt_rand(1000, 9999) . '_' . $orig;
|
||||
if (!@copy($files['tmp_name'][$i], $keepPath)) error('附件暂存失败');
|
||||
// 占位标记:ticketCreate 入库后按 ticketId 调 ghImgRepoPush(keepPath, orig) 拿 raw URL
|
||||
$out[] = '@ghimgrepo:' . $keepPath . ':' . $orig;
|
||||
}
|
||||
$GLOBALS['__ticket_dbg_uploaded'] = count($out);
|
||||
$GLOBALS['__ticket_dbg_urls'] = $out;
|
||||
return $out;
|
||||
}
|
||||
|
||||
// ★ 15m44v 变更(用户定调"附件要显示实际图片"):
|
||||
// 非 git仓模式的 GitHub(imgrepo_mode=img)→ 附件走图床拿真实 URL。
|
||||
// 仅当图床没配(baseurl/key 空)时才回退 @gh: 文件名占位(玩家去 GitHub 手动传)。
|
||||
if (!empty($cfg['gh_enabled'])) {
|
||||
// 图床可用 → 不拦截,落到下方 uploadTicketFiles 走 easyUploadFile 拿真实 URL
|
||||
// 图床没配 → 回退 ghTicketFileNames(@gh: 占位,仅记文件名)
|
||||
if ($cfg['baseurl'] === '' || $cfg['key'] === '' || !$cfg['attach_enabled']) {
|
||||
$out = ghTicketFileNames();
|
||||
$GLOBALS['__ticket_dbg_uploaded'] = count($out);
|
||||
@@ -590,6 +644,39 @@ function ticketCreate() {
|
||||
$GLOBALS['__ticket_dbg_urls'] = $images;
|
||||
writeTicketCreateDebug();
|
||||
|
||||
// ★ 15m44w: git仓模式附件推仓拿 raw URL —— 入库后逐个把 @ghimgrepo: 占位换成 raw 链接。
|
||||
// 此时已有 ticketId+title,调 ghImgRepoPush 推图到 imgrepo 仓,更新 images(写库+同步issue 都用)。
|
||||
if (is_array($images) && count(array_filter($images, function($u) { return is_string($u) && strpos($u, '@ghimgrepo:') === 0; })) > 0) {
|
||||
$resolved = [];
|
||||
foreach ($images as $u) {
|
||||
if (is_string($u) && strpos($u, '@ghimgrepo:') === 0) {
|
||||
// @ghimgrepo:<临时路径>:<原文件名>
|
||||
$parts = explode(':', $u, 3);
|
||||
$tmp = isset($parts[1]) ? $parts[1] : '';
|
||||
$orig = isset($parts[2]) ? $parts[2] : 'attachment';
|
||||
$r = function_exists('ghImgRepoPush') ? ghImgRepoPush($player, $ticketId, $title, $tmp, $orig) : ['ok' => false];
|
||||
if (!empty($r['ok'])) {
|
||||
$resolved[] = $r['url']; // raw.githubusercontent.com 链接
|
||||
} else {
|
||||
// 推仓失败 → 回退 @gh: 文件名占位(让玩家去 GitHub 手动传原图)
|
||||
$resolved[] = '@gh:附件图' . (count($resolved) + 1) . ':' . $orig;
|
||||
}
|
||||
@unlink($tmp); // 清临时文件
|
||||
} else {
|
||||
$resolved[] = $u;
|
||||
}
|
||||
}
|
||||
$images = $resolved;
|
||||
// 回写工单 images(建单时存的是 @ghimgrepo: 占位,现在换成 raw URL)
|
||||
try {
|
||||
$upd = $db->prepare("UPDATE web_tickets SET images = :imgs WHERE id = :id");
|
||||
$upd->bindValue(':imgs', encodeTicketImages($images), SQLITE3_TEXT);
|
||||
$upd->bindValue(':id', $ticketId, SQLITE3_INTEGER);
|
||||
$upd->execute();
|
||||
} catch (\Throwable $e) { /* 回写失败不阻塞 */ }
|
||||
$GLOBALS['__ticket_dbg_urls'] = $images;
|
||||
}
|
||||
|
||||
// 15m44l: 同步 GitHub issue(非阻塞:失败不影响 PHP 工单入库;未授权/未配置则跳过)
|
||||
$ghToken = ghTicketToken($player);
|
||||
$ghSyncOk = ghSyncCreate($ticketId, $player, $title, $description, $images, $ghToken);
|
||||
|
||||
+154
@@ -443,6 +443,160 @@ function ghHandleWebhook($payload, $event) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* ★ 15m44w: 把工单附件图推到 GitHub 图片仓(imgrepo = issueimg),拿 raw 链接。
|
||||
* 用户定调:GitHub 模式本就是为省服务器内存,附件不走图床 API,改成 git 推图仓。
|
||||
* - 运行时先检测 git 环境:有 git → 走 git commit+push(账号/key 用【img.json 的 PAT】拼认证 URL);
|
||||
* 无 git → 走 GitHub Contents API(token 也从 img.json 读)。
|
||||
* - 文件路径:{玩家名}/{工单号}_{YYYYMMDD}_{工单标题}/{原文件名}(每玩家一个文件夹)
|
||||
* - raw 链接:raw.githubusercontent.com/{owner}/{repo}/{branch}/{path}
|
||||
* 失败返回 ['ok'=>false,'msg'=>..],调用方回退 @gh: 占位。
|
||||
*/
|
||||
|
||||
/** 服务端是否装了 git(git --version 能跑) */
|
||||
function ghGitAvailable() {
|
||||
static $cached = null;
|
||||
if ($cached !== null) return $cached;
|
||||
$cached = false;
|
||||
if (function_exists('exec')) {
|
||||
$o = []; $ret = 0;
|
||||
@exec('git --version 2>NUL', $o, $ret); // Windows: 2>NUL 吞错误
|
||||
if ($ret !== 0) @exec('git --version 2>/dev/null', $o, $ret); // Linux
|
||||
$cached = ($ret === 0 && !empty($o));
|
||||
}
|
||||
return $cached;
|
||||
}
|
||||
|
||||
function ghImgRepoPush($player, $ticketId, $title, $tmpPath, $origName) {
|
||||
$cfg = ghGetCfg();
|
||||
$full = isset($cfg['imgrepo_full']) ? $cfg['imgrepo_full'] : '';
|
||||
$owner = isset($cfg['imgrepo_owner']) ? $cfg['imgrepo_owner'] : '';
|
||||
$branch = (isset($cfg['imgrepo_branch']) && $cfg['imgrepo_branch'] !== '') ? $cfg['imgrepo_branch'] : 'main';
|
||||
$token = isset($cfg['imgrepo_token']) ? $cfg['imgrepo_token'] : ''; // ★ 15m44w:账号/key 走 img.json(真值在 .workbuddy,不进 git)
|
||||
if ($full === '' || strpos($full, '/') === false) {
|
||||
return ['ok' => false, 'url' => '', 'path' => '', 'msg' => 'imgrepo 仓库地址未配置(img.json repo.imgrepo.仓库地址)'];
|
||||
}
|
||||
if ($owner === '') $owner = explode('/', $full)[0];
|
||||
|
||||
// 仓内相对路径:{玩家名}/{工单号}_{YYYYMMDD}_{清洗标题}/{原文件名}
|
||||
$safeTitle = preg_replace('/[^A-Za-z0-9_\x{4E00}-\x{9FFF}]/u', '_', mb_substr($title, 0, 30));
|
||||
if ($safeTitle === '') $safeTitle = 'untitled';
|
||||
$safePlayer = preg_replace('/[^A-Za-z0-9_]/', '_', $player);
|
||||
$relPath = $safePlayer . '/' . ((int)$ticketId) . '_' . date('Ymd') . '_' . $safeTitle . '/' . $origName;
|
||||
$rawUrl = 'https://raw.githubusercontent.com/' . $full . '/' . $branch . '/' . $relPath;
|
||||
|
||||
// ===== 分支1:有 git 环境 → git commit + push(账号/key 用 img.json 的 PAT 拼认证 URL)=====
|
||||
if (ghGitAvailable()) {
|
||||
$commitMsg = 'ticket#' . $ticketId . ': ' . mb_substr($title, 0, 40);
|
||||
$r = ghImgRepoPushViaGit($full, $owner, $branch, $relPath, $tmpPath, $token, $commitMsg);
|
||||
if (!empty($r['ok'])) {
|
||||
ghLog('imgrepo push(via git) ok ticket=' . $ticketId . ' path=' . $relPath);
|
||||
return ['ok' => true, 'url' => $rawUrl, 'path' => $relPath, 'msg' => 'ok'];
|
||||
}
|
||||
// git 失败 → 记日志,落 Contents API 兜底(git 可能没配全局 credential)
|
||||
ghLog('imgrepo git push failed ticket=' . $ticketId . ': ' . $r['msg'] . ' → fallback Contents API');
|
||||
}
|
||||
|
||||
// ===== 分支2:无 git(或 git 失败)→ GitHub Contents API(token 来自 img.json)=====
|
||||
if ($token === '') {
|
||||
return ['ok' => false, 'url' => '', 'path' => '', 'msg' => 'img.json repo.imgrepo 的 key/登录密码 未配置(git 与 Contents API 都需要 PAT)'];
|
||||
}
|
||||
$r2 = ghImgRepoPushViaContents($full, $branch, $relPath, $tmpPath, $token, 'ticket#' . $ticketId . ': ' . mb_substr($title, 0, 40));
|
||||
if (!empty($r2['ok'])) {
|
||||
ghLog('imgrepo push(via Contents API) ok ticket=' . $ticketId . ' path=' . $relPath);
|
||||
return ['ok' => true, 'url' => $rawUrl, 'path' => $relPath, 'msg' => 'ok'];
|
||||
}
|
||||
ghLog('imgrepo contents api failed ticket=' . $ticketId . ': ' . $r2['msg']);
|
||||
return ['ok' => false, 'url' => '', 'path' => '', 'msg' => $r2['msg']];
|
||||
}
|
||||
|
||||
/** git 方式推图(★ 15m44w:账号/key 走 img.json 的 PAT,拼进认证 URL,不依赖服务端全局 git 缓存) */
|
||||
function ghImgRepoPushViaGit($full, $owner, $branch, $relPath, $tmpPath, $token, $commitMsg) {
|
||||
$workRoot = __DIR__ . '/db/gh_imgrepo_work';
|
||||
if (!is_dir($workRoot)) @mkdir($workRoot, 0755, true);
|
||||
$repoDir = $workRoot . '/' . preg_replace('/[^A-Za-z0-9_.-]/', '_', $full);
|
||||
$gitBin = 'git';
|
||||
// 跑 git 命令(Windows/Linux 通用,env 注入 GIT_TERMINAL_PROMPT=0 禁交互 + 走全局 credential)
|
||||
$runGit = function($args, $cwd, $timeout = 60) use ($gitBin) {
|
||||
$cmd = $gitBin . ' ' . implode(' ', array_map(function($a) {
|
||||
if (preg_match('/[^\x20-\x7E]/', $a) || strpos($a, ' ') !== false) return '"' . str_replace('"', '\\"', $a) . '"';
|
||||
return $a;
|
||||
}, $args));
|
||||
$env = array_merge($_SERVER, ['GIT_TERMINAL_PROMPT' => '0']);
|
||||
if ($token !== '') {
|
||||
// ★ 用 img.json 的 PAT 做认证(禁交互 + 不污染全局 credential)
|
||||
$env['GIT_CONFIG_COUNT'] = '1';
|
||||
$env['GIT_CONFIG_KEY_0'] = 'http.https://github.com/.extraheader';
|
||||
$env['GIT_CONFIG_VALUE_0'] = 'Authorization: Basic ' . base64_encode($owner . ':' . $token);
|
||||
}
|
||||
$desc = [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w']];
|
||||
$proc = proc_open($cmd, $desc, $pipes, $cwd, $env);
|
||||
if (!is_resource($proc)) return ['ok' => false, 'msg' => 'proc_open 失败'];
|
||||
fclose($pipes[0]);
|
||||
$out = stream_get_contents($pipes[1]); fclose($pipes[1]);
|
||||
$err = stream_get_contents($pipes[2]); fclose($pipes[2]);
|
||||
$st = proc_close($proc);
|
||||
return ['ok' => ($st === 0), 'msg' => trim((string)$err . (string)$out)];
|
||||
};
|
||||
|
||||
$cloneUrl = ($token !== '')
|
||||
? ('https://' . $owner . ':' . $token . '@github.com/' . $full . '.git') // ★ PAT 拼认证 URL(img.json token)
|
||||
: ('https://github.com/' . $full . '.git');
|
||||
try {
|
||||
// 1) 本地仓(首次 clone,已有则 fetch)
|
||||
if (is_dir($repoDir . '/.git')) {
|
||||
$runGit(['fetch', '--all'], $repoDir, 60);
|
||||
$runGit(['checkout', '-B', $branch, 'origin/' . $branch], $repoDir, 30);
|
||||
} else {
|
||||
@mkdir(dirname($repoDir), 0755, true);
|
||||
$r = $runGit(['clone', '--depth', '1', '--branch', $branch, '--single-branch', $cloneUrl, $repoDir], $workRoot, 120);
|
||||
if (!$r['ok']) return ['ok' => false, 'msg' => 'clone 失败: ' . mb_substr($r['msg'], 0, 160)];
|
||||
$runGit(['config', 'user.name', 'caoyuan-ticket-bot'], $repoDir, 10);
|
||||
$runGit(['config', 'user.email', 'bot@caoyuan.local'], $repoDir, 10);
|
||||
}
|
||||
// 2) 写文件到仓内
|
||||
$target = $repoDir . '/' . $relPath;
|
||||
@mkdir(dirname($target), 0755, true);
|
||||
if (!@copy($tmpPath, $target)) return ['ok' => false, 'msg' => '写仓内文件失败'];
|
||||
// 3) add + commit + push(认证走 img.json PAT:clone URL 已带 owner:token,push 复用 origin)
|
||||
$runGit(['add', '--', $relPath], $repoDir, 30);
|
||||
$runGit(['commit', '-m', $commitMsg, '--allow-empty'], $repoDir, 30);
|
||||
$rp = $runGit(['push', 'origin', 'HEAD:refs/heads/' . $branch], $repoDir, 90);
|
||||
if (!$rp['ok']) {
|
||||
@unlink($target);
|
||||
return ['ok' => false, 'msg' => 'push 失败: ' . mb_substr($rp['msg'], 0, 200)];
|
||||
}
|
||||
return ['ok' => true, 'msg' => 'ok'];
|
||||
} catch (\Throwable $e) {
|
||||
return ['ok' => false, 'msg' => 'git 异常: ' . $e->getMessage()];
|
||||
}
|
||||
}
|
||||
|
||||
/** GitHub Contents API 方式推图(免 git 二进制;token 当 Authorization: Bearer) */
|
||||
function ghImgRepoPushViaContents($full, $branch, $relPath, $tmpPath, $token, $commitMsg) {
|
||||
if (!is_file($tmpPath)) return ['ok' => false, 'msg' => '临时文件不存在'];
|
||||
$content = @file_get_contents($tmpPath);
|
||||
if ($content === false) return ['ok' => false, 'msg' => '读临时文件失败'];
|
||||
$encoded = base64_encode($content);
|
||||
// 路径 URL 编码(每段单独编码,保留 /)
|
||||
$encPath = implode('/', array_map('rawurlencode', explode('/', $relPath)));
|
||||
$apiPath = '/repos/' . $full . '/contents/' . $encPath;
|
||||
// 同名文件已存在?GET 拿 sha(Contents API 更新需带 sha)
|
||||
$chk = ghApiRequest('GET', $apiPath, null, $token);
|
||||
$payload = [
|
||||
'message' => $commitMsg,
|
||||
'content' => $encoded,
|
||||
'branch' => $branch,
|
||||
];
|
||||
if (!empty($chk['ok']) && is_array($chk['data']) && isset($chk['data']['sha'])) {
|
||||
$payload['sha'] = $chk['data']['sha'];
|
||||
}
|
||||
$r = ghApiRequest('PUT', $apiPath, $payload, $token);
|
||||
if (!$r['ok']) return ['ok' => false, 'msg' => $r['msg']];
|
||||
return ['ok' => true, 'msg' => 'ok'];
|
||||
}
|
||||
|
||||
/** 写诊断日志(1 天自动清理) */
|
||||
function ghLog($msg) {
|
||||
try {
|
||||
|
||||
@@ -21,6 +21,33 @@
|
||||
"驳回": "已驳回",
|
||||
"已回复": "已回复",
|
||||
"撤销": "已撤销"
|
||||
},
|
||||
"imgrepo": {
|
||||
"模式": true,
|
||||
"用户名": "username",
|
||||
"仓库地址": "https://github.com/username/reponame.git",
|
||||
"key/登录密码": "userkey",
|
||||
"分支": "main",
|
||||
"说明": [
|
||||
"模式=true,走专门的图片仓库,模式=false用图床",
|
||||
"同样支持:图床/img/tuchuang/GitHub/gh/GH/github",
|
||||
"温馨提示,GitHub虽然没有明确限制仓库大小",
|
||||
"但保险起见,建议每隔一段时间删仓重建",
|
||||
"避免被GitHub风控约谈,因为github本身不允许把github当网盘/图床等存储空间"
|
||||
],
|
||||
"如何安装git": {
|
||||
"自动安装": [
|
||||
"第一步打开宝塔或任意agent终端",
|
||||
"第二步充值AI点数或自备大模型",
|
||||
"第三步发提示词给他",
|
||||
"提示词:xx你好,请你读取系统环境,帮我配置并安装git",
|
||||
"第四步验证,终端输入git --version,有输出版本号就成功"
|
||||
],
|
||||
"手动安装": {
|
||||
"centos和其变种": "sudo dnf install git-all",
|
||||
"debian和其变种": "sudo apt install git-all"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"图像/附件支持": {
|
||||
|
||||
@@ -74,6 +74,13 @@ function loadImgJson() {
|
||||
'撤销' => '已撤销',
|
||||
'处理中' => '处理中',
|
||||
],
|
||||
// ★ 15m44w: GitHub 图片仓(imgrepo)配置——附件走 git 推图仓拿 raw URL,省服务器内存
|
||||
// (用户定调:GitHub 模式本就是为省内存才做的,附件不走图床 API 而走 git 推仓)
|
||||
'imgrepo_mode' => 'gh', // 'gh'=git仓模式 | 'img'=图床模式(归一化后)
|
||||
'imgrepo_owner' => '', // 用户名(owner)
|
||||
'imgrepo_full' => '', // owner/repo(从仓库地址提取)
|
||||
'imgrepo_branch' => 'main', // 分支
|
||||
'imgrepo_token' => '', // key/登录密码(当 PAT 用)
|
||||
];
|
||||
$file = __DIR__ . '/img.json';
|
||||
if (!is_readable($file)) return $cfg;
|
||||
@@ -143,6 +150,46 @@ function loadImgJson() {
|
||||
if ($k !== '') $cfg['gh_label_map'][$k] = trim((string)$v);
|
||||
}
|
||||
}
|
||||
// ★ 15m44w: 读 repo.imgrepo(GitHub 图片仓)—— 附件走 git 推图仓拿 raw URL,省服务器内存。
|
||||
// 归一化判断 模式 字段:true / gh / GH / GitHub / github / GitHub-issue(不区分大小写)
|
||||
// → git仓模式(imgrepo_mode='gh');false / img / 图床 / tuchuang → 图床模式('img')。
|
||||
if (isset($j['repo']['imgrepo']) && is_array($j['repo']['imgrepo'])) {
|
||||
$ir = $j['repo']['imgrepo'];
|
||||
$modeRaw = $ir['模式'] ?? null;
|
||||
// 归一化:布尔 true→'gh',false→'img';字符串去空格/小写/去 -_[],
|
||||
// 含 github/gh 或等于 gh 算 git仓;含 img/图床/tuchuang/lan/lsky 算图床;缺省按 true 处理
|
||||
$modeNorm = '';
|
||||
if (is_bool($modeRaw)) {
|
||||
$modeNorm = $modeRaw ? 'gh' : 'img';
|
||||
} else {
|
||||
$modeNorm = strtolower(trim((string)$modeRaw));
|
||||
$modeNorm = str_replace([' ', '-', '_', '[', ']'], '', $modeNorm);
|
||||
}
|
||||
if ($modeNorm === 'gh' || $modeNorm === 'github' || strpos($modeNorm, 'github') === 0 || $modeNorm === 'true') {
|
||||
$cfg['imgrepo_mode'] = 'gh';
|
||||
} elseif ($modeNorm === 'img' || $modeNorm === '图床' || $modeNorm === 'tuchuang' || $modeNorm === 'lan' || $modeNorm === 'lsky' || $modeNorm === 'false' || $modeNorm === '') {
|
||||
$cfg['imgrepo_mode'] = 'img';
|
||||
} else {
|
||||
// 未知值按 git仓模式(用户填 GitHub 变体多)
|
||||
$cfg['imgrepo_mode'] = 'gh';
|
||||
}
|
||||
$cfg['imgrepo_owner'] = trim((string)($ir['用户名'] ?? ''));
|
||||
$cfg['imgrepo_token'] = trim((string)($ir['key/登录密码'] ?? ''));
|
||||
$cfg['imgrepo_branch'] = trim((string)($ir['分支'] ?? 'main'));
|
||||
if ($cfg['imgrepo_branch'] === '') $cfg['imgrepo_branch'] = 'main';
|
||||
// 仓库地址 https://github.com/owner/name[.git] → 提取 owner/name
|
||||
$irUrl = trim((string)($ir['仓库地址'] ?? ''));
|
||||
$irPath = preg_replace('#^https?://#i', '', $irUrl);
|
||||
$irPath = preg_replace('#^github\.com/#i', '', $irPath);
|
||||
$irPath = preg_replace('#\.git$#i', '', $irPath);
|
||||
$irPath = trim($irPath, '/');
|
||||
$cfg['imgrepo_full'] = (strpos($irPath, '/') !== false) ? $irPath : '';
|
||||
// 仓库地址有但 owner 没填 → 从仓库地址补 owner
|
||||
if ($cfg['imgrepo_owner'] === '' && $cfg['imgrepo_full'] !== '') {
|
||||
$parts = explode('/', $cfg['imgrepo_full']);
|
||||
$cfg['imgrepo_owner'] = $parts[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!isset($j['repo']['webhook_secret'])) {
|
||||
// 兼容旧顶层 webhook_secret(已部署的生产 img.json 可能是旧结构)
|
||||
|
||||
Reference in New Issue
Block a user