Add download preview flow and search fallback
Some checks failed
build-push / docker (push) Has been cancelled

This commit is contained in:
AI Assistant
2026-03-12 16:01:19 +09:00
parent 5d0ba114ee
commit e136650790
5 changed files with 312 additions and 48 deletions

View File

@@ -64,12 +64,23 @@ func (h *Hub) Remove(conn *websocket.Conn) {
_ = conn.Close()
}
type PreviewResponse struct {
Title string `json:"title"`
Thumbnail string `json:"thumbnail"`
Duration string `json:"duration"`
DurationSeconds int `json:"durationSeconds"`
StartDefault string `json:"startDefault"`
EndDefault string `json:"endDefault"`
Qualities []map[string]any `json:"qualities"`
}
func RegisterRoutes(router *gin.Engine, app *App) {
router.GET("/healthz", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "ok"})
})
router.GET("/ws", app.handleWS)
router.GET("/api/history/check", app.checkDuplicate)
router.POST("/api/download/preview", app.previewDownload)
router.POST("/api/upload", app.uploadFile)
router.POST("/api/download", app.startDownload)
router.POST("/api/search", app.searchMedia)
@@ -132,6 +143,7 @@ func (a *App) startDownload(c *gin.Context) {
URL string `json:"url"`
Start string `json:"start"`
End string `json:"end"`
Quality string `json:"quality"`
Force bool `json:"force"`
}
if err := c.ShouldBindJSON(&req); err != nil {
@@ -157,13 +169,46 @@ func (a *App) startDownload(c *gin.Context) {
return
}
go a.runDownload(recordID, req.URL, req.Start, req.End, outputPath)
quality := strings.TrimSpace(req.Quality)
if quality == "" {
quality = "best"
}
go a.runDownload(recordID, req.URL, req.Start, req.End, quality, outputPath)
c.JSON(http.StatusAccepted, gin.H{"message": "download started", "recordId": recordID})
}
func (a *App) runDownload(recordID int64, url, start, end, outputPath string) {
func (a *App) previewDownload(c *gin.Context) {
var req struct {
URL string `json:"url"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if strings.TrimSpace(req.URL) == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "url is required"})
return
}
cmd := exec.Command("python3", a.WorkerScript, "--mode", "probe", "--url", req.URL, "--output", filepath.Join(a.DownloadsDir, "probe.tmp"))
output, err := cmd.CombinedOutput()
if err != nil {
c.JSON(http.StatusBadGateway, gin.H{"error": strings.TrimSpace(string(output))})
return
}
var preview PreviewResponse
if err := json.Unmarshal(output, &preview); err != nil {
c.JSON(http.StatusBadGateway, gin.H{"error": "invalid probe response"})
return
}
c.JSON(http.StatusOK, preview)
}
func (a *App) runDownload(recordID int64, url, start, end, quality, outputPath string) {
a.Hub.Broadcast("progress", gin.H{"type": "download", "status": "queued", "progress": 0, "url": url})
cmd := exec.Command("python3", a.WorkerScript, "--url", url, "--start", start, "--end", end, "--output", outputPath)
cmd := exec.Command("python3", a.WorkerScript, "--url", url, "--start", start, "--end", end, "--quality", quality, "--output", outputPath)
stdout, err := cmd.StdoutPipe()
if err != nil {
a.Hub.Broadcast("progress", gin.H{"type": "download", "status": "error", "progress": 0, "message": err.Error()})
@@ -187,6 +232,9 @@ func (a *App) runDownload(recordID int64, url, start, end, outputPath string) {
a.Hub.Broadcast("progress", msg)
}
}
if err := scanner.Err(); err != nil {
a.Hub.Broadcast("progress", gin.H{"type": "download", "status": "error", "progress": 100, "message": err.Error()})
}
status := "completed"
if err := cmd.Wait(); err != nil {