mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-02 12:26:36 +08:00
feat(strm): auto-append remote directory tail to local output path (#16)
When adding or editing a cloud sync path, append the last segment of the remote directory to the local output directory (e.g. /home/ubuntu/strm + 电影). Remote changes replace the previous auto-appended tail; local browse and blur commit the base path with the current remote tail. Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: truewhile <truewhile@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
/** 取路径最后一级目录/文件名(兼容 / 与 \\)。 */
|
||||
export function lastPathSegment(path: string): string {
|
||||
const trimmed = path.trim().replace(/[\\/]+$/, '')
|
||||
if (!trimmed) return ''
|
||||
const parts = trimmed.split(/[/\\]/).filter(Boolean)
|
||||
return parts[parts.length - 1] ?? ''
|
||||
}
|
||||
|
||||
function pathSeparator(path: string): '/' | '\\' {
|
||||
return path.includes('\\') ? '\\' : '/'
|
||||
}
|
||||
|
||||
function normalizeSlashes(path: string): string {
|
||||
return path.replace(/\\/g, '/').replace(/\/+$/, '')
|
||||
}
|
||||
|
||||
/** 去掉 local 末尾与 prevRemoteTail 匹配的自动拼接段。 */
|
||||
function stripRemoteTail(localPath: string, prevRemoteTail: string): string {
|
||||
if (!prevRemoteTail) return localPath.trim()
|
||||
const trimmed = localPath.trim()
|
||||
if (!trimmed) return ''
|
||||
const norm = normalizeSlashes(trimmed)
|
||||
const suffix = '/' + prevRemoteTail
|
||||
if (norm.endsWith(suffix)) {
|
||||
const stripped = norm.slice(0, -suffix.length)
|
||||
if (!stripped) return ''
|
||||
return stripped.split('/').join(pathSeparator(trimmed))
|
||||
}
|
||||
if (norm === prevRemoteTail) return ''
|
||||
return trimmed
|
||||
}
|
||||
|
||||
/** 将远端最后一级目录名拼到本地输出目录末尾;远端变更时替换旧尾段。 */
|
||||
export function syncLocalPathWithRemote(
|
||||
localPath: string,
|
||||
remotePath: string,
|
||||
prevRemoteTail = '',
|
||||
): { localPath: string; remoteTail: string } {
|
||||
const remoteTail = lastPathSegment(remotePath)
|
||||
const base = stripRemoteTail(localPath, prevRemoteTail)
|
||||
if (!remoteTail) {
|
||||
return { localPath: base || localPath.trim(), remoteTail: '' }
|
||||
}
|
||||
if (!base) {
|
||||
return { localPath: localPath.trim(), remoteTail }
|
||||
}
|
||||
const sep = pathSeparator(base)
|
||||
const normBase = normalizeSlashes(base)
|
||||
if (normBase.endsWith('/' + remoteTail)) {
|
||||
return { localPath: base, remoteTail }
|
||||
}
|
||||
return { localPath: `${base}${sep}${remoteTail}`, remoteTail }
|
||||
}
|
||||
Reference in New Issue
Block a user