Spaces:
Running
Running
Surf commited on
Commit ·
bb41201
1
Parent(s): b787312
复用主程序 R2 适配器保存小说项目
Browse files- fanqie/projects.js +11 -0
- server.js +2 -0
fanqie/projects.js
CHANGED
|
@@ -87,18 +87,29 @@ async function request(method, key, body = null, contentType = '') {
|
|
| 87 |
throw new Error('R2 request failed')
|
| 88 |
}
|
| 89 |
|
|
|
|
| 90 |
async function readJson(key, fallback = null) {
|
|
|
|
|
|
|
| 91 |
const response = await request('GET', key)
|
| 92 |
if (response.status === 404) return fallback
|
| 93 |
if (!response.ok) throw new HttpError(502, `R2 读取失败:${response.status}`)
|
| 94 |
try { return await response.json() } catch { throw new HttpError(502, 'R2 返回的数据不是合法 JSON') }
|
| 95 |
}
|
| 96 |
async function writeJson(key, data) {
|
|
|
|
|
|
|
| 97 |
const response = await request('PUT', key, JSON.stringify(data), 'application/json')
|
| 98 |
if (!response.ok) throw new HttpError(502, `R2 保存失败:${response.status}`)
|
| 99 |
return data
|
| 100 |
}
|
| 101 |
async function remove(key) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
const response = await request('DELETE', key)
|
| 103 |
if (!response.ok && response.status !== 404) throw new HttpError(502, `R2 删除失败:${response.status}`)
|
| 104 |
}
|
|
|
|
| 87 |
throw new Error('R2 request failed')
|
| 88 |
}
|
| 89 |
|
| 90 |
+
function serverCloud() { return globalThis.__xiaoshuoCloud || null }
|
| 91 |
async function readJson(key, fallback = null) {
|
| 92 |
+
const cloud = serverCloud()
|
| 93 |
+
if (cloud) return cloud.getJSON(key, fallback)
|
| 94 |
const response = await request('GET', key)
|
| 95 |
if (response.status === 404) return fallback
|
| 96 |
if (!response.ok) throw new HttpError(502, `R2 读取失败:${response.status}`)
|
| 97 |
try { return await response.json() } catch { throw new HttpError(502, 'R2 返回的数据不是合法 JSON') }
|
| 98 |
}
|
| 99 |
async function writeJson(key, data) {
|
| 100 |
+
const cloud = serverCloud()
|
| 101 |
+
if (cloud) return cloud.putJSON(key, data)
|
| 102 |
const response = await request('PUT', key, JSON.stringify(data), 'application/json')
|
| 103 |
if (!response.ok) throw new HttpError(502, `R2 保存失败:${response.status}`)
|
| 104 |
return data
|
| 105 |
}
|
| 106 |
async function remove(key) {
|
| 107 |
+
const cloud = serverCloud()
|
| 108 |
+
if (cloud) {
|
| 109 |
+
const response = await cloud.r2('DELETE', key)
|
| 110 |
+
if (!response.ok && response.status !== 404) throw new HttpError(502, `R2 删除失败:${response.status}`)
|
| 111 |
+
return
|
| 112 |
+
}
|
| 113 |
const response = await request('DELETE', key)
|
| 114 |
if (!response.ok && response.status !== 404) throw new HttpError(502, `R2 删除失败:${response.status}`)
|
| 115 |
}
|
server.js
CHANGED
|
@@ -84255,6 +84255,8 @@ app.get("/api/clean/materials", async (req, res) => {
|
|
| 84255 |
});
|
| 84256 |
/* ===== [新增结束] ===== */
|
| 84257 |
/* ===== [新增] 番茄写作工作台(/fanqie/)—— 素材接口与本站共用访问密码,不影响原有任何功能 ===== */
|
|
|
|
|
|
|
| 84258 |
try { require("./fanqie/mount.js")(app, passwordOk, import_express.default); } catch (e) { console.error("fanqie mount failed:", e); }
|
| 84259 |
/* ===== [新增结束] ===== */
|
| 84260 |
app.all("/api/*", async (req, res) => {
|
|
|
|
| 84255 |
});
|
| 84256 |
/* ===== [新增结束] ===== */
|
| 84257 |
/* ===== [新增] 番茄写作工作台(/fanqie/)—— 素材接口与本站共用访问密码,不影响原有任何功能 ===== */
|
| 84258 |
+
// 将主程序已经验证可用的 R2 适配器提供给 Fanqie 项目存储层,避免重复实现 SigV4。
|
| 84259 |
+
globalThis.__xiaoshuoCloud = { getJSON, putJSON, r2 };
|
| 84260 |
try { require("./fanqie/mount.js")(app, passwordOk, import_express.default); } catch (e) { console.error("fanqie mount failed:", e); }
|
| 84261 |
/* ===== [新增结束] ===== */
|
| 84262 |
app.all("/api/*", async (req, res) => {
|