feat(storage): browse SMB shares and bound connection checks
This commit is contained in:
1 parent
8417b1dff8
commit
a785fa73d4
15 files changed
+277
-39
No files matched your search
@@ -0,0 +1,20 @@
|
||||
'use strict';
|
||||
const {SmbClient,segments}=require('./storage-files.cjs');
|
||||
const native=require('./smb-native.cjs');
|
||||
let input='';process.stdin.setEncoding('utf8');process.stdin.on('data',chunk=>{input+=chunk;if(input.length>32768)process.exit(1);});
|
||||
process.stdin.on('end',async()=>{
|
||||
try{
|
||||
const server=JSON.parse(input),slash=String.fromCharCode(92),root=slash.repeat(2)+server.host;
|
||||
let value;
|
||||
if(!server.share){const names=await native.listShares(server);const entries=[];
|
||||
for(const name of names){try{if(segments(name).length===1)entries.push({name,type:'directory',address:root+slash+name});}catch{}}
|
||||
value={serverRoot:true,readable:false,address:root,parent:null,entries:entries.slice(0,500),truncated:entries.length>500};
|
||||
}else{
|
||||
const area=await new SmbClient().area(server),listing=await area.list(''),parts=[server.share,...segments(server.directory)],address=root+slash+parts.join(slash);
|
||||
value={serverRoot:false,readable:true,address,parent:parts.length===1?root:root+slash+parts.slice(0,-1).join(slash),entries:listing.entries.filter(entry=>entry.type==='directory').map(entry=>({...entry,address:address+slash+entry.name})),truncated:listing.truncated};
|
||||
}
|
||||
process.stdout.write(JSON.stringify({ok:true,value}));
|
||||
}catch(error){const messages={ENOENT:'找不到目标目录,请检查共享路径。',EACCES:'没有共享目录读取权限,请检查账号。',EPERM:'没有共享目录读取权限,请检查账号。',ENOTDIR:'目标路径不是目录。'};
|
||||
process.stdout.write(JSON.stringify({ok:false,status:error.status||502,error:error.status?error.message:messages[error.code]||'读取共享目录失败,请检查服务器、网络与访问权限。'}));
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user