fix: lsky gateway reads links.url + strategy_id support (COS strategy verified end-to-end: https cos url, public access ok)
This commit is contained in:
@@ -6,9 +6,16 @@ import zlib from 'node:zlib';
|
||||
// 图床网关:Lsky Pro(已对接 COS),附件直传对象存储,应用服务器不做搬运工
|
||||
const LSKY_API = (process.env.LSKY_API || '').replace(/\/$/, '');
|
||||
const LSKY_TOKEN = process.env.LSKY_TOKEN || '';
|
||||
const LSKY_STRATEGY_ID = process.env.LSKY_STRATEGY_ID || ''; // 存储策略(如 2=COS),不填用默认策略
|
||||
function sanitizeImageUrl(u) {
|
||||
// 裸 IP:port 的 http 地址在 HTTPS 站点会被拦截;域名地址统一切 https
|
||||
if (!u) return u;
|
||||
return /^http:\/\/\d+\./.test(u) ? u : u.replace(/^http:\/\//, 'https://');
|
||||
}
|
||||
async function lskyUpload(buf, fileName, mime) {
|
||||
const fd = new FormData();
|
||||
fd.append('file', new Blob([buf], { type: mime || 'application/octet-stream' }), fileName);
|
||||
if (LSKY_STRATEGY_ID) fd.append('strategy_id', LSKY_STRATEGY_ID);
|
||||
const r = await fetch(LSKY_API + '/upload', {
|
||||
method: 'POST',
|
||||
headers: { authorization: 'Bearer ' + LSKY_TOKEN, accept: 'application/json' },
|
||||
@@ -16,8 +23,9 @@ async function lskyUpload(buf, fileName, mime) {
|
||||
signal: AbortSignal.timeout(60_000),
|
||||
});
|
||||
const j = await r.json().catch(() => null);
|
||||
if (!j || j.status !== true || !j.data?.url) throw new Error('LSKY ' + r.status + ': ' + (j?.message || 'no url'));
|
||||
return j.data;
|
||||
const url = sanitizeImageUrl(j?.data?.links?.url || j?.data?.url);
|
||||
if (!j || j.status !== true || !url) throw new Error('LSKY ' + r.status + ': ' + (j?.message || 'no url'));
|
||||
return { url, thumb_url: sanitizeImageUrl(j?.data?.thumbnail_url) || undefined, key: j.data.key };
|
||||
}
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
Reference in New Issue
Block a user