feat(storage): browse SMB shares and bound connection checks

This commit is contained in:
阿白叔 committed 2026-10-17 17:47:22 +08:00
1 parent 8417b1dff8
commit a785fa73d4
15 files changed
+277 -39

No files matched your search

+24 -6
View File
@@ -4,7 +4,7 @@ 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.pendingChecks=new Set();this.stopped=false;}
constructor({store,jobs,outputDir,client=new SmbClient(),checkTimeoutMs=15000}){this.store=store;this.jobs=jobs;this.outputDir=outputDir;this.client=client;this.running=new Map();this.pendingChecks=new Set();this.probeControllers=new Set();this.checkTimeoutMs=checkTimeoutMs;this.stopped=false;}
busy(id){return this.running.has(id);}
async saveServer(input){
if(this.stopped)throw fail('存储服务正在停止。',503);
@@ -15,11 +15,29 @@ class StorageService{
if(snapshot.id&&this.busy(snapshot.id))throw fail('此服务器正在执行操作,请稍后保存。',409);
return this.store.saveServer(snapshot,readState);
}
async checkConnection(input){
async probe(saved,{signal}={}){
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);}
const controller=new AbortController();const cancel=()=>controller.abort(signal.reason?.status?signal.reason:fail('已取消连接操作。',499));
if(signal?.aborted)cancel();else signal?.addEventListener('abort',cancel,{once:true});
const timer=setTimeout(()=>controller.abort(fail('连接超时,请检查服务器地址、网络及共享权限后重试。',504)),this.checkTimeoutMs);
const task=(async()=>{let onAbort,work;
try{return await Promise.race([
new Promise((resolve,reject)=>{onAbort=()=>reject(controller.signal.reason);if(controller.signal.aborted)onAbort();else controller.signal.addEventListener('abort',onAbort,{once:true});}),
work=(async()=>{if(controller.signal.aborted)throw controller.signal.reason;
if(this.client.browse)return await this.client.browse(saved,{signal:controller.signal});
const area=await this.client.area(saved),listing=await area.list('');return {serverRoot:false,readable:true,entries:listing.entries.filter(e=>e.type==='directory'),truncated:listing.truncated};
})()
]);}catch(error){throw safe(error);}finally{if(controller.signal.aborted&&this.client.browse&&work){let cleanup;await Promise.race([work.catch(()=>{}),new Promise(resolve=>{cleanup=setTimeout(resolve,1800);})]);clearTimeout(cleanup);}clearTimeout(timer);signal?.removeEventListener('abort',cancel);if(onAbort)controller.signal.removeEventListener('abort',onAbort);this.probeControllers.delete(controller);}
})();
this.pendingChecks.add(task);this.probeControllers.add(controller);try{return await task;}finally{this.pendingChecks.delete(task);this.probeControllers.delete(controller);}
}
async browseConnection(input,options){
const {saved}=this.store.prepareServer(input,{allowServerRoot:true});return this.probe(saved,options);
}
async checkConnection(input,options){
const {saved}=this.store.prepareServer(input,{allowServerRoot:true});const result=await this.probe(saved,options);
if(!saved.share)return {...result,readable:false,serverRoot:true,message:'已连接服务器,请浏览并选择一个共享目录。'};
return {...result,readable:true,checkedAt:new Date().toISOString(),message:'连接成功,目标目录可读取。'};
}
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){
@@ -45,6 +63,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(),...this.pendingChecks]);}
async stop(){this.stopped=true;for(const controller of this.probeControllers)controller.abort(fail('存储服务正在停止。',503));await Promise.allSettled([...this.running.values(),...this.pendingChecks]);}
}
module.exports={StorageService};