Files
SceneMarshal/scripts/remote-common.ps1
T
ali 708f9c1802 feat(archive): 新增 media-archive,一键把整理好的目录归档进媒体库
- scripts/media_archive.py:按目录名里的 [tmdbid-N] 查 TMDB,结合
  original_language / origin_country / genres 给出分类建议;交互选择或 --to 指定;
  同盘 os.rename(跨盘回退 shutil.move),目标存在则拒绝,移动后校验文件数/体积;
  可选触发 Jellyfin /Library/Refresh
- scripts/media-archive.ps1 + remote-common.ps1:Windows 侧包装,路径翻译与 media-rename 共用
- 右键菜单新增「SceneRename 归档」,共 10 个注册项
- deploy.ps1 同时安装 /usr/local/bin/media-archive
2026-11-17 13:00:52 +08:00

89 lines
3.4 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#Requires -Version 7.0
<#
.SYNOPSIS
media-rename / media-archive 共用的 SSH 与路径翻译工具(由调用方 dot-source 引入)。
#>
$script:Remote = 'ali@10.10.1.202'
$script:QuarkDir = '/hdd/sda1/Quark-Down'
$script:SshBaseOpts = @('-o', 'BatchMode=yes', '-o', 'ConnectTimeout=10')
function Assert-SshAgent {
if (-not $env:SSH_AUTH_SOCK) {
$pipe = '\\.\pipe\openssh-ssh-agent'
if (Test-Path -LiteralPath $pipe) { $env:SSH_AUTH_SOCK = $pipe }
}
if (-not $env:SSH_AUTH_SOCK) { throw 'SSH agent 未就绪:请启动 Bitwarden 并启用 SSH agent。' }
$out = & ssh-add -l 2>&1
if ($LASTEXITCODE -ne 0) {
$hint = if ($LASTEXITCODE -eq 1) { 'agent 里没有可用密钥' } else { '连不上 agent 管道' }
throw "SSH agent 不可用($hint):$out`n请先解锁 Bitwarden。"
}
}
function ConvertTo-RemotePath([string]$Path) {
$p = ($Path -replace '\\', '/').Trim()
if ($p -match '^//10\.10\.1\.202/n100-sda1/?(.*)$') {
$rest = $Matches[1]
return $(if ($rest) { "/hdd/sda1/$rest" } else { '/hdd/sda1' })
}
if ($p -match '^//10\.10\.1\.202/n100-ali/?(.*)$') {
$rest = $Matches[1]
return $(if ($rest) { "/home/ali/$rest" } else { '/home/ali' })
}
if ($p.StartsWith('//')) { throw "不认识的共享路径: $Path" }
if ($p -match '^/') { return $p }
if ($p -match '^[A-Za-z]:/') {
if ($p -match '^[Yy]:/(.*)$') { return '/hdd/sda1/' + $Matches[1] }
throw "只有 Y: 映射到 N100(Y:\ = \\10.10.1.202\n100-sda1 = /hdd/sda1)。当前路径: $Path"
}
return $null
}
function ConvertTo-RemoteArg([string]$Value) {
"'" + ($Value -replace "'", "'\''") + "'"
}
function Invoke-RemoteQuery([string]$Command) {
$out = & ssh @SshBaseOpts $script:Remote $Command 2>&1
if ($LASTEXITCODE -ne 0) { throw "远端查询失败: $Command`n$out" }
return $out
}
function Test-IsPathLike([string]$Value) {
return ($Value -match '^[A-Za-z]:' -or $Value.StartsWith('\\') -or $Value.StartsWith('/') -or
$Value.Contains('/') -or $Value.Contains('\'))
}
function Resolve-RemoteTarget([string]$Name) {
if ($Name -eq 'latest') {
$raw = (Invoke-RemoteQuery "find $script:QuarkDir -maxdepth 1 -mindepth 1 -type d -printf '%T@ %p\n' | sort -rn | head -1 | cut -d' ' -f2-") -join "`n"
$latest = $raw.Trim()
if (-not $latest) { throw "$script:QuarkDir 下没有任何子目录" }
return $latest
}
$candidate = "$script:QuarkDir/$Name"
$exists = (Invoke-RemoteQuery "test -e $(ConvertTo-RemoteArg $candidate) && echo yes || echo no") -join ''
if ($exists.Trim() -eq 'yes') { return $candidate }
$dirs = @(Invoke-RemoteQuery "ls -1 $script:QuarkDir")
$hits = @($dirs | Where-Object { $_ -like "*$Name*" })
if ($hits.Count -eq 1) { return "$script:QuarkDir/$($hits[0])" }
if ($hits.Count -gt 1) {
throw "『$Name』匹配到多个目录,请指定更完整的名称或直接用路径:`n " + ($hits -join "`n ")
}
throw "在 $script:QuarkDir 下找不到『$Name』。可用 latest,或给出完整路径。"
}
function Resolve-TargetToken([string]$Token) {
if (Test-IsPathLike $Token) {
$translated = ConvertTo-RemotePath $Token
if (-not $translated) { throw "无法识别的路径: $Token" }
return $translated
}
return Resolve-RemoteTarget $Token
}