This commit is contained in:
@@ -54,38 +54,36 @@ def refresh_pp():
|
||||
emit('building',78,'本地内容清单暂不可用,将记录为新资料待校验')
|
||||
return build_pp_live_source(inventory,manifest)
|
||||
|
||||
def dingtalk_root_key(connection_id, space_id):
|
||||
from source_catalog_import import stable_connection_id
|
||||
key='space-'+space_id
|
||||
if stable_connection_id('dingtalk-'+key)==connection_id:return key
|
||||
# Legacy identities remain authoritative for existing directory annotations.
|
||||
inventory=json.loads(pathlib.Path('/sources/dingtalk/data/dingtalk-internal/inventory-state.json').read_text())
|
||||
for key in inventory.get('roots',{}):
|
||||
if stable_connection_id('dingtalk-'+key)==connection_id:return key
|
||||
raise RuntimeError('DingTalk source identity cannot be resolved safely')
|
||||
|
||||
def refresh_dingtalk(name,connection_id=''):
|
||||
root=pathlib.Path('/sources/dingtalk/data/dingtalk-internal/inventory-state.json');state=json.loads(root.read_text())
|
||||
# Prefer the root registered on the connection row (spaces/<root>?space=):
|
||||
# name matching is the legacy fallback for the imported sources.
|
||||
root_id=''
|
||||
if connection_id and re.fullmatch(r'[A-Za-z0-9_.:-]{1,36}',connection_id):
|
||||
try:
|
||||
rows=subprocess.run(['psql','-X','-At','-d',os.environ.get('DATABASE_URL',''),'-c',"SELECT base_url FROM source_connections WHERE id='"+connection_id+"'"],capture_output=True,text=True,timeout=30)
|
||||
locator=rows.stdout.strip() if rows.returncode==0 else ''
|
||||
if locator.startswith('spaces/'):root_id=locator.split('?',1)[0][len('spaces/'):]
|
||||
except Exception:root_id=''
|
||||
matches=[(key,value) for key,value in state['roots'].items() if (root_id and value.get('configured_root_id')==root_id) or value.get('live_space_name')==name]
|
||||
if root_id and len(matches)>1:matches=[m for m in matches if m[1].get('configured_root_id')==root_id]
|
||||
if len(matches)!=1:raise RuntimeError('DingTalk source root missing')
|
||||
key,current=matches[0];output=pathlib.Path('/tmp')/('dingtalk-'+key+'-live.json')
|
||||
if output.exists():output.unlink()
|
||||
command=['python3','/sources/dingtalk/browser_dingtalk_scan.py','--capture','/run/source-secrets/dingtalk_capture.json','--output',str(output),'--root',f"{key}={current['configured_root_id']}",'--checkpoint-every','10']
|
||||
emit('connecting',8,'正在验证钉钉专用浏览器登录')
|
||||
process=subprocess.Popen(command,stdout=subprocess.PIPE,stderr=subprocess.DEVNULL,text=True)
|
||||
assert process.stdout is not None
|
||||
last=10
|
||||
for line in process.stdout:
|
||||
if line.startswith('progress root='):
|
||||
values={part.split('=',1)[0]:part.split('=',1)[1] for part in line.strip().split() if '=' in part}
|
||||
done=int(values.get('folders','0'));pending=int(values.get('pending','0'));total=done+pending;files=int(values.get('files','0'))
|
||||
current_progress=10+(65*done//total if total else 65);last=max(last,current_progress)
|
||||
emit('scanning',last,f"正在扫描目录:已完成 {done} 个文件夹,待处理 {pending} 个",done,total,scanned_directories=done,discovered_directories=total,scanned_files=files,estimated_total_files=files,total_is_estimate=True)
|
||||
if process.wait()!=0 or not output.is_file():raise RefreshBlocked('钉钉登录会话已失效,请在来源链接重新扫码')
|
||||
payload=json.loads(output.read_text());sources=build_dingtalk_sources(payload);match=[source for source in sources if source.get('root_node_id')==current['configured_root_id'] or source['name']==name]
|
||||
if len(match)!=1:raise RuntimeError('DingTalk refreshed source missing')
|
||||
from dws_dingtalk_scan import walk
|
||||
if not re.fullmatch(r'[A-Za-z0-9_.:-]{1,36}',connection_id):raise RuntimeError('invalid source identity')
|
||||
rows=subprocess.run(['psql','-X','-At','-v','ON_ERROR_STOP=1','-d',os.environ.get('DATABASE_URL',''),'-c',"SELECT base_url FROM source_connections WHERE id='"+connection_id+"'"],capture_output=True,text=True,timeout=30)
|
||||
if rows.returncode:raise RuntimeError('DingTalk source lookup failed')
|
||||
match=re.fullmatch(r'spaces/([A-Za-z0-9_-]+)\?space=([A-Za-z0-9_-]+)',rows.stdout.strip())
|
||||
if not match:raise RuntimeError('DingTalk source root missing')
|
||||
root_id,space_id=match.groups()
|
||||
root_key=dingtalk_root_key(connection_id,space_id)
|
||||
kind='drive' if space_id.isdigit() else 'wiki'
|
||||
emit('connecting',8,'正在用 dws 读取来源目录')
|
||||
def report(done,pending,files):
|
||||
total=done+pending
|
||||
emit('scanning',10+(65*done//total if total else 65),f"正在扫描目录:已完成 {done} 个文件夹,待处理 {pending} 个",done,total,scanned_directories=done,discovered_directories=total,scanned_files=files,estimated_total_files=files,total_is_estimate=True)
|
||||
try:nodes=walk(space_id,kind,root_id,report)
|
||||
except Exception as error:raise RefreshBlocked('dws 目录读取失败,请检查共享授权及来源读取权限') from error
|
||||
sources=build_dingtalk_sources({'roots':{root_key:{'configured_root_id':root_id,'live_space_name':name,'nodes':nodes}}})
|
||||
if len(sources)!=1:raise RuntimeError('DingTalk refreshed source missing')
|
||||
emit('building',80,'正在校验钉钉父子目录和文件数量')
|
||||
return match[0]
|
||||
return sources[0]
|
||||
|
||||
def refresh_alist(name):
|
||||
sys.path.insert(0,'/sources/alist');from alist_readonly_inventory import AListReadOnlyClient,join_path,read_connections
|
||||
|
||||
Reference in New Issue
Block a user