精简后的项目:仅保留 api/ui 源码、文档、构建依赖
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
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(), "Unauthorized access to photo", "reason", 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
|
||||
w.Header().Set("Cache-Control", "private, max-age=31536000, immutable")
|
||||
if mediaURL.ContentType != "" {
|
||||
w.Header().Set("Content-Type", mediaURL.ContentType)
|
||||
}
|
||||
|
||||
http.ServeFile(w, r, cachedPath)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user