mirror of
https://git.vectorsigma.ru/public/photoview.git
synced 2026-08-03 19:39:19 +00:00
* Refactored code; temp disable workflows on push * remove printf leftovers * A better sanitizing code * extend auth routes tests * more tests, fix 2 bugs in token processing * more refactoring and fixes * optimize video mediaURLs DB query and make testCachePath in utils thread-safe * fix always passing test * and more refactoring; 1 more test, but cleanup still has to be fixed * more debug code in the test * fix the test * Final commit * Adding a defer function just in case of panic * Address some of review comments * Protect the shared var by mutex; implement correct context-aware scanning * Better error; better initial func wrap/capture; better log messages in `photos.go`; * Addressing a few more review comments * Order the query results; a better error type catch; setting the MP4 content type header explicitly * try to fix the cancelation detection condition * Extract a function and call it by name --------- Co-authored-by: Konstantin Koval
79 lines
2.2 KiB
Go
79 lines
2.2 KiB
Go
package routes
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
|
|
"github.com/gorilla/mux"
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/photoview/photoview/api/graphql/models"
|
|
"github.com/photoview/photoview/api/log"
|
|
"github.com/photoview/photoview/api/scanner"
|
|
)
|
|
|
|
func RegisterPhotoRoutes(db *gorm.DB, router *mux.Router) {
|
|
|
|
router.HandleFunc("/{name}", func(w http.ResponseWriter, r *http.Request) {
|
|
mediaName := mux.Vars(r)["name"]
|
|
|
|
var mediaURL models.MediaURL
|
|
result := db.Model(&models.MediaURL{}).Joins("Media").Select("media_urls.*").Where("media_urls.media_name = ?", mediaName).Scan(&mediaURL)
|
|
if err := result.Error; err != nil {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
w.Write([]byte("404"))
|
|
return
|
|
}
|
|
|
|
media := mediaURL.Media
|
|
if media == nil {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
w.Write([]byte("404 - Media not found"))
|
|
return
|
|
}
|
|
|
|
if success, response, status, err := authenticateMedia(media, db, r); !success {
|
|
if err != nil {
|
|
log.Warn(r.Context(), "error authenticating photo", "error", err)
|
|
}
|
|
w.WriteHeader(status)
|
|
w.Write([]byte(response))
|
|
return
|
|
}
|
|
|
|
cachedPath, err := mediaURL.CachedPath()
|
|
if err != nil {
|
|
log.Error(r.Context(), "error getting cached path for media URL", "error", err)
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
w.Write([]byte(internalServerError))
|
|
return
|
|
}
|
|
|
|
if _, err := os.Stat(cachedPath); os.IsNotExist((err)) {
|
|
// err := db.Transaction(func(tx *gorm.DB) error {
|
|
if err = scanner.ProcessSingleMediaFunc(r.Context(), db, media); err != nil {
|
|
log.Error(r.Context(), "processing image not found in cache",
|
|
"media_cache_path", cachedPath,
|
|
"error", err)
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
w.Write([]byte(internalServerError))
|
|
return
|
|
}
|
|
|
|
if _, err = os.Stat(cachedPath); err != nil {
|
|
log.Error(r.Context(), "after reprocessing image not found in cache",
|
|
"media_cache_path", cachedPath,
|
|
"error", err)
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
w.Write([]byte(internalServerError))
|
|
return
|
|
}
|
|
}
|
|
|
|
// Allow caching the resource for 1 day
|
|
w.Header().Set("Cache-Control", "private, max-age=86400, immutable")
|
|
|
|
http.ServeFile(w, r, cachedPath)
|
|
})
|
|
}
|