mirror of
https://github.com/truewhile/MeBox.git
synced 2026-09-28 11:16:37 +08:00
Fix organize targets and cloud transfer controls
This commit is contained in:
@@ -153,7 +153,7 @@ docker pull shukbet/mediastationgo:latest
|
|||||||
volumes:
|
volumes:
|
||||||
- ./data:/data
|
- ./data:/data
|
||||||
- ./cache:/cache
|
- ./cache:/cache
|
||||||
- ./media:/media:ro
|
- ./media:/media
|
||||||
- ./downloads:/downloads
|
- ./downloads:/downloads
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -163,7 +163,7 @@ volumes:
|
|||||||
| --- | --- | --- |
|
| --- | --- | --- |
|
||||||
| `./data` | `/data` | 程序数据库、配置、账号信息;一定要备份 |
|
| `./data` | `/data` | 程序数据库、配置、账号信息;一定要备份 |
|
||||||
| `./cache` | `/cache` | 缓存目录;可清理 |
|
| `./cache` | `/cache` | 缓存目录;可清理 |
|
||||||
| `./media` | `/media` | 媒体库目录;网页里添加媒体库时填 `/media/...` |
|
| `./media` | `/media` | 媒体库目录;自动整理入库需要可写,网页里添加媒体库时填 `/media/...` |
|
||||||
| `./downloads` | `/downloads` | 下载目录;文件管理和自动整理会用 |
|
| `./downloads` | `/downloads` | 下载目录;文件管理和自动整理会用 |
|
||||||
|
|
||||||
如果你的媒体在 NAS 真实目录,例如:
|
如果你的媒体在 NAS 真实目录,例如:
|
||||||
@@ -179,7 +179,7 @@ volumes:
|
|||||||
volumes:
|
volumes:
|
||||||
- ./data:/data
|
- ./data:/data
|
||||||
- ./cache:/cache
|
- ./cache:/cache
|
||||||
- /vol1/1000/Media:/media:ro
|
- /vol1/1000/Media:/media
|
||||||
- /vol1/1000/Downloads:/downloads
|
- /vol1/1000/Downloads:/downloads
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
@@ -193,7 +193,8 @@ environment:
|
|||||||
- `volumes` 右边是容器里的路径,建议固定用 `/media` 和 `/downloads`。
|
- `volumes` 右边是容器里的路径,建议固定用 `/media` 和 `/downloads`。
|
||||||
- 在网页里新建媒体库时,填容器路径,例如 `/media/电影`、`/media/电视剧`。
|
- 在网页里新建媒体库时,填容器路径,例如 `/media/电影`、`/media/电视剧`。
|
||||||
- 不要把 NAS 绝对路径写成 `./vol1/...`,`./` 表示当前部署目录下面的相对路径。
|
- 不要把 NAS 绝对路径写成 `./vol1/...`,`./` 表示当前部署目录下面的相对路径。
|
||||||
- Windows Docker Desktop 可以写成 `D:/Media:/media:ro`、`D:/Downloads:/downloads`。
|
- Windows Docker Desktop 可以写成 `D:/Media:/media`、`D:/Downloads:/downloads`。
|
||||||
|
- 如果你只想扫描/播放、不使用自动整理入库,可以手动加 `:ro` 变成只读;只要要整理、重命名、入库,媒体库挂载必须保持读写。
|
||||||
|
|
||||||
### 最简单 compose 示例
|
### 最简单 compose 示例
|
||||||
|
|
||||||
@@ -228,7 +229,7 @@ services:
|
|||||||
|
|
||||||
# 新手先用当前目录下的 media/downloads。
|
# 新手先用当前目录下的 media/downloads。
|
||||||
# NAS 用户把左边改成真实绝对路径。
|
# NAS 用户把左边改成真实绝对路径。
|
||||||
- ./media:/media:ro
|
- ./media:/media
|
||||||
- ./downloads:/downloads
|
- ./downloads:/downloads
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
|
|||||||
+5
-4
@@ -153,7 +153,7 @@ Focus on this part:
|
|||||||
volumes:
|
volumes:
|
||||||
- ./data:/data
|
- ./data:/data
|
||||||
- ./cache:/cache
|
- ./cache:/cache
|
||||||
- ./media:/media:ro
|
- ./media:/media
|
||||||
- ./downloads:/downloads
|
- ./downloads:/downloads
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -179,7 +179,7 @@ change the compose file to:
|
|||||||
volumes:
|
volumes:
|
||||||
- ./data:/data
|
- ./data:/data
|
||||||
- ./cache:/cache
|
- ./cache:/cache
|
||||||
- /vol1/1000/Media:/media:ro
|
- /vol1/1000/Media:/media
|
||||||
- /vol1/1000/Downloads:/downloads
|
- /vol1/1000/Downloads:/downloads
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
@@ -193,7 +193,8 @@ Rules:
|
|||||||
- The right side is the container path. Keep `/media` and `/downloads` unless you know why you are changing them.
|
- The right side is the container path. Keep `/media` and `/downloads` unless you know why you are changing them.
|
||||||
- In the web UI, create libraries with container paths such as `/media/Movies` or `/media/TV`.
|
- In the web UI, create libraries with container paths such as `/media/Movies` or `/media/TV`.
|
||||||
- Do not write NAS absolute paths as `./vol1/...`; `./` means a folder under the current compose directory.
|
- Do not write NAS absolute paths as `./vol1/...`; `./` means a folder under the current compose directory.
|
||||||
- On Windows Docker Desktop, paths like `D:/Media:/media:ro` and `D:/Downloads:/downloads` are fine.
|
- On Windows Docker Desktop, paths like `D:/Media:/media` and `D:/Downloads:/downloads` are fine.
|
||||||
|
- If you only scan/play existing media and never organize into the library, you may add `:ro`; if you use organize/rename/ingest, the media mount must stay writable.
|
||||||
|
|
||||||
### Minimal compose example
|
### Minimal compose example
|
||||||
|
|
||||||
@@ -228,7 +229,7 @@ services:
|
|||||||
|
|
||||||
# Beginners can keep ./media and ./downloads.
|
# Beginners can keep ./media and ./downloads.
|
||||||
# NAS users should replace the left side with real absolute paths.
|
# NAS users should replace the left side with real absolute paths.
|
||||||
- ./media:/media:ro
|
- ./media:/media
|
||||||
- ./downloads:/downloads
|
- ./downloads:/downloads
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
|
|||||||
+5
-3
@@ -44,12 +44,14 @@ services:
|
|||||||
- ./cache:/cache
|
- ./cache:/cache
|
||||||
|
|
||||||
# 媒体库目录。
|
# 媒体库目录。
|
||||||
|
# 如果要使用自动整理/重命名/入库,这里必须保持读写,不能加 :ro。
|
||||||
|
# 只有完全不整理、只扫描/播放已有媒体时,才建议手动改成只读。
|
||||||
# 新手可先把影片放到当前目录的 ./media。
|
# 新手可先把影片放到当前目录的 ./media。
|
||||||
# NAS 用户把左边改成真实路径,例如:
|
# NAS 用户把左边改成真实路径,例如:
|
||||||
# - /vol1/1000/Media:/media:ro
|
# - /vol1/1000/Media:/media
|
||||||
# Windows Docker Desktop 示例:
|
# Windows Docker Desktop 示例:
|
||||||
# - D:/Media:/media:ro
|
# - D:/Media:/media
|
||||||
- ./media:/media:ro
|
- ./media:/media
|
||||||
|
|
||||||
# 下载目录。
|
# 下载目录。
|
||||||
# qB 下载目录、手动整理、自动整理会经常用到。
|
# qB 下载目录、手动整理、自动整理会经常用到。
|
||||||
|
|||||||
@@ -109,7 +109,7 @@ func scanLibraryHandler(svc *service.Container) gin.HandlerFunc {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
_, _, _ = svc.Scan.StartCloudLibraryScan(id, false)
|
_, _, _ = svc.Scan.StartCloudLibraryScan(id, false)
|
||||||
finishHTTPTask(task, nil, "queued", "云盘扫描已加入后台队列", map[string]int64{"queued": 1})
|
finishHTTPTask(task, nil, "queued", "云盘扫描已加入后台队列", map[string]int64{"queued": 1}, nil)
|
||||||
c.JSON(http.StatusAccepted, gin.H{
|
c.JSON(http.StatusAccepted, gin.H{
|
||||||
"library_id": id,
|
"library_id": id,
|
||||||
"visited": 0,
|
"visited": 0,
|
||||||
@@ -126,11 +126,11 @@ func scanLibraryHandler(svc *service.Container) gin.HandlerFunc {
|
|||||||
task := startScanHTTPTask(svc, "手动扫描入库", lib.Name, lib.Path)
|
task := startScanHTTPTask(svc, "手动扫描入库", lib.Name, lib.Path)
|
||||||
res, err := svc.Scan.ScanLibrary(c.Request.Context(), id)
|
res, err := svc.Scan.ScanLibrary(c.Request.Context(), id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
finishHTTPTask(task, err, "scan", "手动扫描入库失败", nil)
|
finishHTTPTask(task, err, "scan", "手动扫描入库失败", nil, nil)
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
finishHTTPTask(task, nil, "completed", "手动扫描入库结束", scanTaskMetrics(res))
|
finishHTTPTask(task, nil, "completed", "手动扫描入库结束", scanTaskMetrics(res), nil)
|
||||||
c.JSON(http.StatusOK, res)
|
c.JSON(http.StatusOK, res)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,18 +61,18 @@ func organizeMediaHandler(svc *service.Container) gin.HandlerFunc {
|
|||||||
task := startOrganizeHTTPTask(svc, "手动整理媒体", opts)
|
task := startOrganizeHTTPTask(svc, "手动整理媒体", opts)
|
||||||
dst, err := svc.Organizer.OrganizeMediaWithOptions(c.Request.Context(), c.Param("id"), opts)
|
dst, err := svc.Organizer.OrganizeMediaWithOptions(c.Request.Context(), c.Param("id"), opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
finishHTTPTask(task, err, "organize", "手动整理媒体失败", nil)
|
finishHTTPTask(task, err, "organize", "手动整理媒体失败", nil, nil)
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
payload := gin.H{"path": dst}
|
payload := gin.H{"path": dst}
|
||||||
if organizeScanAfter(req.ScanAfter) && !req.DryRun && svc.Scan != nil {
|
if organizeScanAfter(req.ScanAfter) && !req.DryRun && svc.Scan != nil {
|
||||||
updateHTTPTask(task, "scan_scrape", "正在扫描入库并按设置刮削", nil)
|
updateHTTPTask(task, "scan_scrape", "正在扫描入库并按设置刮削", nil, nil)
|
||||||
scans, scrapes := scanAndScrapeAfterOrganize(c, svc, dst, strings.TrimSpace(req.LibraryID), req.ScrapeAfter)
|
scans, scrapes := scanAndScrapeAfterOrganize(c, svc, dst, strings.TrimSpace(req.LibraryID), req.ScrapeAfter)
|
||||||
payload["scans"] = scans
|
payload["scans"] = scans
|
||||||
payload["scrapes"] = scrapes
|
payload["scrapes"] = scrapes
|
||||||
}
|
}
|
||||||
finishHTTPTask(task, nil, "completed", "手动整理媒体结束", nil)
|
finishHTTPTask(task, nil, "completed", "手动整理媒体结束", nil, nil)
|
||||||
c.JSON(http.StatusOK, payload)
|
c.JSON(http.StatusOK, payload)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -85,15 +85,16 @@ func organizeLibraryHandler(svc *service.Container) gin.HandlerFunc {
|
|||||||
task := startOrganizeHTTPTask(svc, "手动整理媒体库", opts)
|
task := startOrganizeHTTPTask(svc, "手动整理媒体库", opts)
|
||||||
res, err := svc.Organizer.OrganizeLibraryWithOptions(c.Request.Context(), c.Param("id"), opts)
|
res, err := svc.Organizer.OrganizeLibraryWithOptions(c.Request.Context(), c.Param("id"), opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
finishHTTPTask(task, err, "organize", "手动整理媒体库失败", nil)
|
finishHTTPTask(task, err, "organize", "手动整理媒体库失败", nil, nil)
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
details := service.OrganizeTaskDetails(res, 8)
|
||||||
if organizeScanAfter(req.ScanAfter) && !req.DryRun && svc.Scan != nil {
|
if organizeScanAfter(req.ScanAfter) && !req.DryRun && svc.Scan != nil {
|
||||||
updateHTTPTask(task, "scan_scrape", "正在扫描入库并按设置刮削", nil)
|
updateHTTPTask(task, "scan_scrape", "正在扫描入库并按设置刮削", service.OrganizeTaskMetrics(res), details)
|
||||||
res.Scans, res.Scrapes = scanAndScrapeAfterOrganize(c, svc, res.DestPath, c.Param("id"), req.ScrapeAfter)
|
res.Scans, res.Scrapes = scanAndScrapeAfterOrganize(c, svc, res.DestPath, c.Param("id"), req.ScrapeAfter)
|
||||||
}
|
}
|
||||||
finishHTTPTask(task, nil, "completed", "手动整理媒体库结束", service.OrganizeTaskMetrics(res))
|
finishHTTPTask(task, nil, "completed", "手动整理媒体库结束", service.OrganizeTaskMetrics(res), details)
|
||||||
c.JSON(http.StatusOK, res)
|
c.JSON(http.StatusOK, res)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -116,16 +117,17 @@ func organizeDirectoryHandler(svc *service.Container) gin.HandlerFunc {
|
|||||||
task := startOrganizeHTTPTask(svc, "手动整理入库", opts)
|
task := startOrganizeHTTPTask(svc, "手动整理入库", opts)
|
||||||
res, err := svc.Organizer.OrganizeDirectory(c.Request.Context(), opts)
|
res, err := svc.Organizer.OrganizeDirectory(c.Request.Context(), opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
finishHTTPTask(task, err, "organize", "手动整理入库失败", nil)
|
finishHTTPTask(task, err, "organize", "手动整理入库失败", nil, nil)
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
updateHTTPTask(task, "organize", "手动整理完成,准备扫描入库", service.OrganizeTaskMetrics(res))
|
details := service.OrganizeTaskDetails(res, 8)
|
||||||
|
updateHTTPTask(task, "organize", "手动整理完成,准备扫描入库", service.OrganizeTaskMetrics(res), details)
|
||||||
if organizeScanAfter(req.ScanAfter) && !req.DryRun && svc.Scan != nil {
|
if organizeScanAfter(req.ScanAfter) && !req.DryRun && svc.Scan != nil {
|
||||||
updateHTTPTask(task, "scan_scrape", "正在扫描入库并按设置刮削", service.OrganizeTaskMetrics(res))
|
updateHTTPTask(task, "scan_scrape", "正在扫描入库并按设置刮削", service.OrganizeTaskMetrics(res), details)
|
||||||
res.Scans, res.Scrapes = scanAndScrapeAfterOrganize(c, svc, res.DestPath, strings.TrimSpace(req.LibraryID), req.ScrapeAfter)
|
res.Scans, res.Scrapes = scanAndScrapeAfterOrganize(c, svc, res.DestPath, strings.TrimSpace(req.LibraryID), req.ScrapeAfter)
|
||||||
}
|
}
|
||||||
finishHTTPTask(task, nil, "completed", "手动整理入库结束", service.OrganizeTaskMetrics(res))
|
finishHTTPTask(task, nil, "completed", "手动整理入库结束", service.OrganizeTaskMetrics(res), details)
|
||||||
c.JSON(http.StatusOK, res)
|
c.JSON(http.StatusOK, res)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -146,18 +148,18 @@ func startOrganizeHTTPTask(svc *service.Container, name string, opts service.Org
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateHTTPTask(task *service.TaskHandle, stage, message string, metrics map[string]int64) {
|
func updateHTTPTask(task *service.TaskHandle, stage, message string, metrics map[string]int64, details []string) {
|
||||||
if task == nil {
|
if task == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
task.Update(service.TaskUpdate{Stage: stage, Message: message, Metrics: metrics})
|
task.Update(service.TaskUpdate{Stage: stage, Message: message, Metrics: metrics, Details: details})
|
||||||
}
|
}
|
||||||
|
|
||||||
func finishHTTPTask(task *service.TaskHandle, err error, stage, message string, metrics map[string]int64) {
|
func finishHTTPTask(task *service.TaskHandle, err error, stage, message string, metrics map[string]int64, details []string) {
|
||||||
if task == nil {
|
if task == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
task.Finish(err, service.TaskUpdate{Stage: stage, Message: message, Metrics: metrics})
|
task.Finish(err, service.TaskUpdate{Stage: stage, Message: message, Metrics: metrics, Details: details})
|
||||||
}
|
}
|
||||||
|
|
||||||
func scanAndScrapeAfterOrganize(c *gin.Context, svc *service.Container, destRoot, preferredLibraryID string, scrapeOverride *bool) ([]service.OrganizeScanSummary, []service.OrganizeScrapeSummary) {
|
func scanAndScrapeAfterOrganize(c *gin.Context, svc *service.Container, destRoot, preferredLibraryID string, scrapeOverride *bool) ([]service.OrganizeScanSummary, []service.OrganizeScrapeSummary) {
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ func scrapeOneHandler(svc *service.Container) gin.HandlerFunc {
|
|||||||
}
|
}
|
||||||
task := startScrapeHTTPTask(svc, "手动刮削媒体", m.Title, m.Path)
|
task := startScrapeHTTPTask(svc, "手动刮削媒体", m.Title, m.Path)
|
||||||
if err := svc.Scraper.EnrichOne(c.Request.Context(), m); err != nil {
|
if err := svc.Scraper.EnrichOne(c.Request.Context(), m); err != nil {
|
||||||
finishHTTPTask(task, err, "scrape", "手动刮削媒体失败", nil)
|
finishHTTPTask(task, err, "scrape", "手动刮削媒体失败", nil, nil)
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -92,7 +92,7 @@ func scrapeOneHandler(svc *service.Container) gin.HandlerFunc {
|
|||||||
if refreshed != nil && refreshed.ScrapeStatus == "matched" {
|
if refreshed != nil && refreshed.ScrapeStatus == "matched" {
|
||||||
metrics["matched"] = 1
|
metrics["matched"] = 1
|
||||||
}
|
}
|
||||||
finishHTTPTask(task, nil, "completed", "手动刮削媒体结束", metrics)
|
finishHTTPTask(task, nil, "completed", "手动刮削媒体结束", metrics, nil)
|
||||||
c.JSON(http.StatusOK, refreshed)
|
c.JSON(http.StatusOK, refreshed)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -118,7 +118,7 @@ func scrapeLibraryHandler(svc *service.Container) gin.HandlerFunc {
|
|||||||
stage = "scrape"
|
stage = "scrape"
|
||||||
message = "手动刮削媒体库失败"
|
message = "手动刮削媒体库失败"
|
||||||
}
|
}
|
||||||
finishHTTPTask(task, err, stage, message, metrics)
|
finishHTTPTask(task, err, stage, message, metrics, nil)
|
||||||
}(libID, task)
|
}(libID, task)
|
||||||
c.JSON(http.StatusAccepted, gin.H{"status": "scraping"})
|
c.JSON(http.StatusAccepted, gin.H{"status": "scraping"})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -121,6 +121,10 @@ func schemaHandler(_ *service.Container) gin.HandlerFunc {
|
|||||||
{"key": "cloud.upload_recursive", "type": "toggle", "label": "递归扫描源目录"},
|
{"key": "cloud.upload_recursive", "type": "toggle", "label": "递归扫描源目录"},
|
||||||
{"key": "cloud.upload_sidecars", "type": "toggle", "label": "同步 NFO / 海报 / 字幕"},
|
{"key": "cloud.upload_sidecars", "type": "toggle", "label": "同步 NFO / 海报 / 字幕"},
|
||||||
{"key": "cloud.upload_overwrite", "type": "toggle", "label": "覆盖远端同名文件"},
|
{"key": "cloud.upload_overwrite", "type": "toggle", "label": "覆盖远端同名文件"},
|
||||||
|
{"key": "cloud.upload_transfer_mode", "type": "select", "label": "自动转存方式", "options": []gin.H{
|
||||||
|
{"value": "copy", "label": "复制"},
|
||||||
|
{"value": "move", "label": "移动"},
|
||||||
|
}},
|
||||||
{"key": "cloud.upload_interval_seconds", "type": "number", "label": "自动转存间隔秒数"},
|
{"key": "cloud.upload_interval_seconds", "type": "number", "label": "自动转存间隔秒数"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1136,6 +1136,7 @@ func (d *DownloadService) onTorrentComplete(ctx context.Context, torrent QBitTor
|
|||||||
DestPath: res.DestPath,
|
DestPath: res.DestPath,
|
||||||
Message: "下载完成整理已完成,准备扫描入库",
|
Message: "下载完成整理已完成,准备扫描入库",
|
||||||
Metrics: OrganizeTaskMetrics(res),
|
Metrics: OrganizeTaskMetrics(res),
|
||||||
|
Details: OrganizeTaskDetails(res, 8),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if d.scanner != nil && res != nil && strings.TrimSpace(res.DestPath) != "" && OrganizeResultNeedsVisibilitySync(res) {
|
if d.scanner != nil && res != nil && strings.TrimSpace(res.DestPath) != "" && OrganizeResultNeedsVisibilitySync(res) {
|
||||||
@@ -1144,6 +1145,7 @@ func (d *DownloadService) onTorrentComplete(ctx context.Context, torrent QBitTor
|
|||||||
Stage: "scan_scrape",
|
Stage: "scan_scrape",
|
||||||
Message: "正在扫描入库并按设置刮削",
|
Message: "正在扫描入库并按设置刮削",
|
||||||
Metrics: OrganizeTaskMetrics(res),
|
Metrics: OrganizeTaskMetrics(res),
|
||||||
|
Details: OrganizeTaskDetails(res, 8),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
res.Scans, res.Scrapes = d.scanner.ScanAndScrapeLibrariesForPath(ctx, res.DestPath, "", OrganizeScrapeAfterEnabled(ctx, d.repo))
|
res.Scans, res.Scrapes = d.scanner.ScanAndScrapeLibrariesForPath(ctx, res.DestPath, "", OrganizeScrapeAfterEnabled(ctx, d.repo))
|
||||||
@@ -1160,6 +1162,7 @@ func (d *DownloadService) onTorrentComplete(ctx context.Context, torrent QBitTor
|
|||||||
Stage: "completed",
|
Stage: "completed",
|
||||||
Message: "下载完成自动整理入库结束",
|
Message: "下载完成自动整理入库结束",
|
||||||
Metrics: OrganizeTaskMetrics(res),
|
Metrics: OrganizeTaskMetrics(res),
|
||||||
|
Details: OrganizeTaskDetails(res, 8),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
d.markCompletedTorrentCatchupRecorded(context.Background(), torrent)
|
d.markCompletedTorrentCatchupRecorded(context.Background(), torrent)
|
||||||
|
|||||||
@@ -115,7 +115,18 @@ func (o *OrganizerService) OrganizeMediaWithOptions(ctx context.Context, mediaID
|
|||||||
if err != nil || lib == nil {
|
if err != nil || lib == nil {
|
||||||
return "", errors.New("library not found")
|
return "", errors.New("library not found")
|
||||||
}
|
}
|
||||||
|
if _, ok := ParseCloudLibraryMount(lib.Path); ok {
|
||||||
|
return "", errors.New("local organize cannot use cloud libraries directly; use external storage scan/mount for cloud media or enable cloud transfer to write to cloud")
|
||||||
|
}
|
||||||
baseRoot := o.resolveBaseRoot(ctx, lib, opts.DestPath)
|
baseRoot := o.resolveBaseRoot(ctx, lib, opts.DestPath)
|
||||||
|
if _, ok := ParseCloudLibraryMount(baseRoot); ok {
|
||||||
|
return "", errors.New("organize destination must be a local writable media directory; enable cloud transfer in external storage when writing to cloud")
|
||||||
|
}
|
||||||
|
if !opts.DryRun {
|
||||||
|
if err := ensureOrganizeDestinationWritable(baseRoot); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
}
|
||||||
mode := o.resolveTransferMode(ctx, opts.TransferMode)
|
mode := o.resolveTransferMode(ctx, opts.TransferMode)
|
||||||
if isSeriesLibraryType(lib.Type) {
|
if isSeriesLibraryType(lib.Type) {
|
||||||
if err := o.refreshEpisodeIdentity(m, lib); err != nil {
|
if err := o.refreshEpisodeIdentity(m, lib); err != nil {
|
||||||
@@ -284,6 +295,9 @@ func (o *OrganizerService) OrganizeLibraryWithOptions(ctx context.Context, libra
|
|||||||
if err != nil || lib == nil {
|
if err != nil || lib == nil {
|
||||||
return nil, errors.New("library not found")
|
return nil, errors.New("library not found")
|
||||||
}
|
}
|
||||||
|
if _, ok := ParseCloudLibraryMount(lib.Path); ok {
|
||||||
|
return nil, errors.New("local organize cannot use cloud libraries directly; use external storage scan/mount for cloud media or enable cloud transfer to write to cloud")
|
||||||
|
}
|
||||||
var rows []model.Media
|
var rows []model.Media
|
||||||
if err := o.repo.DB.WithContext(ctx).
|
if err := o.repo.DB.WithContext(ctx).
|
||||||
Where("library_id = ? AND deleted_at IS NULL", libraryID).
|
Where("library_id = ? AND deleted_at IS NULL", libraryID).
|
||||||
@@ -292,8 +306,19 @@ func (o *OrganizerService) OrganizeLibraryWithOptions(ctx context.Context, libra
|
|||||||
}
|
}
|
||||||
// 源目录(待整理):仅整理位于该目录下的媒体;留空 = 整个媒体库。
|
// 源目录(待整理):仅整理位于该目录下的媒体;留空 = 整个媒体库。
|
||||||
sourceRoot := o.resolveSourceRoot(ctx, lib, opts.SourcePath)
|
sourceRoot := o.resolveSourceRoot(ctx, lib, opts.SourcePath)
|
||||||
|
if _, ok := ParseCloudLibraryMount(sourceRoot); ok {
|
||||||
|
return nil, errors.New("organize source must be a local directory; cloud libraries should be managed from external storage scan/mount")
|
||||||
|
}
|
||||||
// 目的地目录:已位于该根下的文件视为已整理;受 dest_path 覆盖与设置影响。
|
// 目的地目录:已位于该根下的文件视为已整理;受 dest_path 覆盖与设置影响。
|
||||||
baseRoot := o.resolveBaseRoot(ctx, lib, opts.DestPath)
|
baseRoot := o.resolveBaseRoot(ctx, lib, opts.DestPath)
|
||||||
|
if _, ok := ParseCloudLibraryMount(baseRoot); ok {
|
||||||
|
return nil, errors.New("organize destination must be a local writable media directory; enable cloud transfer in external storage when writing to cloud")
|
||||||
|
}
|
||||||
|
if !opts.DryRun {
|
||||||
|
if err := ensureOrganizeDestinationWritable(baseRoot); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
res := &OrganizeResult{SourcePath: sourceRoot, DestPath: baseRoot, DryRun: opts.DryRun}
|
res := &OrganizeResult{SourcePath: sourceRoot, DestPath: baseRoot, DryRun: opts.DryRun}
|
||||||
for i := range rows {
|
for i := range rows {
|
||||||
// 不在源目录内的文件跳过(不属于本次「从源目录整理」的范围)。
|
// 不在源目录内的文件跳过(不属于本次「从源目录整理」的范围)。
|
||||||
|
|||||||
@@ -123,10 +123,19 @@ func (o *OrganizerService) OrganizeDirectory(ctx context.Context, opts OrganizeO
|
|||||||
if statErr != nil {
|
if statErr != nil {
|
||||||
return nil, fmt.Errorf("source directory not accessible: %s", filepath.Clean(requestedSource))
|
return nil, fmt.Errorf("source directory not accessible: %s", filepath.Clean(requestedSource))
|
||||||
}
|
}
|
||||||
dest := resolveMappedDestinationPath(o.defaultDestRoot(ctx, opts.DestPath))
|
requestedDest := strings.TrimSpace(o.defaultDestRoot(ctx, opts.DestPath))
|
||||||
|
if _, ok := ParseCloudLibraryMount(requestedDest); ok {
|
||||||
|
return nil, errors.New("organize destination must be a local writable media directory; enable cloud transfer in external storage when writing to cloud")
|
||||||
|
}
|
||||||
|
dest := resolveMappedDestinationPath(requestedDest)
|
||||||
if dest == "" || dest == "." {
|
if dest == "" || dest == "." {
|
||||||
return nil, errors.New("destination path required")
|
return nil, errors.New("destination path required")
|
||||||
}
|
}
|
||||||
|
if !opts.DryRun {
|
||||||
|
if err := ensureOrganizeDestinationWritable(dest); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
mode := o.resolveTransferMode(ctx, opts.TransferMode)
|
mode := o.resolveTransferMode(ctx, opts.TransferMode)
|
||||||
res := &OrganizeResult{SourcePath: source, DestPath: dest, DryRun: opts.DryRun}
|
res := &OrganizeResult{SourcePath: source, DestPath: dest, DryRun: opts.DryRun}
|
||||||
if !info.IsDir() {
|
if !info.IsDir() {
|
||||||
@@ -197,6 +206,32 @@ func (o *OrganizerService) OrganizeDirectory(ctx context.Context, opts OrganizeO
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ensureOrganizeDestinationWritable(dest string) error {
|
||||||
|
dest = strings.TrimSpace(dest)
|
||||||
|
if dest == "" || dest == "." {
|
||||||
|
return errors.New("destination path required")
|
||||||
|
}
|
||||||
|
if _, ok := ParseCloudLibraryMount(dest); ok {
|
||||||
|
return errors.New("organize destination must be a local writable media directory; enable cloud transfer in external storage when writing to cloud")
|
||||||
|
}
|
||||||
|
if err := os.MkdirAll(dest, 0o755); err != nil { // #nosec G301 -- organized media directories must remain readable by NAS/player users.
|
||||||
|
return fmt.Errorf("destination path is not a writable directory: %s: %w", dest, err)
|
||||||
|
}
|
||||||
|
probe, err := os.CreateTemp(dest, ".mediastation-write-test-*") // #nosec G304 -- dest is operator-configured organize root.
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("destination path is not writable: %s: %w", dest, err)
|
||||||
|
}
|
||||||
|
name := probe.Name()
|
||||||
|
if closeErr := probe.Close(); closeErr != nil {
|
||||||
|
_ = os.Remove(name)
|
||||||
|
return fmt.Errorf("destination path write probe failed: %s: %w", dest, closeErr)
|
||||||
|
}
|
||||||
|
if err := os.Remove(name); err != nil {
|
||||||
|
return fmt.Errorf("destination path cleanup probe failed: %s: %w", dest, err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
type organizeDirectoryLayout struct {
|
type organizeDirectoryLayout struct {
|
||||||
MediaType string
|
MediaType string
|
||||||
Category string
|
Category string
|
||||||
|
|||||||
@@ -394,6 +394,7 @@ func (s *SchedulerService) cloudUploadInput(ctx context.Context) CloudUploadInpu
|
|||||||
Recursive: parseBoolSetting(get(CloudUploadRecursiveKey), true),
|
Recursive: parseBoolSetting(get(CloudUploadRecursiveKey), true),
|
||||||
IncludeSidecars: parseBoolSetting(get(CloudUploadSidecarsKey), true),
|
IncludeSidecars: parseBoolSetting(get(CloudUploadSidecarsKey), true),
|
||||||
Overwrite: parseBoolSetting(get(CloudUploadOverwriteKey), false),
|
Overwrite: parseBoolSetting(get(CloudUploadOverwriteKey), false),
|
||||||
|
TransferMode: get(CloudUploadTransferModeKey),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -569,6 +570,7 @@ func (s *SchedulerService) jobOrganizeSource(ctx context.Context) error {
|
|||||||
DestPath: res.DestPath,
|
DestPath: res.DestPath,
|
||||||
Message: "自动整理完成,准备扫描入库",
|
Message: "自动整理完成,准备扫描入库",
|
||||||
Metrics: OrganizeTaskMetrics(res),
|
Metrics: OrganizeTaskMetrics(res),
|
||||||
|
Details: OrganizeTaskDetails(res, 8),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if s.scanner != nil && res != nil && strings.TrimSpace(res.DestPath) != "" && OrganizeResultNeedsVisibilitySync(res) {
|
if s.scanner != nil && res != nil && strings.TrimSpace(res.DestPath) != "" && OrganizeResultNeedsVisibilitySync(res) {
|
||||||
@@ -577,6 +579,7 @@ func (s *SchedulerService) jobOrganizeSource(ctx context.Context) error {
|
|||||||
Stage: "scan_scrape",
|
Stage: "scan_scrape",
|
||||||
Message: "正在扫描入库并按设置刮削",
|
Message: "正在扫描入库并按设置刮削",
|
||||||
Metrics: OrganizeTaskMetrics(res),
|
Metrics: OrganizeTaskMetrics(res),
|
||||||
|
Details: OrganizeTaskDetails(res, 8),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
res.Scans, res.Scrapes = s.scanner.ScanAndScrapeLibrariesForPath(ctx, res.DestPath, "", OrganizeScrapeAfterEnabled(ctx, s.repo))
|
res.Scans, res.Scrapes = s.scanner.ScanAndScrapeLibrariesForPath(ctx, res.DestPath, "", OrganizeScrapeAfterEnabled(ctx, s.repo))
|
||||||
@@ -594,6 +597,7 @@ func (s *SchedulerService) jobOrganizeSource(ctx context.Context) error {
|
|||||||
Stage: "completed",
|
Stage: "completed",
|
||||||
Message: "自动整理入库结束",
|
Message: "自动整理入库结束",
|
||||||
Metrics: OrganizeTaskMetrics(res),
|
Metrics: OrganizeTaskMetrics(res),
|
||||||
|
Details: OrganizeTaskDetails(res, 8),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if s.log != nil && res != nil {
|
if s.log != nil && res != nil {
|
||||||
|
|||||||
@@ -147,9 +147,10 @@ func (s *StorageConfigService) Save(ctx context.Context, in StorageInput) (*Stor
|
|||||||
return s.Get(ctx, in.Type)
|
return s.Get(ctx, in.Type)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Logout clears saved cloud login credentials and disables the storage backend.
|
// Logout clears saved cloud login credentials, disables the storage backend,
|
||||||
// It intentionally keeps non-secret connection hints such as server / WebDAV
|
// and removes virtual cloud libraries/media for that provider. It intentionally
|
||||||
// URL / timeout so the admin can log in again without rebuilding the form.
|
// keeps non-secret connection hints such as server / WebDAV URL / timeout so
|
||||||
|
// the admin can log in again without rebuilding the form.
|
||||||
func (s *StorageConfigService) Logout(ctx context.Context, typ string) (*StorageView, error) {
|
func (s *StorageConfigService) Logout(ctx context.Context, typ string) (*StorageView, error) {
|
||||||
if !validStorageType(typ) {
|
if !validStorageType(typ) {
|
||||||
return nil, fmt.Errorf("unsupported storage type %q", typ)
|
return nil, fmt.Errorf("unsupported storage type %q", typ)
|
||||||
@@ -172,7 +173,53 @@ func (s *StorageConfigService) Logout(ctx context.Context, typ string) (*Storage
|
|||||||
cfg[k] = v
|
cfg[k] = v
|
||||||
}
|
}
|
||||||
enabled := false
|
enabled := false
|
||||||
return s.Save(ctx, StorageInput{Type: typ, Config: cfg, Enabled: &enabled})
|
saved, err := s.Save(ctx, StorageInput{Type: typ, Config: cfg, Enabled: &enabled})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
purged, err := s.purgeCloudLibraries(ctx, typ)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if s.log != nil {
|
||||||
|
s.log.Info("storage logout cleared cloud libraries",
|
||||||
|
zap.String("storage_type", typ),
|
||||||
|
zap.Int("libraries_deleted", purged))
|
||||||
|
}
|
||||||
|
return saved, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *StorageConfigService) purgeCloudLibraries(ctx context.Context, storageType string) (int, error) {
|
||||||
|
if s == nil || s.repo == nil || s.repo.Library == nil || s.repo.Media == nil {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
libs, err := s.repo.Library.List(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("list libraries: %w", err)
|
||||||
|
}
|
||||||
|
var affectedLibs []string
|
||||||
|
for _, lib := range libs {
|
||||||
|
if mount, ok := ParseCloudLibraryMount(lib.Path); ok && mount.Provider == storageType {
|
||||||
|
affectedLibs = append(affectedLibs, lib.ID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, libID := range affectedLibs {
|
||||||
|
if err := s.repo.Media.PurgeByLibrary(ctx, libID); err != nil {
|
||||||
|
if s.log != nil {
|
||||||
|
s.log.Warn("purge media by library failed", zap.String("library_id", libID), zap.Error(err))
|
||||||
|
}
|
||||||
|
return len(affectedLibs), fmt.Errorf("purge media by library %s: %w", libID, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, libID := range affectedLibs {
|
||||||
|
if err := s.repo.Library.Delete(ctx, libID); err != nil {
|
||||||
|
if s.log != nil {
|
||||||
|
s.log.Warn("delete library failed", zap.String("library_id", libID), zap.Error(err))
|
||||||
|
}
|
||||||
|
return len(affectedLibs), fmt.Errorf("delete library %s: %w", libID, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return len(affectedLibs), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func isStorageLoginSecretKey(key string) bool {
|
func isStorageLoginSecretKey(key string) bool {
|
||||||
@@ -778,32 +825,9 @@ func (s *StorageConfigService) DeleteStorage(ctx context.Context, storageType st
|
|||||||
return fmt.Errorf("storage config not found: %s", storageType)
|
return fmt.Errorf("storage config not found: %s", storageType)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找使用该存储的云盘库
|
affectedLibs, err := s.purgeCloudLibraries(ctx, storageType)
|
||||||
libs, err := s.repo.Library.List(ctx)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("list libraries: %w", err)
|
return err
|
||||||
}
|
|
||||||
|
|
||||||
var affectedLibs []string
|
|
||||||
for _, lib := range libs {
|
|
||||||
// 检查是否是云盘库且Provider匹配
|
|
||||||
if mount, ok := ParseCloudLibraryMount(lib.Path); ok && mount.Provider == storageType {
|
|
||||||
affectedLibs = append(affectedLibs, lib.ID)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 删除关联媒体
|
|
||||||
for _, libID := range affectedLibs {
|
|
||||||
if err := s.repo.Media.PurgeByLibrary(ctx, libID); err != nil {
|
|
||||||
s.log.Warn("purge media by library failed", zap.String("library_id", libID), zap.Error(err))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 删除关联库
|
|
||||||
for _, libID := range affectedLibs {
|
|
||||||
if err := s.repo.Library.Delete(ctx, libID); err != nil {
|
|
||||||
s.log.Warn("delete library failed", zap.String("library_id", libID), zap.Error(err))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 删除存储配置
|
// 删除存储配置
|
||||||
@@ -813,7 +837,7 @@ func (s *StorageConfigService) DeleteStorage(ctx context.Context, storageType st
|
|||||||
|
|
||||||
s.log.Info("storage deleted",
|
s.log.Info("storage deleted",
|
||||||
zap.String("storage_type", storageType),
|
zap.String("storage_type", storageType),
|
||||||
zap.Int("libraries_deleted", len(affectedLibs)))
|
zap.Int("libraries_deleted", affectedLibs))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,13 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
func TestStorageConfigLogoutClearsCloudCredentialsOnly(t *testing.T) {
|
"github.com/ShukeBta/MediaStationGo/internal/model"
|
||||||
_, storage := newStorageUploadTestService(t)
|
)
|
||||||
|
|
||||||
|
func TestStorageConfigLogoutClearsCredentialsAndCloudLibraries(t *testing.T) {
|
||||||
|
repos, storage := newStorageUploadTestService(t)
|
||||||
enabled := true
|
enabled := true
|
||||||
if _, err := storage.Save(t.Context(), StorageInput{
|
if _, err := storage.Save(t.Context(), StorageInput{
|
||||||
Type: "openlist",
|
Type: "openlist",
|
||||||
@@ -20,6 +24,20 @@ func TestStorageConfigLogoutClearsCloudCredentialsOnly(t *testing.T) {
|
|||||||
}); err != nil {
|
}); err != nil {
|
||||||
t.Fatalf("save storage: %v", err)
|
t.Fatalf("save storage: %v", err)
|
||||||
}
|
}
|
||||||
|
cloudLib := model.Library{Name: "OpenList", Path: BuildCloudLibraryPath("openlist", "/TV", "/TV"), Type: "tv", Enabled: true}
|
||||||
|
if err := repos.Library.Create(t.Context(), &cloudLib); err != nil {
|
||||||
|
t.Fatalf("create cloud library: %v", err)
|
||||||
|
}
|
||||||
|
localLib := model.Library{Name: "Local", Path: t.TempDir(), Type: "movie", Enabled: true}
|
||||||
|
if err := repos.Library.Create(t.Context(), &localLib); err != nil {
|
||||||
|
t.Fatalf("create local library: %v", err)
|
||||||
|
}
|
||||||
|
if err := repos.Media.Upsert(t.Context(), &model.Media{LibraryID: cloudLib.ID, Title: "Cloud", Path: "cloud://openlist/TV/Movie.mkv"}); err != nil {
|
||||||
|
t.Fatalf("create cloud media: %v", err)
|
||||||
|
}
|
||||||
|
if err := repos.Media.Upsert(t.Context(), &model.Media{LibraryID: localLib.ID, Title: "Local", Path: localLib.Path + "/Movie.mkv"}); err != nil {
|
||||||
|
t.Fatalf("create local media: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
view, err := storage.Logout(t.Context(), "openlist")
|
view, err := storage.Logout(t.Context(), "openlist")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -36,4 +54,21 @@ func TestStorageConfigLogoutClearsCloudCredentialsOnly(t *testing.T) {
|
|||||||
if view.Config["server"] != "http://openlist.test" || view.Config["url"] != "http://openlist.test/dav/" || view.Config["timeout_seconds"] != "120" {
|
if view.Config["server"] != "http://openlist.test" || view.Config["url"] != "http://openlist.test/dav/" || view.Config["timeout_seconds"] != "120" {
|
||||||
t.Fatalf("logout should keep non-secret connection hints, config = %#v", view.Config)
|
t.Fatalf("logout should keep non-secret connection hints, config = %#v", view.Config)
|
||||||
}
|
}
|
||||||
|
if got, err := repos.Library.FindByID(t.Context(), cloudLib.ID); err != nil {
|
||||||
|
t.Fatalf("find cloud library: %v", err)
|
||||||
|
} else if got != nil {
|
||||||
|
t.Fatalf("cloud library should be removed after logout: %#v", got)
|
||||||
|
}
|
||||||
|
if got, err := repos.Library.FindByID(t.Context(), localLib.ID); err != nil {
|
||||||
|
t.Fatalf("find local library: %v", err)
|
||||||
|
} else if got == nil {
|
||||||
|
t.Fatal("local library should remain after cloud logout")
|
||||||
|
}
|
||||||
|
var cloudMediaCount int64
|
||||||
|
if err := repos.DB.Unscoped().Model(&model.Media{}).Where("path = ?", "cloud://openlist/TV/Movie.mkv").Count(&cloudMediaCount).Error; err != nil {
|
||||||
|
t.Fatalf("count cloud media: %v", err)
|
||||||
|
}
|
||||||
|
if cloudMediaCount != 0 {
|
||||||
|
t.Fatalf("cloud media should be purged after logout, count=%d", cloudMediaCount)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ const (
|
|||||||
CloudUploadRecursiveKey = "cloud.upload_recursive"
|
CloudUploadRecursiveKey = "cloud.upload_recursive"
|
||||||
CloudUploadSidecarsKey = "cloud.upload_sidecars"
|
CloudUploadSidecarsKey = "cloud.upload_sidecars"
|
||||||
CloudUploadOverwriteKey = "cloud.upload_overwrite"
|
CloudUploadOverwriteKey = "cloud.upload_overwrite"
|
||||||
|
CloudUploadTransferModeKey = "cloud.upload_transfer_mode"
|
||||||
CloudUploadIntervalSecondsKey = "cloud.upload_interval_seconds"
|
CloudUploadIntervalSecondsKey = "cloud.upload_interval_seconds"
|
||||||
CloudUploadUnsupportedProvider = "本地文件直传目前支持 Alist / OpenList / WebDAV / CloudDrive2;115/夸克原生上传需要各自的分片上传私有接口,建议先用 CloudDrive2、OpenList 或 Alist 桥接后转存。"
|
CloudUploadUnsupportedProvider = "本地文件直传目前支持 Alist / OpenList / WebDAV / CloudDrive2;115/夸克原生上传需要各自的分片上传私有接口,建议先用 CloudDrive2、OpenList 或 Alist 桥接后转存。"
|
||||||
)
|
)
|
||||||
@@ -34,12 +35,14 @@ type CloudUploadInput struct {
|
|||||||
Recursive bool `json:"recursive"`
|
Recursive bool `json:"recursive"`
|
||||||
IncludeSidecars bool `json:"include_sidecars"`
|
IncludeSidecars bool `json:"include_sidecars"`
|
||||||
Overwrite bool `json:"overwrite"`
|
Overwrite bool `json:"overwrite"`
|
||||||
|
TransferMode string `json:"transfer_mode"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CloudUploadResult struct {
|
type CloudUploadResult struct {
|
||||||
SourcePath string `json:"source_path"`
|
SourcePath string `json:"source_path"`
|
||||||
DestPath string `json:"dest_path"`
|
DestPath string `json:"dest_path"`
|
||||||
Uploaded int `json:"uploaded"`
|
Uploaded int `json:"uploaded"`
|
||||||
|
Moved int `json:"moved,omitempty"`
|
||||||
Skipped int `json:"skipped"`
|
Skipped int `json:"skipped"`
|
||||||
Bytes int64 `json:"bytes"`
|
Bytes int64 `json:"bytes"`
|
||||||
Errors []string `json:"errors,omitempty"`
|
Errors []string `json:"errors,omitempty"`
|
||||||
@@ -49,7 +52,7 @@ type CloudUploadResult struct {
|
|||||||
type CloudUploadResultItem struct {
|
type CloudUploadResultItem struct {
|
||||||
Source string `json:"source"`
|
Source string `json:"source"`
|
||||||
Target string `json:"target"`
|
Target string `json:"target"`
|
||||||
Action string `json:"action"` // upload / skip / error
|
Action string `json:"action"` // upload / move / skip / error
|
||||||
Size int64 `json:"size,omitempty"`
|
Size int64 `json:"size,omitempty"`
|
||||||
Reason string `json:"reason,omitempty"`
|
Reason string `json:"reason,omitempty"`
|
||||||
}
|
}
|
||||||
@@ -65,10 +68,10 @@ var cloudUploadSidecarExtensions = map[string]struct{}{
|
|||||||
".srt": {}, ".ass": {}, ".ssa": {}, ".vtt": {}, ".sub": {}, ".idx": {},
|
".srt": {}, ".ass": {}, ".ssa": {}, ".vtt": {}, ".sub": {}, ".idx": {},
|
||||||
}
|
}
|
||||||
|
|
||||||
// UploadLocal copies local media files into an external storage backend. It is
|
// UploadLocal copies or moves local media files into an external storage
|
||||||
// intentionally conservative: it never deletes local files, skips existing
|
// backend. Cloud writes are intentionally gated by the per-storage
|
||||||
// remote targets unless Overwrite is set, and only uploads video + common
|
// transfer_enabled switch so mounting/scanning a cloud provider does not
|
||||||
// sidecar metadata files.
|
// accidentally make it writable.
|
||||||
func (s *StorageConfigService) UploadLocal(ctx context.Context, in CloudUploadInput) (*CloudUploadResult, error) {
|
func (s *StorageConfigService) UploadLocal(ctx context.Context, in CloudUploadInput) (*CloudUploadResult, error) {
|
||||||
in.Type = strings.TrimSpace(in.Type)
|
in.Type = strings.TrimSpace(in.Type)
|
||||||
in.SourcePath = strings.TrimSpace(in.SourcePath)
|
in.SourcePath = strings.TrimSpace(in.SourcePath)
|
||||||
@@ -76,7 +79,22 @@ func (s *StorageConfigService) UploadLocal(ctx context.Context, in CloudUploadIn
|
|||||||
if in.SourcePath == "" {
|
if in.SourcePath == "" {
|
||||||
return nil, errors.New("source_path required")
|
return nil, errors.New("source_path required")
|
||||||
}
|
}
|
||||||
uploader, err := s.uploader(ctx, in.Type)
|
view, err := s.Get(ctx, in.Type)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if view == nil || !view.Enabled {
|
||||||
|
return nil, fmt.Errorf("%s storage not configured", in.Type)
|
||||||
|
}
|
||||||
|
if !parseBoolSetting(strr(view.Config["transfer_enabled"]), false) {
|
||||||
|
return nil, errors.New("cloud transfer is disabled for this storage; enable transfer in external storage settings before writing to cloud")
|
||||||
|
}
|
||||||
|
mode := resolveCloudUploadTransferMode(in.TransferMode, strr(view.Config["transfer_mode"]), s.settingValue(ctx, CloudUploadTransferModeKey))
|
||||||
|
if mode != TransferCopy && mode != TransferMove {
|
||||||
|
return nil, errors.New("transfer_mode must be copy or move")
|
||||||
|
}
|
||||||
|
in.TransferMode = string(mode)
|
||||||
|
uploader, err := s.uploaderForView(in.Type, view)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -123,6 +141,13 @@ func (s *StorageConfigService) uploader(ctx context.Context, typ string) (storag
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if view == nil || !view.Enabled {
|
||||||
|
return nil, fmt.Errorf("%s storage not configured", typ)
|
||||||
|
}
|
||||||
|
return s.uploaderForView(typ, view)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *StorageConfigService) uploaderForView(typ string, view *StorageView) (storageUploader, error) {
|
||||||
if view == nil || !view.Enabled {
|
if view == nil || !view.Enabled {
|
||||||
return nil, fmt.Errorf("%s storage not configured", typ)
|
return nil, fmt.Errorf("%s storage not configured", typ)
|
||||||
}
|
}
|
||||||
@@ -171,7 +196,38 @@ func (s *StorageConfigService) uploadOne(ctx context.Context, uploader storageUp
|
|||||||
}
|
}
|
||||||
result.Uploaded++
|
result.Uploaded++
|
||||||
result.Bytes += size
|
result.Bytes += size
|
||||||
addUploadItem(result, CloudUploadResultItem{Source: localPath, Target: remotePath, Action: "upload", Size: size})
|
action := "upload"
|
||||||
|
mode := resolveCloudUploadTransferMode(in.TransferMode)
|
||||||
|
if mode == TransferMove {
|
||||||
|
if err := os.Remove(localPath); err != nil {
|
||||||
|
addUploadError(result, localPath, remotePath, fmt.Errorf("uploaded but failed to remove local source: %w", err))
|
||||||
|
addUploadItem(result, CloudUploadResultItem{Source: localPath, Target: remotePath, Action: "upload", Size: size, Reason: "source remove failed"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
result.Moved++
|
||||||
|
action = "move"
|
||||||
|
}
|
||||||
|
addUploadItem(result, CloudUploadResultItem{Source: localPath, Target: remotePath, Action: action, Size: size})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *StorageConfigService) settingValue(ctx context.Context, key string) string {
|
||||||
|
if s == nil || s.repo == nil || s.repo.Setting == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
v, _ := s.repo.Setting.Get(ctx, key)
|
||||||
|
return strings.TrimSpace(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func resolveCloudUploadTransferMode(values ...string) TransferMode {
|
||||||
|
for _, value := range values {
|
||||||
|
switch strings.ToLower(strings.TrimSpace(value)) {
|
||||||
|
case "move", "移动":
|
||||||
|
return TransferMove
|
||||||
|
case "copy", "复制":
|
||||||
|
return TransferCopy
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return TransferCopy
|
||||||
}
|
}
|
||||||
|
|
||||||
func eligibleCloudUploadFile(localPath string, includeSidecars bool) bool {
|
func eligibleCloudUploadFile(localPath string, includeSidecars bool) bool {
|
||||||
|
|||||||
@@ -48,8 +48,9 @@ func TestStorageConfigUploadLocalToAlist(t *testing.T) {
|
|||||||
if _, err := storage.Save(t.Context(), StorageInput{
|
if _, err := storage.Save(t.Context(), StorageInput{
|
||||||
Type: "alist",
|
Type: "alist",
|
||||||
Config: map[string]any{
|
Config: map[string]any{
|
||||||
"server": alist.URL,
|
"server": alist.URL,
|
||||||
"token": "alist-token",
|
"token": "alist-token",
|
||||||
|
"transfer_enabled": "true",
|
||||||
},
|
},
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -122,8 +123,9 @@ func TestStorageConfigUploadLocalToOpenListAPI(t *testing.T) {
|
|||||||
if _, err := storage.Save(t.Context(), StorageInput{
|
if _, err := storage.Save(t.Context(), StorageInput{
|
||||||
Type: "openlist",
|
Type: "openlist",
|
||||||
Config: map[string]any{
|
Config: map[string]any{
|
||||||
"server": openlist.URL,
|
"server": openlist.URL,
|
||||||
"token": "openlist-token",
|
"token": "openlist-token",
|
||||||
|
"transfer_enabled": "true",
|
||||||
},
|
},
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -195,9 +197,10 @@ func TestStorageConfigUploadLocalToOpenListAPIWithUsernamePassword(t *testing.T)
|
|||||||
if _, err := storage.Save(t.Context(), StorageInput{
|
if _, err := storage.Save(t.Context(), StorageInput{
|
||||||
Type: "openlist",
|
Type: "openlist",
|
||||||
Config: map[string]any{
|
Config: map[string]any{
|
||||||
"server": openlist.URL,
|
"server": openlist.URL,
|
||||||
"username": "alice",
|
"username": "alice",
|
||||||
"password": "secret",
|
"password": "secret",
|
||||||
|
"transfer_enabled": "true",
|
||||||
},
|
},
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -285,8 +288,9 @@ func TestSchedulerCloudUploadUsesConfiguredLocalSource(t *testing.T) {
|
|||||||
if _, err := storage.Save(t.Context(), StorageInput{
|
if _, err := storage.Save(t.Context(), StorageInput{
|
||||||
Type: "alist",
|
Type: "alist",
|
||||||
Config: map[string]any{
|
Config: map[string]any{
|
||||||
"server": alist.URL,
|
"server": alist.URL,
|
||||||
"token": "token",
|
"token": "token",
|
||||||
|
"transfer_enabled": "true",
|
||||||
},
|
},
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -337,9 +341,10 @@ func TestStorageConfigUploadLocalToCloudDrive2(t *testing.T) {
|
|||||||
if _, err := storage.Save(t.Context(), StorageInput{
|
if _, err := storage.Save(t.Context(), StorageInput{
|
||||||
Type: "clouddrive2",
|
Type: "clouddrive2",
|
||||||
Config: map[string]any{
|
Config: map[string]any{
|
||||||
"url": dav.URL + "/dav",
|
"url": dav.URL + "/dav",
|
||||||
"username": "user",
|
"username": "user",
|
||||||
"password": "pass",
|
"password": "pass",
|
||||||
|
"transfer_enabled": "true",
|
||||||
},
|
},
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -362,13 +367,91 @@ func TestStorageConfigUploadLocalToCloudDrive2(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStorageConfigUploadLocalRequiresTransferEnabled(t *testing.T) {
|
||||||
|
_, storage := newStorageUploadTestService(t)
|
||||||
|
if _, err := storage.Save(t.Context(), StorageInput{
|
||||||
|
Type: "alist",
|
||||||
|
Config: map[string]any{
|
||||||
|
"server": "http://alist.test",
|
||||||
|
"token": "token",
|
||||||
|
},
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
source := t.TempDir()
|
||||||
|
if err := os.WriteFile(filepath.Join(source, "Movie.mkv"), []byte("movie"), 0o644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
_, err := storage.UploadLocal(t.Context(), CloudUploadInput{
|
||||||
|
Type: "alist",
|
||||||
|
SourcePath: source,
|
||||||
|
DestPath: "/MediaStationGo",
|
||||||
|
Recursive: true,
|
||||||
|
})
|
||||||
|
if err == nil || !strings.Contains(err.Error(), "transfer is disabled") {
|
||||||
|
t.Fatalf("upload error = %v, want transfer disabled", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStorageConfigUploadLocalMoveDeletesSourceAfterUpload(t *testing.T) {
|
||||||
|
var uploaded []string
|
||||||
|
alist := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
switch r.URL.Path {
|
||||||
|
case "/api/fs/mkdir":
|
||||||
|
_, _ = w.Write([]byte(`{"code":200}`))
|
||||||
|
case "/api/fs/get":
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
case "/api/fs/put":
|
||||||
|
decoded, _ := url.PathUnescape(r.Header.Get("File-Path"))
|
||||||
|
uploaded = append(uploaded, decoded)
|
||||||
|
_, _ = w.Write([]byte(`{"code":200}`))
|
||||||
|
default:
|
||||||
|
t.Fatalf("unexpected alist path %s", r.URL.Path)
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
defer alist.Close()
|
||||||
|
|
||||||
|
_, storage := newStorageUploadTestService(t)
|
||||||
|
if _, err := storage.Save(t.Context(), StorageInput{
|
||||||
|
Type: "alist",
|
||||||
|
Config: map[string]any{
|
||||||
|
"server": alist.URL,
|
||||||
|
"token": "token",
|
||||||
|
"transfer_enabled": "true",
|
||||||
|
"transfer_mode": "move",
|
||||||
|
},
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
source := t.TempDir()
|
||||||
|
file := filepath.Join(source, "Movie.mkv")
|
||||||
|
if err := os.WriteFile(file, []byte("movie"), 0o644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
res, err := storage.UploadLocal(t.Context(), CloudUploadInput{
|
||||||
|
Type: "alist",
|
||||||
|
SourcePath: source,
|
||||||
|
DestPath: "/MediaStationGo",
|
||||||
|
Recursive: true,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("upload local move: %v", err)
|
||||||
|
}
|
||||||
|
if res.Uploaded != 1 || res.Moved != 1 || len(uploaded) != 1 {
|
||||||
|
t.Fatalf("result = %+v uploaded=%#v", res, uploaded)
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(file); !os.IsNotExist(err) {
|
||||||
|
t.Fatalf("source should be removed after move upload, stat err=%v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func newStorageUploadTestService(t *testing.T) (*repository.Container, *StorageConfigService) {
|
func newStorageUploadTestService(t *testing.T) (*repository.Container, *StorageConfigService) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if err := db.AutoMigrate(&model.StorageConfig{}, &model.Setting{}); err != nil {
|
if err := db.AutoMigrate(&model.StorageConfig{}, &model.Setting{}, &model.Library{}, &model.Media{}); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
repos := repository.New(db)
|
repos := repository.New(db)
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ type BackgroundTask struct {
|
|||||||
DestPath string `json:"dest_path,omitempty"`
|
DestPath string `json:"dest_path,omitempty"`
|
||||||
Message string `json:"message,omitempty"`
|
Message string `json:"message,omitempty"`
|
||||||
Error string `json:"error,omitempty"`
|
Error string `json:"error,omitempty"`
|
||||||
|
Details []string `json:"details,omitempty"`
|
||||||
Metrics map[string]int64 `json:"metrics,omitempty"`
|
Metrics map[string]int64 `json:"metrics,omitempty"`
|
||||||
StartedAt time.Time `json:"started_at"`
|
StartedAt time.Time `json:"started_at"`
|
||||||
UpdatedAt time.Time `json:"updated_at"`
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
@@ -43,6 +44,7 @@ type TaskUpdate struct {
|
|||||||
SourcePath string
|
SourcePath string
|
||||||
DestPath string
|
DestPath string
|
||||||
Message string
|
Message string
|
||||||
|
Details []string
|
||||||
Metrics map[string]int64
|
Metrics map[string]int64
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -206,6 +208,9 @@ func applyTaskUpdate(task *BackgroundTask, update TaskUpdate) {
|
|||||||
if update.Message != "" {
|
if update.Message != "" {
|
||||||
task.Message = update.Message
|
task.Message = update.Message
|
||||||
}
|
}
|
||||||
|
if update.Details != nil {
|
||||||
|
task.Details = append([]string(nil), update.Details...)
|
||||||
|
}
|
||||||
if update.Metrics != nil {
|
if update.Metrics != nil {
|
||||||
task.Metrics = cloneTaskMetrics(update.Metrics)
|
task.Metrics = cloneTaskMetrics(update.Metrics)
|
||||||
}
|
}
|
||||||
@@ -213,6 +218,9 @@ func applyTaskUpdate(task *BackgroundTask, update TaskUpdate) {
|
|||||||
|
|
||||||
func cloneBackgroundTask(task BackgroundTask) BackgroundTask {
|
func cloneBackgroundTask(task BackgroundTask) BackgroundTask {
|
||||||
task.Metrics = cloneTaskMetrics(task.Metrics)
|
task.Metrics = cloneTaskMetrics(task.Metrics)
|
||||||
|
if task.Details != nil {
|
||||||
|
task.Details = append([]string(nil), task.Details...)
|
||||||
|
}
|
||||||
if task.FinishedAt != nil {
|
if task.FinishedAt != nil {
|
||||||
finishedAt := *task.FinishedAt
|
finishedAt := *task.FinishedAt
|
||||||
task.FinishedAt = &finishedAt
|
task.FinishedAt = &finishedAt
|
||||||
@@ -278,6 +286,43 @@ func OrganizeTaskMetrics(res *OrganizeResult) map[string]int64 {
|
|||||||
return metrics
|
return metrics
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func OrganizeTaskDetails(res *OrganizeResult, limit int) []string {
|
||||||
|
if res == nil || limit <= 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
out := make([]string, 0, limit)
|
||||||
|
for _, line := range res.Errors {
|
||||||
|
line = strings.TrimSpace(line)
|
||||||
|
if line == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
out = append(out, "错误: "+line)
|
||||||
|
if len(out) >= limit {
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, item := range res.Items {
|
||||||
|
if item.Action != "error" && item.Action != "skip" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
line := strings.TrimSpace(item.Source)
|
||||||
|
if item.Reason != "" {
|
||||||
|
line += ": " + strings.TrimSpace(item.Reason)
|
||||||
|
}
|
||||||
|
if line == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
out = append(out, item.Action+": "+line)
|
||||||
|
if len(out) >= limit {
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(out) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
func OrganizeSkipReasonCounts(res *OrganizeResult) map[string]int {
|
func OrganizeSkipReasonCounts(res *OrganizeResult) map[string]int {
|
||||||
if res == nil || len(res.Items) == 0 {
|
if res == nil || len(res.Items) == 0 {
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -36,13 +36,14 @@ export interface CloudUploadResult {
|
|||||||
source_path: string
|
source_path: string
|
||||||
dest_path: string
|
dest_path: string
|
||||||
uploaded: number
|
uploaded: number
|
||||||
|
moved?: number
|
||||||
skipped: number
|
skipped: number
|
||||||
bytes: number
|
bytes: number
|
||||||
errors?: string[]
|
errors?: string[]
|
||||||
items?: Array<{
|
items?: Array<{
|
||||||
source: string
|
source: string
|
||||||
target: string
|
target: string
|
||||||
action: 'upload' | 'skip' | 'error'
|
action: 'upload' | 'move' | 'skip' | 'error'
|
||||||
size?: number
|
size?: number
|
||||||
reason?: string
|
reason?: string
|
||||||
}>
|
}>
|
||||||
@@ -103,6 +104,7 @@ export const storageAPI = {
|
|||||||
recursive: boolean
|
recursive: boolean
|
||||||
include_sidecars: boolean
|
include_sidecars: boolean
|
||||||
overwrite: boolean
|
overwrite: boolean
|
||||||
|
transfer_mode?: 'copy' | 'move'
|
||||||
},
|
},
|
||||||
) =>
|
) =>
|
||||||
api
|
api
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ export interface BackgroundTask {
|
|||||||
dest_path?: string
|
dest_path?: string
|
||||||
message?: string
|
message?: string
|
||||||
error?: string
|
error?: string
|
||||||
|
details?: string[]
|
||||||
metrics?: Record<string, number>
|
metrics?: Record<string, number>
|
||||||
started_at: string
|
started_at: string
|
||||||
updated_at: string
|
updated_at: string
|
||||||
|
|||||||
@@ -107,6 +107,10 @@ function settingOn(value: string): boolean {
|
|||||||
return ['1', 'true', 'yes', 'on', 'enabled', '启用', '开启'].includes(value.trim().toLowerCase())
|
return ['1', 'true', 'yes', 'on', 'enabled', '启用', '开启'].includes(value.trim().toLowerCase())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isCloudLibraryPath(value: string): boolean {
|
||||||
|
return value.trim().toLowerCase().startsWith('cloud://')
|
||||||
|
}
|
||||||
|
|
||||||
// FileManagerPage provides a focused local storage view:
|
// FileManagerPage provides a focused local storage view:
|
||||||
// browse allowed roots, optionally recurse, and perform safe local operations.
|
// browse allowed roots, optionally recurse, and perform safe local operations.
|
||||||
export function FileManagerPage() {
|
export function FileManagerPage() {
|
||||||
@@ -145,6 +149,10 @@ export function FileManagerPage() {
|
|||||||
if (data?.path) return data.path
|
if (data?.path) return data.path
|
||||||
return path
|
return path
|
||||||
}, [data?.path, path])
|
}, [data?.path, path])
|
||||||
|
const localLibraries = useMemo(
|
||||||
|
() => libraries.filter((library) => !isCloudLibraryPath(library.path)),
|
||||||
|
[libraries],
|
||||||
|
)
|
||||||
|
|
||||||
const refresh = useCallback(() => {
|
const refresh = useCallback(() => {
|
||||||
setLoading(true)
|
setLoading(true)
|
||||||
@@ -193,11 +201,15 @@ export function FileManagerPage() {
|
|||||||
}, [path])
|
}, [path])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const lib = libraries.find((item) => item.id === organizeLibraryID)
|
if (!organizeLibraryID) return
|
||||||
if (!lib) return
|
const lib = localLibraries.find((item) => item.id === organizeLibraryID)
|
||||||
|
if (!lib) {
|
||||||
|
setOrganizeLibraryID('')
|
||||||
|
return
|
||||||
|
}
|
||||||
setOrganizeDestPath(lib.path)
|
setOrganizeDestPath(lib.path)
|
||||||
setOrganizeMediaType(lib.type || 'auto')
|
setOrganizeMediaType(lib.type || 'auto')
|
||||||
}, [libraries, organizeLibraryID])
|
}, [localLibraries, organizeLibraryID])
|
||||||
|
|
||||||
const changeAutoConfig = (key: keyof AutoOrganizeConfig, value: string) => {
|
const changeAutoConfig = (key: keyof AutoOrganizeConfig, value: string) => {
|
||||||
setAutoConfig((current) => ({ ...current, [key]: value }))
|
setAutoConfig((current) => ({ ...current, [key]: value }))
|
||||||
@@ -551,12 +563,15 @@ export function FileManagerPage() {
|
|||||||
onChange={(event) => setOrganizeLibraryID(event.target.value)}
|
onChange={(event) => setOrganizeLibraryID(event.target.value)}
|
||||||
>
|
>
|
||||||
<option value="">手动填写目的路径</option>
|
<option value="">手动填写目的路径</option>
|
||||||
{libraries.map((library) => (
|
{localLibraries.map((library) => (
|
||||||
<option key={library.id} value={library.id}>
|
<option key={library.id} value={library.id}>
|
||||||
{library.name}({library.type})— {library.path}
|
{library.name}({library.type})— {library.path}
|
||||||
</option>
|
</option>
|
||||||
))}
|
))}
|
||||||
</select>
|
</select>
|
||||||
|
<span className="text-[11px] text-sand-500">
|
||||||
|
手动整理只写入本地可写媒体库;网盘请到“外部存储”中挂载、扫描或转存。
|
||||||
|
</span>
|
||||||
</label>
|
</label>
|
||||||
<label className="space-y-1">
|
<label className="space-y-1">
|
||||||
<span className="text-xs text-ink-50">目的路径</span>
|
<span className="text-xs text-ink-50">目的路径</span>
|
||||||
|
|||||||
@@ -325,7 +325,7 @@ const GROUPS: SettingGroup[] = [
|
|||||||
key: 'cloud.upload_auto_enabled',
|
key: 'cloud.upload_auto_enabled',
|
||||||
label: '启用自动转存',
|
label: '启用自动转存',
|
||||||
type: 'toggle',
|
type: 'toggle',
|
||||||
hint: '开启后后台会按间隔扫描本地源目录,把视频、NFO、海报、字幕复制到目标存储;不会删除本地源文件。',
|
hint: '开启后后台会按间隔扫描本地源目录,把视频、NFO、海报、字幕转存到目标存储;还需要在外部存储页开启该目标的“允许转存写入”。',
|
||||||
defaultValue: 'false',
|
defaultValue: 'false',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -373,6 +373,17 @@ const GROUPS: SettingGroup[] = [
|
|||||||
type: 'toggle',
|
type: 'toggle',
|
||||||
defaultValue: 'false',
|
defaultValue: 'false',
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
key: 'cloud.upload_transfer_mode',
|
||||||
|
label: '自动转存方式',
|
||||||
|
type: 'select',
|
||||||
|
defaultValue: 'copy',
|
||||||
|
hint: '复制会保留本地源文件;移动只在上传成功后删除本地文件。',
|
||||||
|
options: [
|
||||||
|
{ value: 'copy', label: '复制' },
|
||||||
|
{ value: 'move', label: '移动' },
|
||||||
|
],
|
||||||
|
},
|
||||||
{
|
{
|
||||||
key: 'cloud.upload_interval_seconds',
|
key: 'cloud.upload_interval_seconds',
|
||||||
label: '自动转存间隔秒数',
|
label: '自动转存间隔秒数',
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { FormEvent, useEffect, useMemo, useState } from 'react'
|
import { FormEvent, useEffect, useMemo, useState } from 'react'
|
||||||
import { Cloud, FileVideo, Folder, Loader2, LogOut, PauseCircle, QrCode, RefreshCw, Save, Send, Trash2, Upload } from 'lucide-react'
|
import { Cloud, Copy, FileVideo, Folder, Loader2, LogOut, Move, PauseCircle, QrCode, RefreshCw, Save, Send, Trash2, Upload } from 'lucide-react'
|
||||||
import toast from 'react-hot-toast'
|
import toast from 'react-hot-toast'
|
||||||
|
|
||||||
import { libraryAPI } from '../api/library'
|
import { libraryAPI } from '../api/library'
|
||||||
@@ -26,6 +26,7 @@ const TYPE_LABEL: Record<string, string> = {
|
|||||||
clouddrive2: 'CloudDrive2',
|
clouddrive2: 'CloudDrive2',
|
||||||
}
|
}
|
||||||
const PATH_BASED_CLOUD = new Set<StorageType>(['openlist', 'clouddrive2'])
|
const PATH_BASED_CLOUD = new Set<StorageType>(['openlist', 'clouddrive2'])
|
||||||
|
const TRANSFER_SUPPORTED_TYPES = new Set<StorageType>(['alist', 'openlist', 'webdav', 'clouddrive2'])
|
||||||
|
|
||||||
function normalizeCloudDisplayPath(value: string) {
|
function normalizeCloudDisplayPath(value: string) {
|
||||||
let text = value.trim()
|
let text = value.trim()
|
||||||
@@ -151,6 +152,7 @@ const FIELD_DEFS: Record<StorageType, { key: string; label: string; secret?: boo
|
|||||||
|
|
||||||
function StorageForm({ type }: { type: StorageType }) {
|
function StorageForm({ type }: { type: StorageType }) {
|
||||||
const fields = useMemo(() => FIELD_DEFS[type], [type])
|
const fields = useMemo(() => FIELD_DEFS[type], [type])
|
||||||
|
const transferSupported = TRANSFER_SUPPORTED_TYPES.has(type)
|
||||||
const [config, setConfig] = useState<Record<string, string>>({})
|
const [config, setConfig] = useState<Record<string, string>>({})
|
||||||
const [enabled, setEnabled] = useState(true)
|
const [enabled, setEnabled] = useState(true)
|
||||||
const [loading, setLoading] = useState(true)
|
const [loading, setLoading] = useState(true)
|
||||||
@@ -163,6 +165,9 @@ function StorageForm({ type }: { type: StorageType }) {
|
|||||||
try {
|
try {
|
||||||
const r = await storageAPI.get(type)
|
const r = await storageAPI.get(type)
|
||||||
const next: Record<string, string> = {}
|
const next: Record<string, string> = {}
|
||||||
|
for (const [key, value] of Object.entries(r.config ?? {})) {
|
||||||
|
next[key] = value === '********' ? '' : String(value ?? '')
|
||||||
|
}
|
||||||
for (const f of fields) {
|
for (const f of fields) {
|
||||||
const v = r.config?.[f.key]
|
const v = r.config?.[f.key]
|
||||||
// List() redacts secrets to "********"; show empty so the user
|
// List() redacts secrets to "********"; show empty so the user
|
||||||
@@ -221,14 +226,14 @@ function StorageForm({ type }: { type: StorageType }) {
|
|||||||
const onLogout = async () => {
|
const onLogout = async () => {
|
||||||
const ok = await confirmAction({
|
const ok = await confirmAction({
|
||||||
title: '退出云盘登录',
|
title: '退出云盘登录',
|
||||||
message: `将清空「${TYPE_LABEL[type] ?? type}」保存的 Cookie / Token / 密码并停用该外部存储;不会删除网盘文件,也不会删除已挂载媒体库。`,
|
message: `将清空「${TYPE_LABEL[type] ?? type}」保存的 Cookie / Token / 密码并停用该外部存储,同时移除本项目中的对应网盘挂载和媒体记录;不会删除网盘里的真实文件。`,
|
||||||
confirmText: '退出登录',
|
confirmText: '退出登录',
|
||||||
})
|
})
|
||||||
if (!ok) return
|
if (!ok) return
|
||||||
setLoggingOut(true)
|
setLoggingOut(true)
|
||||||
try {
|
try {
|
||||||
await storageAPI.logout(type)
|
await storageAPI.logout(type)
|
||||||
toast.success('已退出云盘登录并停用该存储')
|
toast.success('已退出云盘登录、停用该存储并清理本项目挂载')
|
||||||
await refresh()
|
await refresh()
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
const msg =
|
const msg =
|
||||||
@@ -280,6 +285,39 @@ function StorageForm({ type }: { type: StorageType }) {
|
|||||||
/>
|
/>
|
||||||
启用
|
启用
|
||||||
</label>
|
</label>
|
||||||
|
{transferSupported && (
|
||||||
|
<div className="rounded-xl border border-blue-100 bg-blue-50/60 p-3">
|
||||||
|
<div className="mb-2 text-sm font-semibold text-ink-600">转存写入权限</div>
|
||||||
|
<p className="mb-3 text-xs text-ink-50">
|
||||||
|
挂载/扫描只需要读取;只有手动开启这里,才允许把本地文件转存写入到该外部存储。可随时关闭。
|
||||||
|
</p>
|
||||||
|
<div className="flex flex-wrap items-center gap-4 text-sm text-ink-100">
|
||||||
|
<label className="flex items-center gap-2">
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
className="h-4 w-4 accent-primary-400"
|
||||||
|
checked={(config.transfer_enabled ?? '').toLowerCase() === 'true'}
|
||||||
|
onChange={(event) => setConfig((current) => ({ ...current, transfer_enabled: event.target.checked ? 'true' : 'false' }))}
|
||||||
|
/>
|
||||||
|
允许转存写入
|
||||||
|
</label>
|
||||||
|
<label className="flex items-center gap-2">
|
||||||
|
<span>转存方式</span>
|
||||||
|
<select
|
||||||
|
className="input-base w-32"
|
||||||
|
value={config.transfer_mode === 'move' ? 'move' : 'copy'}
|
||||||
|
onChange={(event) => setConfig((current) => ({ ...current, transfer_mode: event.target.value }))}
|
||||||
|
>
|
||||||
|
<option value="copy">复制</option>
|
||||||
|
<option value="move">移动</option>
|
||||||
|
</select>
|
||||||
|
</label>
|
||||||
|
<span className="text-xs text-ink-50">
|
||||||
|
复制保留本地源文件;移动只在上传成功后删除本地文件。
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
<div className="flex justify-end gap-2 pt-2">
|
<div className="flex justify-end gap-2 pt-2">
|
||||||
{isCloud(type) && (
|
{isCloud(type) && (
|
||||||
<button
|
<button
|
||||||
@@ -306,13 +344,17 @@ function StorageForm({ type }: { type: StorageType }) {
|
|||||||
保存
|
保存
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<StorageUploadPanel type={type} />
|
<StorageUploadPanel
|
||||||
|
type={type}
|
||||||
|
transferEnabled={(config.transfer_enabled ?? '').toLowerCase() === 'true'}
|
||||||
|
transferMode={config.transfer_mode === 'move' ? 'move' : 'copy'}
|
||||||
|
/>
|
||||||
{isCloud(type) && <CloudBrowser type={type} />}
|
{isCloud(type) && <CloudBrowser type={type} />}
|
||||||
</form>
|
</form>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function StorageUploadPanel({ type }: { type: StorageType }) {
|
function StorageUploadPanel({ type, transferEnabled, transferMode }: { type: StorageType; transferEnabled: boolean; transferMode: 'copy' | 'move' }) {
|
||||||
const [sourcePath, setSourcePath] = useState('')
|
const [sourcePath, setSourcePath] = useState('')
|
||||||
const [destPath, setDestPath] = useState('/MediaStationGo')
|
const [destPath, setDestPath] = useState('/MediaStationGo')
|
||||||
const [recursive, setRecursive] = useState(true)
|
const [recursive, setRecursive] = useState(true)
|
||||||
@@ -320,7 +362,7 @@ function StorageUploadPanel({ type }: { type: StorageType }) {
|
|||||||
const [overwrite, setOverwrite] = useState(false)
|
const [overwrite, setOverwrite] = useState(false)
|
||||||
const [busy, setBusy] = useState(false)
|
const [busy, setBusy] = useState(false)
|
||||||
|
|
||||||
const supported = type === 'alist' || type === 'openlist' || type === 'webdav' || type === 'clouddrive2'
|
const supported = TRANSFER_SUPPORTED_TYPES.has(type)
|
||||||
|
|
||||||
const submit = async () => {
|
const submit = async () => {
|
||||||
if (!supported) {
|
if (!supported) {
|
||||||
@@ -331,6 +373,10 @@ function StorageUploadPanel({ type }: { type: StorageType }) {
|
|||||||
toast.error('请填写本地源目录或文件路径')
|
toast.error('请填写本地源目录或文件路径')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if (!transferEnabled) {
|
||||||
|
toast.error('请先开启“允许转存写入”并保存该外部存储配置')
|
||||||
|
return
|
||||||
|
}
|
||||||
setBusy(true)
|
setBusy(true)
|
||||||
try {
|
try {
|
||||||
const { result, error } = await storageAPI.uploadLocal(type, {
|
const { result, error } = await storageAPI.uploadLocal(type, {
|
||||||
@@ -339,9 +385,11 @@ function StorageUploadPanel({ type }: { type: StorageType }) {
|
|||||||
recursive,
|
recursive,
|
||||||
include_sidecars: includeSidecars,
|
include_sidecars: includeSidecars,
|
||||||
overwrite,
|
overwrite,
|
||||||
|
transfer_mode: transferMode,
|
||||||
})
|
})
|
||||||
const errText = error || (result.errors && result.errors.length > 0 ? ` · 错误 ${result.errors.length}` : '')
|
const errText = error || (result.errors && result.errors.length > 0 ? ` · 错误 ${result.errors.length}` : '')
|
||||||
toast.success(`转存完成:上传 ${result.uploaded} · 跳过 ${result.skipped} · ${fmtBytes(result.bytes)}${errText}`)
|
const movedText = result.moved ? ` · 移动 ${result.moved}` : ''
|
||||||
|
toast.success(`转存完成:上传 ${result.uploaded}${movedText} · 跳过 ${result.skipped} · ${fmtBytes(result.bytes)}${errText}`)
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
toast.error((err as { response?: { data?: { error?: string } } })?.response?.data?.error ?? '转存失败')
|
toast.error((err as { response?: { data?: { error?: string } } })?.response?.data?.error ?? '转存失败')
|
||||||
} finally {
|
} finally {
|
||||||
@@ -354,10 +402,12 @@ function StorageUploadPanel({ type }: { type: StorageType }) {
|
|||||||
<div className="mb-3 flex items-start justify-between gap-3">
|
<div className="mb-3 flex items-start justify-between gap-3">
|
||||||
<div>
|
<div>
|
||||||
<h3 className="flex items-center gap-2 font-display text-base font-semibold text-ink-600">
|
<h3 className="flex items-center gap-2 font-display text-base font-semibold text-ink-600">
|
||||||
<Upload size={16} /> 本地媒体转存到此存储
|
{transferMode === 'move' ? <Move size={16} /> : <Copy size={16} />} 本地媒体转存到此存储
|
||||||
</h3>
|
</h3>
|
||||||
<p className="mt-1 text-xs text-ink-50">
|
<p className="mt-1 text-xs text-ink-50">
|
||||||
复制本地媒体文件到外部存储,保留本地源文件;自动跳过远端已存在文件。
|
{transferMode === 'move'
|
||||||
|
? '移动模式会先上传到外部存储,上传成功后才删除本地源文件;远端已存在且未覆盖时不会删除本地。'
|
||||||
|
: '复制本地媒体文件到外部存储,保留本地源文件;自动跳过远端已存在文件。'}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
{!supported && (
|
{!supported && (
|
||||||
@@ -414,9 +464,9 @@ function StorageUploadPanel({ type }: { type: StorageType }) {
|
|||||||
<input type="checkbox" className="h-4 w-4 accent-primary-400" checked={overwrite} onChange={(event) => setOverwrite(event.target.checked)} />
|
<input type="checkbox" className="h-4 w-4 accent-primary-400" checked={overwrite} onChange={(event) => setOverwrite(event.target.checked)} />
|
||||||
覆盖远端同名文件
|
覆盖远端同名文件
|
||||||
</label>
|
</label>
|
||||||
<button type="button" className="neon-button ml-auto" disabled={busy || !supported} onClick={submit}>
|
<button type="button" className="neon-button ml-auto" disabled={busy || !supported || !transferEnabled} onClick={submit}>
|
||||||
{busy ? <Loader2 size={16} className="animate-spin" /> : <Upload size={16} />}
|
{busy ? <Loader2 size={16} className="animate-spin" /> : transferMode === 'move' ? <Move size={16} /> : <Upload size={16} />}
|
||||||
{busy ? '转存中…' : '开始转存'}
|
{busy ? '转存中…' : transferMode === 'move' ? '开始移动转存' : '开始复制转存'}
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -94,6 +94,15 @@ function BackgroundTaskTable({ tasks, empty }: { tasks: BackgroundTask[]; empty:
|
|||||||
{formatMetrics(task.metrics) && (
|
{formatMetrics(task.metrics) && (
|
||||||
<div className="mt-1 text-xs text-sand-500">{formatMetrics(task.metrics)}</div>
|
<div className="mt-1 text-xs text-sand-500">{formatMetrics(task.metrics)}</div>
|
||||||
)}
|
)}
|
||||||
|
{task.details && task.details.length > 0 && (
|
||||||
|
<div className="mt-2 max-h-28 overflow-auto rounded-lg border border-gray-200 bg-gray-50 p-2 font-mono text-[11px] text-sand-600">
|
||||||
|
{task.details.map((line, index) => (
|
||||||
|
<div key={`${task.id}-detail-${index}`} className="break-all">
|
||||||
|
{line}
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</td>
|
</td>
|
||||||
<td className="text-ink-100">
|
<td className="text-ink-100">
|
||||||
{new Date(task.finished_at || task.updated_at || task.started_at).toLocaleTimeString()}
|
{new Date(task.finished_at || task.updated_at || task.started_at).toLocaleTimeString()}
|
||||||
|
|||||||
Reference in New Issue
Block a user