Temporary public URLs for videos

This commit is contained in:
Carl Pearson
2024-09-08 05:42:49 -06:00
parent 689ce8280e
commit dc6b391e5a
5 changed files with 83 additions and 4 deletions

View File

@@ -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()
}
}