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
+25
-6
@@ -16,16 +16,35 @@
|
||||
<div class="mb-3"><label class="form-label" for="storage-auth">访问方式</label><select class="form-select" id="storage-auth"><option value="windows" ${mode==='windows'?'selected':''}>免填账号密码(使用 Windows 权限)</option><option value="credentials" ${mode==='credentials'?'selected':''}>指定账号</option></select><div class="form-hint">免填模式复用部署机已有 Windows 共享登录或服务账号权限,也适用于系统允许的免密共享。切换方式不会断开系统已有连接。</div></div>
|
||||
<fieldset id="storage-credentials" class="row g-3 mb-3"><div class="col-md-6"><label class="form-label" for="storage-username">登录账号</label><input class="form-control" id="storage-username" value="${esc(s.username)}" maxlength="200" required autocomplete="off"></div><div class="col-md-6"><label class="form-label" for="storage-password">SMB 密码</label><input class="form-control" type="password" id="storage-password" maxlength="2048" autocomplete="new-password"><div class="form-hint">${s.id&&s.hasPassword?'留空保留现有密码;更换服务器、共享或账号时重新填写。':'密码加密保存在部署机。'}</div></div><div class="col-12"><label class="form-check"><input class="form-check-input" type="checkbox" id="storage-passwordless" ${s.passwordless?'checked':''}><span class="form-check-label">此账号无密码</span></label></div></fieldset>
|
||||
<div class="mb-3"><label class="form-label" for="storage-name">服务器名称</label><input class="form-control" id="storage-name" value="${esc(s.name)}" maxlength="80" placeholder="可选,留空自动生成" autocomplete="off"></div>
|
||||
<label class="form-check"><input class="form-check-input" type="checkbox" id="storage-active" ${s.enabled?'checked':''}><span class="form-check-label">启用服务器</span></label><div class="text-danger mt-3" id="storage-error" role="alert"></div></div><div class="card-footer btn-list"><button class="btn" type="button" id="storage-cancel">取消编辑</button><button class="btn btn-primary ms-auto" type="submit">保存服务器</button></div></form>`;
|
||||
<label class="form-check"><input class="form-check-input" type="checkbox" id="storage-active" ${s.enabled?'checked':''}><span class="form-check-label">启用服务器</span></label><div class="text-secondary mt-3" id="storage-connection-status" role="status">${s.id?'修改连接信息后需重新检查。':'请先检查连接,确认目录可读取后再保存。'}</div><div class="text-danger mt-3" id="storage-error" role="alert"></div></div><div class="card-footer btn-list"><button class="btn" type="button" id="storage-cancel">取消编辑</button><button class="btn ms-auto" type="button" id="storage-connection-check">检查连接</button><button class="btn btn-primary" type="submit" ${s.id?'':'disabled'}>保存服务器</button></div></form>`;
|
||||
const auth=host.querySelector('#storage-auth'),credentials=host.querySelector('#storage-credentials'),passwordless=host.querySelector('#storage-passwordless'),password=host.querySelector('#storage-password');
|
||||
function syncAuth(){const custom=auth.value==='credentials';credentials.hidden=!custom;credentials.disabled=!custom;password.disabled=!custom||passwordless.checked;password.required=custom&&!passwordless.checked&&(!s.hasPassword||mode!=='credentials');}
|
||||
auth.onchange=syncAuth;passwordless.onchange=syncAuth;syncAuth();
|
||||
host.querySelector('#storage-cancel').onclick=()=>host.replaceChildren();
|
||||
host.querySelector('form').onsubmit=async e=>{e.preventDefault();await action(e.submitter,async()=>{
|
||||
const form=host.querySelector('form'),checkButton=host.querySelector('#storage-connection-check'),saveButton=form.querySelector('[type="submit"]'),status=host.querySelector('#storage-connection-status'),errorBox=host.querySelector('#storage-error');
|
||||
let verified=!!s.id,checking=false,saving=false,revision=0;
|
||||
const alive=()=>isActive()&&form.isConnected;
|
||||
const update=()=>{saveButton.disabled=!verified||checking||saving;checkButton.disabled=checking||saving;};
|
||||
function details(){
|
||||
const input={...(s.id?{id:s.id,version:s.version}:{}),address:host.querySelector('#storage-address').value,name:host.querySelector('#storage-name').value,authMode:auth.value,enabled:host.querySelector('#storage-active').checked};
|
||||
if(auth.value==='credentials')Object.assign(input,{username:host.querySelector('#storage-username').value,password:passwordless.checked?'':password.value,passwordless:passwordless.checked});
|
||||
try{await api('/storage/servers',input);if(isActive()){notify('存储服务器已保存。');await render(container,isActive);}}catch(error){if(isActive())host.querySelector('#storage-error').textContent=error.message;}
|
||||
});};host.scrollIntoView({block:'start',behavior:'smooth'});
|
||||
if(auth.value==='credentials')Object.assign(input,{username:host.querySelector('#storage-username').value,password:passwordless.checked?'':password.value,passwordless:passwordless.checked});return input;
|
||||
}
|
||||
function invalidate(event){if(!['storage-address','storage-auth','storage-username','storage-password','storage-passwordless'].includes(event.target.id)&&!(event.target.id==='storage-active'&&!s.enabled&&event.target.checked))return;revision++;verified=false;errorBox.textContent='';status.textContent='连接信息已变更,请重新检查。';update();}
|
||||
form.addEventListener('input',invalidate);form.addEventListener('change',invalidate);
|
||||
checkButton.onclick=async()=>{
|
||||
if(checking||saving||!form.reportValidity())return;
|
||||
const at=revision;verified=false;checking=true;errorBox.textContent='';status.textContent='正在检查连接…';update();
|
||||
try{const result=await api('/storage/servers/check',details());if(alive()&&at===revision){verified=result.readable===true;status.textContent=result.message;}}
|
||||
catch(error){if(alive()&&at===revision){errorBox.textContent=error.message;status.textContent='连接检查失败,尚未保存。';}}
|
||||
finally{checking=false;if(alive())update();}
|
||||
};
|
||||
host.querySelector('#storage-cancel').onclick=()=>host.replaceChildren();
|
||||
form.onsubmit=async e=>{
|
||||
e.preventDefault();if(saving||checking)return;if(!verified){errorBox.textContent='请先检查连接,确认目录可读取后再保存。';return;}
|
||||
const input=details();saving=true;errorBox.textContent='';form.querySelectorAll('input,select,button').forEach(node=>node.disabled=true);
|
||||
try{await api('/storage/servers',input);if(alive()){notify('存储服务器已保存。');await render(container,isActive);}}
|
||||
catch(error){if(alive()){verified=false;errorBox.textContent=error.message;status.textContent='保存失败,请重新检查连接后重试。';}}
|
||||
finally{saving=false;if(alive()){form.querySelectorAll('input,select,button').forEach(node=>node.disabled=false);syncAuth();update();}}
|
||||
};update();host.scrollIntoView({block:'start',behavior:'smooth'});
|
||||
}
|
||||
if(admin){container.querySelector('#storage-add').onclick=()=>editor();container.querySelectorAll('[data-storage-edit]').forEach(b=>b.onclick=()=>editor(data.servers.find(s=>s.id===b.dataset.storageEdit)));container.querySelectorAll('[data-storage-check]').forEach(b=>b.onclick=()=>action(b,async()=>{await api('/storage/servers/'+b.dataset.storageCheck+'/check',{});if(isActive()){notify('存储服务器读写检查成功。');await render(container,isActive);}}));container.querySelector('#storage-auto').onsubmit=e=>{e.preventDefault();return action(e.submitter,async()=>{await api('/storage/settings',{serverId:container.querySelector('#storage-default').value,enabled:container.querySelector('#storage-enabled').checked});if(isActive()){notify('自动归档设置已保存。');await render(container,isActive);}});};}
|
||||
let preview;
|
||||
|
||||
Reference in new issue
Block a user