Temporary public URLs for videos
This commit is contained in:
56
models.go
56
models.go
@@ -1,9 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -27,6 +30,12 @@ type User struct {
|
||||
Password string
|
||||
}
|
||||
|
||||
type TempURL struct {
|
||||
Token string `gorm:"uniqueIndex"`
|
||||
FilePath string
|
||||
ExpiresAt time.Time
|
||||
}
|
||||
|
||||
type DownloadStatus struct {
|
||||
ID uint
|
||||
Progress float64
|
||||
@@ -81,3 +90,50 @@ func (dm *DownloadManager) RemoveStatus(id uint) {
|
||||
defer dm.mutex.Unlock()
|
||||
delete(dm.downloads, id)
|
||||
}
|
||||
|
||||
func generateToken() string {
|
||||
uuidObj := uuid.Must(uuid.NewV7())
|
||||
return uuidObj.String()
|
||||
}
|
||||
|
||||
func CreateTempURL(filePath string) (TempURL, error) {
|
||||
|
||||
token := generateToken()
|
||||
expiration := time.Now().Add(24 * time.Hour)
|
||||
|
||||
tempURL := TempURL{
|
||||
Token: token,
|
||||
FilePath: filePath,
|
||||
ExpiresAt: expiration,
|
||||
}
|
||||
|
||||
if err := db.Create(&tempURL).Error; err != nil {
|
||||
return TempURL{}, errors.New("failed to create temporary URL")
|
||||
}
|
||||
|
||||
return tempURL, nil
|
||||
}
|
||||
|
||||
func cleanupExpiredURLs() {
|
||||
result := db.Where("expires_at < ?", time.Now()).Delete(&TempURL{})
|
||||
if result.Error != nil {
|
||||
fmt.Printf("Error cleaning up expired URLs: %v\n", result.Error)
|
||||
} else {
|
||||
fmt.Printf("Cleaned up %d expired temporary URLs\n", result.RowsAffected)
|
||||
}
|
||||
}
|
||||
|
||||
func vacuumDatabase() {
|
||||
if err := db.Exec("VACUUM").Error; err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func PeriodicCleanup() {
|
||||
ticker := time.NewTicker(12 * time.Hour)
|
||||
for range ticker.C {
|
||||
fmt.Println("PeriodicCleanup...")
|
||||
cleanupExpiredURLs()
|
||||
vacuumDatabase()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user