From cbea8afa2d10154c634c26abc05211869136e14c Mon Sep 17 00:00:00 2001 From: Carl Pearson Date: Tue, 24 Sep 2024 05:48:17 -0600 Subject: [PATCH] Add video/audio delete button --- handlers.go | 60 ++++++++++++++++++++++++++++++++++++++++++ main.go | 2 ++ static/style/video.css | 19 +++++++++++++ templates/video.html | 14 ++++++++++ 4 files changed, 95 insertions(+) diff --git a/handlers.go b/handlers.go index 0eb5b1b..48bb7fa 100644 --- a/handlers.go +++ b/handlers.go @@ -661,6 +661,7 @@ func videosHandler(c echo.Context) error { } type VideoTemplate struct { + ID uint // Video.ID Source string Width uint Height uint @@ -673,6 +674,8 @@ type VideoTemplate struct { } type AudioTemplate struct { + ID uint // Audio.ID + Source string Kbps string Size string Filename string @@ -747,6 +750,7 @@ func videoHandler(c echo.Context) error { rate := float64(video.Size) / video.Length videoURLs = append(videoURLs, VideoTemplate{ + ID: video.ID, Source: video.Source, Width: video.Width, Height: video.Height, @@ -768,6 +772,8 @@ func videoHandler(c echo.Context) error { rate := float64(audio.Size) / audio.Length audioURLs = append(audioURLs, AudioTemplate{ + ID: audio.ID, + Source: audio.Source, Kbps: fmt.Sprintf("%.1f kbps", kbps), Size: humanSize(audio.Size), Filename: audio.Filename, @@ -867,6 +873,60 @@ func deleteOriginalHandler(c echo.Context) error { return c.Redirect(http.StatusSeeOther, "/videos") } +func deleteVideoHandler(c echo.Context) error { + id, _ := strconv.Atoi(c.Param("id")) + referrer := c.Request().Referer() + if referrer == "" { + referrer = "/" + } + + var video Video + result := db.First(&video, id) + if result.Error != nil { + log.Errorln("error retrieving video", id, result.Error) + return c.Redirect(http.StatusSeeOther, referrer) + } + + videoPath := filepath.Join(getDataDir(), video.Filename) + log.Debugln("remove", videoPath) + err := os.Remove(videoPath) + if err != nil { + log.Errorln("coudn't remove", videoPath, err) + } + + if err := db.Delete(&Video{}, id).Error; err != nil { + log.Errorln("error deleting video record", id, err) + } + return c.Redirect(http.StatusSeeOther, referrer) +} + +func deleteAudioHandler(c echo.Context) error { + id, _ := strconv.Atoi(c.Param("id")) + referrer := c.Request().Referer() + if referrer == "" { + referrer = "/" + } + + var audio Audio + result := db.First(&audio, id) + if result.Error != nil { + log.Errorln("error retrieving audio", id, result.Error) + return c.Redirect(http.StatusSeeOther, referrer) + } + + filePath := filepath.Join(getDataDir(), audio.Filename) + log.Debugln("remove", filePath) + err := os.Remove(filePath) + if err != nil { + log.Errorln("coudn't remove", filePath, err) + } + + if err := db.Delete(&Audio{}, id).Error; err != nil { + log.Errorln("error deleting audio record", id, err) + } + return c.Redirect(http.StatusSeeOther, referrer) +} + func tempHandler(c echo.Context) error { token := c.Param("token") diff --git a/main.go b/main.go index 24b61ff..ea0ca69 100644 --- a/main.go +++ b/main.go @@ -123,6 +123,8 @@ func main() { e.POST("/video/:id/delete", deleteOriginalHandler, authMiddleware) e.GET("/temp/:token", tempHandler) e.POST("/video/:id/process", processHandler, authMiddleware) + e.POST("/delete_video/:id", deleteVideoHandler, authMiddleware) + e.POST("/delete_audio/:id", deleteAudioHandler, authMiddleware) dataGroup := e.Group("/data") dataGroup.Use(authMiddleware) diff --git a/static/style/video.css b/static/style/video.css index 4fa4c8f..f045956 100644 --- a/static/style/video.css +++ b/static/style/video.css @@ -1,3 +1,8 @@ +button { + all: unset; + cursor: pointer; +} + .media-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); @@ -46,6 +51,20 @@ border-radius: 4px; } +.media-controls { + text-align: center; + padding-bottom: 15px; +} + +.media-controls button { + display: inline-block; + padding: 10px 15px; + background-color: red; + color: white; + text-decoration: none; + border-radius: 4px; +} + /* narrower than 600px */ @media (max-width: 600px) { .media-grid { diff --git a/templates/video.html b/templates/video.html index 77f5c1d..d266e68 100644 --- a/templates/video.html +++ b/templates/video.html @@ -27,6 +27,13 @@
Download ({{.Size}}, {{.StreamRate}})
+ {{if ne .Source "original"}} +
+
+ +
+
+ {{end}} {{end}} @@ -43,6 +50,13 @@
Download ({{.Size}}, {{.StreamRate}})
+ {{if ne .Source "original"}} +
+
+ +
+
+ {{end}} {{end}}