42 lines
922 B
Go
42 lines
922 B
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"threadr/models"
|
|
)
|
|
|
|
func FileHandler(app *App) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
fileIDStr := r.URL.Query().Get("id")
|
|
fileID, err := strconv.ParseInt(fileIDStr, 10, 64)
|
|
if err != nil {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
|
|
file, err := models.GetFileByID(app.DB, fileID)
|
|
if err != nil || file == nil {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
|
|
isProfileImage, err := models.IsProfileImageFile(app.DB, fileID)
|
|
if err != nil || !isProfileImage {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
|
|
filePath, contentType, ok := models.ResolveStoredImagePath(app.Config.FileStorageDir, file)
|
|
if !ok {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", contentType)
|
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
|
w.Header().Set("Cache-Control", "private, max-age=300")
|
|
http.ServeFile(w, r, filePath)
|
|
}
|
|
}
|