mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-01 12:06:38 +08:00
fix: improve media file organization workflow
This commit is contained in:
@@ -6,25 +6,95 @@ import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/ShukeBta/MediaStationGo/internal/service"
|
||||
)
|
||||
|
||||
type fileFolderReq struct {
|
||||
Path string `json:"path"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type fileRenameReq struct {
|
||||
Path string `json:"path"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type fileTransferReq struct {
|
||||
SourcePath string `json:"source_path"`
|
||||
DestPath string `json:"dest_path"`
|
||||
TransferMode string `json:"transfer_mode"`
|
||||
}
|
||||
|
||||
func browseFilesHandler(svc *service.Container) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
path := c.Query("path")
|
||||
max, _ := strconv.Atoi(c.DefaultQuery("max", "1000"))
|
||||
listing, err := svc.FileManager.List(path, max)
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrPathOutOfBounds) {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, listing)
|
||||
recursive := strings.EqualFold(c.DefaultQuery("recursive", "false"), "true")
|
||||
listing, err := svc.FileManager.List(path, max, recursive)
|
||||
writeFileManagerResponse(c, listing, err)
|
||||
}
|
||||
}
|
||||
|
||||
func createFolderHandler(svc *service.Container) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
var req fileFolderReq
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
res, err := svc.FileManager.CreateFolder(req.Path, req.Name)
|
||||
writeFileManagerResponse(c, res, err)
|
||||
}
|
||||
}
|
||||
|
||||
func renameFileHandler(svc *service.Container) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
var req fileRenameReq
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
res, err := svc.FileManager.Rename(req.Path, req.Name)
|
||||
writeFileManagerResponse(c, res, err)
|
||||
}
|
||||
}
|
||||
|
||||
func deleteFileHandler(svc *service.Container) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
path := c.Query("path")
|
||||
if strings.TrimSpace(path) == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "path required"})
|
||||
return
|
||||
}
|
||||
err := svc.FileManager.Delete(path)
|
||||
writeFileManagerResponse(c, gin.H{"removed": true}, err)
|
||||
}
|
||||
}
|
||||
|
||||
func transferFileHandler(svc *service.Container) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
var req fileTransferReq
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
res, err := svc.FileManager.Transfer(req.SourcePath, req.DestPath, service.TransferMode(req.TransferMode))
|
||||
writeFileManagerResponse(c, res, err)
|
||||
}
|
||||
}
|
||||
|
||||
func writeFileManagerResponse(c *gin.Context, payload any, err error) {
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrPathOutOfBounds) || errors.Is(err, service.ErrRootMutation) {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, payload)
|
||||
}
|
||||
|
||||
@@ -19,6 +19,10 @@ type organizeReq struct {
|
||||
DestPath string `json:"dest_path"`
|
||||
TargetPath string `json:"target_path"` // deprecated alias for dest_path
|
||||
TransferMode string `json:"transfer_mode"`
|
||||
MediaType string `json:"media_type"`
|
||||
ScanAfter bool `json:"scan_after"`
|
||||
LibraryID string `json:"library_id"`
|
||||
DryRun bool `json:"dry_run"`
|
||||
}
|
||||
|
||||
// bindOrganizeOptions parses the optional JSON body into OrganizeOptions.
|
||||
@@ -26,6 +30,10 @@ type organizeReq struct {
|
||||
func bindOrganizeOptions(c *gin.Context) service.OrganizeOptions {
|
||||
var req organizeReq
|
||||
_ = c.ShouldBindJSON(&req)
|
||||
return organizeOptionsFromReq(req)
|
||||
}
|
||||
|
||||
func organizeOptionsFromReq(req organizeReq) service.OrganizeOptions {
|
||||
dest := strings.TrimSpace(req.DestPath)
|
||||
if dest == "" {
|
||||
dest = strings.TrimSpace(req.TargetPath)
|
||||
@@ -33,6 +41,8 @@ func bindOrganizeOptions(c *gin.Context) service.OrganizeOptions {
|
||||
opts := service.OrganizeOptions{
|
||||
SourcePath: strings.TrimSpace(req.SourcePath),
|
||||
DestPath: dest,
|
||||
MediaType: strings.TrimSpace(req.MediaType),
|
||||
DryRun: req.DryRun,
|
||||
}
|
||||
if m := strings.TrimSpace(req.TransferMode); m != "" {
|
||||
opts.TransferMode = service.TransferMode(m)
|
||||
@@ -68,7 +78,7 @@ func organizeLibraryHandler(svc *service.Container) gin.HandlerFunc {
|
||||
// dir + media dir) so the UI can offer them alongside registered libraries.
|
||||
func organizeSourcesHandler(svc *service.Container) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"sources": svc.Organizer.OrganizeSourceCandidates()})
|
||||
c.JSON(http.StatusOK, gin.H{"sources": svc.Organizer.OrganizeSourceCandidates(c.Request.Context())})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,12 +86,17 @@ func organizeSourcesHandler(svc *service.Container) gin.HandlerFunc {
|
||||
// download directory) into the destination with dedup + 洗版.
|
||||
func organizeDirectoryHandler(svc *service.Container) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
opts := bindOrganizeOptions(c)
|
||||
var req organizeReq
|
||||
_ = c.ShouldBindJSON(&req)
|
||||
opts := organizeOptionsFromReq(req)
|
||||
res, err := svc.Organizer.OrganizeDirectory(c.Request.Context(), opts)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if req.ScanAfter && !req.DryRun && svc.Scan != nil {
|
||||
res.Scans = svc.Scan.ScanLibrariesForPath(c.Request.Context(), res.DestPath, strings.TrimSpace(req.LibraryID))
|
||||
}
|
||||
c.JSON(http.StatusOK, res)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,6 +107,10 @@ func registerAuthenticatedRoutes(api *gin.RouterGroup, cfg *config.Config, svc *
|
||||
|
||||
// File browser (used by the library-path picker).
|
||||
authed.GET("/files", middleware.AdminRequired(), browseFilesHandler(svc))
|
||||
authed.POST("/files/folders", middleware.AdminRequired(), createFolderHandler(svc))
|
||||
authed.PUT("/files/rename", middleware.AdminRequired(), renameFileHandler(svc))
|
||||
authed.DELETE("/files", middleware.AdminRequired(), deleteFileHandler(svc))
|
||||
authed.POST("/files/transfer", middleware.AdminRequired(), transferFileHandler(svc))
|
||||
|
||||
// Disk usage breakdown.
|
||||
authed.GET("/storage", middleware.AdminRequired(), storageHandler(svc))
|
||||
|
||||
@@ -78,9 +78,15 @@ func schemaHandler(_ *service.Container) gin.HandlerFunc {
|
||||
"key": "organize",
|
||||
"label": "整理 & 刮削",
|
||||
"items": []gin.H{
|
||||
{"key": "organize.auto", "type": "toggle", "label": "整理源目录定时自动整理"},
|
||||
{"key": "organizer.auto_after_download", "type": "toggle"},
|
||||
{"key": "downloads.smart_classify", "type": "toggle"},
|
||||
{"key": "organizer.smart_classify", "type": "toggle"},
|
||||
{"key": "organize.source_dir", "type": "text", "label": "整理源目录"},
|
||||
{"key": "organize.target_dir", "type": "text", "label": "整理目的地目录"},
|
||||
{"key": "organize.transfer_mode", "type": "select", "label": "默认转移方式"},
|
||||
{"key": "organize.interval_seconds", "type": "number", "label": "自动整理间隔秒数"},
|
||||
{"key": "organize.keep_seeding", "type": "toggle", "label": "保种"},
|
||||
{"key": "organize.movie_format", "type": "text"},
|
||||
{"key": "organize.tv_format", "type": "text"},
|
||||
{"key": "organize.anime_format", "type": "text"},
|
||||
|
||||
Reference in New Issue
Block a user