feat(storage): require readable connections before adding servers
This commit is contained in:
1 parent
b34d51432d
commit
8417b1dff8
11 files changed
+100
-20
No files matched your search
+3
-2
@@ -7,10 +7,11 @@ async function storageApi({storage,workspace,user,url,route,method,body,request,
|
||||
const audit=(action,detail)=>workspace?.record(user,action,detail);
|
||||
if(route==='/api/storage'&&method==='GET'){reply(200,{servers:storage.store.servers(),settings:storage.store.settings(),archives:storage.store.archives()});return true;}
|
||||
if(user.role==='viewer')fail('仅管理员和操作员可以操作归档文件。',403);
|
||||
if(route==='/api/storage/servers'&&method==='POST'){
|
||||
if((route==='/api/storage/servers'||route==='/api/storage/servers/check')&&method==='POST'){
|
||||
if(user.role!=='admin')fail('仅管理员可以维护存储服务器。',403);
|
||||
if(!secureBpmTransport(request,trustedProxy))fail('请在部署机本地或 HTTPS 页面维护存储账号。',403);
|
||||
const saved=storage.saveServer(body);audit('保存存储服务器',saved.name);reply(body.id?200:201,saved);return true;
|
||||
if(route.endsWith('/check')){reply(200,await storage.checkConnection(body));return true;}
|
||||
const saved=await storage.saveServer(body);audit('保存存储服务器',saved.name);reply(body.id?200:201,saved);return true;
|
||||
}
|
||||
if(route==='/api/storage/settings'&&method==='POST'){if(user.role!=='admin')fail('仅管理员可以修改自动归档设置。',403);payload(body,['serverId','enabled']);const saved=storage.saveSettings(body);audit('保存自动归档设置',saved.enabled?'已启用':'已暂停');reply(200,saved);return true;}
|
||||
const match=/^\/api\/storage\/servers\/([\w-]+)\/(check|files|folders|archive|file|preview-info|content)$/.exec(route);
|
||||
|
||||
+17
-3
@@ -4,9 +4,23 @@ const {SmbClient,segments,inside}=require('./storage-files.cjs');
|
||||
const fail=(message,status=400)=>Object.assign(new Error(message),{status});
|
||||
const safe=e=>e.status?e:fail(({ENOENT:'找不到文件或归档目录,请检查路径。',EACCES:'没有共享目录访问权限。',EPERM:'没有共享目录操作权限。',ENOSPC:'存储服务器空间不足。',ENOTDIR:'指定路径不是目录。'})[e.code]||'存储操作未完成,请检查服务器与网络。',502);
|
||||
class StorageService{
|
||||
constructor({store,jobs,outputDir,client=new SmbClient()}){this.store=store;this.jobs=jobs;this.outputDir=outputDir;this.client=client;this.running=new Map();this.stopped=false;}
|
||||
constructor({store,jobs,outputDir,client=new SmbClient()}){this.store=store;this.jobs=jobs;this.outputDir=outputDir;this.client=client;this.running=new Map();this.pendingChecks=new Set();this.stopped=false;}
|
||||
busy(id){return this.running.has(id);}
|
||||
saveServer(input){if(input?.id&&this.busy(input.id))throw fail('此服务器正在执行操作,请稍后保存。',409);return this.store.saveServer(input);}
|
||||
async saveServer(input){
|
||||
if(this.stopped)throw fail('存储服务正在停止。',503);
|
||||
const snapshot=structuredClone(input),{changed}=this.store.prepareServer(snapshot);
|
||||
if(snapshot.id&&this.busy(snapshot.id))throw fail('此服务器正在执行操作,请稍后保存。',409);
|
||||
const readState=changed?await this.checkConnection(snapshot):null;
|
||||
if(this.stopped)throw fail('存储服务正在停止。',503);
|
||||
if(snapshot.id&&this.busy(snapshot.id))throw fail('此服务器正在执行操作,请稍后保存。',409);
|
||||
return this.store.saveServer(snapshot,readState);
|
||||
}
|
||||
async checkConnection(input){
|
||||
if(this.stopped)throw fail('存储服务正在停止。',503);
|
||||
const {saved}=this.store.prepareServer(input);
|
||||
const task=(async()=>{try{const area=await this.client.area(saved);await area.list('');return {readable:true,checkedAt:new Date().toISOString(),message:'连接成功,目标目录可读取。'};}catch(error){throw safe(error);}})();
|
||||
this.pendingChecks.add(task);try{return await task;}finally{this.pendingChecks.delete(task);}
|
||||
}
|
||||
saveSettings(input){if(input?.enabled){const server=this.store.publicServer(input.serverId);if(!server.enabled||!server.state.readable||!server.state.writable)throw fail('请先启用服务器并检查读写成功。',409);}return this.store.saveSettings(input);}
|
||||
run(id,work){
|
||||
if(this.stopped)return Promise.reject(fail('存储服务正在停止。',503));
|
||||
@@ -31,6 +45,6 @@ class StorageService{
|
||||
});
|
||||
}
|
||||
async automatic(job){const settings=this.store.settings();if(!settings.enabled||job.status!=='success'||!job.createdAt||job.createdAt<settings.since)return null;return this.archive(settings.serverId,job.jobId,'','自动归档');}
|
||||
async stop(){this.stopped=true;await Promise.allSettled([...this.running.values()]);}
|
||||
async stop(){this.stopped=true;await Promise.allSettled([...this.running.values(),...this.pendingChecks]);}
|
||||
}
|
||||
module.exports={StorageService};
|
||||
@@ -22,7 +22,7 @@ class StorageStore{
|
||||
secret(id){const row=this.row(id);try{const saved=this.vault.decode(id,row.payload);return {authMode:'credentials',passwordless:false,...saved,id,version:row.version,state:JSON.parse(row.state)};}catch{fail('存储凭据无法解密,请联系管理员。',503);}}
|
||||
publicServer(id){const {password,...server}=this.secret(id);return {...server,hasPassword:!!password};}
|
||||
servers(){return this.db.prepare('SELECT id FROM servers ORDER BY rowid').all().map(r=>this.publicServer(r.id));}
|
||||
saveServer(input){
|
||||
prepareServer(input){
|
||||
const fields=['id','version','name','host','share','directory','username','password','enabled','state','hasPassword','createdAt','updatedAt','address','authMode','passwordless'];
|
||||
if(!input||typeof input!=='object'||Array.isArray(input)||Object.keys(input).some(k=>!fields.includes(k)))fail('存储服务器配置无效。');
|
||||
const old=input.id?this.secret(input.id):null;if(old&&input.version!==old.version)fail('服务器已被修改,请刷新后重试。',409);
|
||||
@@ -53,9 +53,13 @@ class StorageStore{
|
||||
if(typeof input.enabled!=='boolean')fail('请选择服务器是否启用。');
|
||||
const name=input.name===undefined||input.name===''?(host+' / '+share).slice(0,80):text(input.name,'服务器名称',80);
|
||||
const id=old?.id||crypto.randomUUID(),saved={name,host,share,directory,authMode,passwordless,username,password,enabled:input.enabled,createdAt:old?.createdAt||now(),updatedAt:now()};
|
||||
const changed=!old||['host','share','directory','authMode','username','password'].some(k=>saved[k]!==old[k])||(!old.enabled&&saved.enabled);
|
||||
return {id,saved,old,changed};
|
||||
}
|
||||
saveServer(input,readState){
|
||||
const {id,saved,old,changed}=this.prepareServer(input);
|
||||
let payload;try{payload=this.vault.encode(id,saved);}catch{fail('存储凭据加密不可用,请联系管理员。',503);}
|
||||
const changed=!old||['host','share','directory','authMode','username','password'].some(k=>saved[k]!==old[k]);
|
||||
const state=changed?{checkedAt:null,readable:false,writable:false,message:'待检查连接'}:old.state;
|
||||
const state=changed?(readState?{checkedAt:readState.checkedAt,readable:true,writable:false,message:'目录可读取,写入权限尚未检查'}:{checkedAt:null,readable:false,writable:false,message:'待检查连接'}):old.state;
|
||||
this.db.prepare('INSERT INTO servers VALUES(?,?,?,?) ON CONFLICT(id) DO UPDATE SET version=excluded.version,payload=excluded.payload,state=excluded.state').run(id,(old?.version||0)+1,payload,JSON.stringify(state));
|
||||
if(old&&(!saved.enabled||changed)&&this.settings().serverId===id)this.saveSettings({serverId:id,enabled:false});
|
||||
return this.publicServer(id);
|
||||
|
||||
Reference in new issue
Block a user