Add /video/id/set_timestamp handler, timestamp in video HTML

This commit is contained in:
Carl Pearson
2025-05-01 05:33:22 -06:00
parent 14adf10c68
commit 010c6bd191
5 changed files with 70 additions and 8 deletions

View File

@@ -75,3 +75,4 @@ Build and push this container to ghcr
- [x] header on playlist page
- [x] Choose database directory
- [x] add extension to download
- [ ] server-side watch progress

58
handlers/set_timestamp.go Normal file
View File

@@ -0,0 +1,58 @@
package handlers
import (
"net/http"
"strconv"
"github.com/labstack/echo/v4"
"gorm.io/gorm"
"ytdlp-site/database"
"ytdlp-site/originals"
)
func SetTimestamp(c echo.Context) error {
// Parse the ID from the URL parameter
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid ID format"})
}
// Define a struct to receive the timestamp from JSON
type TimestampRequest struct {
Timestamp string `json:"timestamp"`
}
// Parse the request body
var req TimestampRequest
if err := c.Bind(&req); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid request body"})
}
// Validate that timestamp was provided
if req.Timestamp == "" {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Timestamp is required"})
}
// Get database connection
db := database.Get()
// Update the timestamp field with the value from the request
result := db.Model(&originals.Original{}).
Where("id = ?", id).
Update("timestamp", req.Timestamp)
// Handle database errors
if result.Error != nil {
log.Errorln(result.Error)
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Database error"})
}
// Check if record was found
if result.RowsAffected == 0 {
log.Errorln(gorm.ErrRecordNotFound)
return c.JSON(http.StatusNotFound, map[string]string{"error": "Record not found"})
}
return c.NoContent(http.StatusOK)
}

View File

@@ -144,6 +144,7 @@ func main() {
e.GET("/temp/:token", tempHandler)
e.POST("/video/:id/process", processHandler, handlers.AuthMiddleware)
e.POST("/video/:id/toggle_watched", handlers.ToggleWatched, handlers.AuthMiddleware)
e.POST("/video/:id/set_timestamp", handlers.SetTimestamp, handlers.AuthMiddleware)
e.POST("/delete_video/:id", deleteVideoHandler, handlers.AuthMiddleware)
e.POST("/delete_audio/:id", deleteAudioHandler, handlers.AuthMiddleware)
e.POST("/transcode_to_video/:id", transcodeToVideoHandler, handlers.AuthMiddleware)

View File

@@ -23,14 +23,15 @@ const (
type Original struct {
gorm.Model
UserID uint
URL string
Title string
Artist string
Status Status
Audio bool // video download requested
Video bool // audio download requested
Watched bool
UserID uint
URL string
Title string
Artist string
Status Status
Audio bool // video download requested
Video bool // audio download requested
Watched bool
Timestamp float64
Playlist bool // part of a playlist
PlaylistID uint // Playlist.ID (if part of a playlist)

View File

@@ -106,6 +106,7 @@
</div>
<input type="hidden" id="video-id" value="{{.original.ID}}">
<input type="hidden" id="video-timestamp" value="{{.original.Timestamp}}">
<script src="/static/script/save-media-progress.js"></script>
{{template "footer" .}}