This repository has been archived on 2023-11-13. You can view files and clone it, but cannot push or open issues or pull requests.
imagini/internal/api/media.go

56 lines
1.2 KiB
Go
Raw Normal View History

2021-01-22 05:00:55 +00:00
package api
import (
"net/http"
2021-02-04 10:16:13 +00:00
"os"
"path"
"reichard.io/imagini/graph/model"
2021-01-22 05:00:55 +00:00
)
2021-02-04 20:31:07 +00:00
/**
* Responsible for serving up static images / videos
**/
2021-01-22 05:00:55 +00:00
func (api *API) mediaHandler(w http.ResponseWriter, r *http.Request) {
2021-02-04 10:16:13 +00:00
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
if path.Dir(r.URL.Path) != "/media" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
// Acquire Width & Height Parameters
query := r.URL.Query()
width := query["width"]
height := query["height"]
_ = width
_ = height
// TODO: Caching & Resizing
// - If both, force resize with new scale
// - If one, scale resize proportionally
// Pull out userID
authContext := r.Context().Value("auth").(*model.AuthContext)
rawUserID, _ := (*authContext.AccessToken).Get("sub")
userID := rawUserID.(string)
// Derive Path
fileName := path.Base(r.URL.Path)
folderPath := path.Join("/" + api.Config.DataPath + "/media/" + userID)
mediaPath := path.Join(folderPath + "/" + fileName)
// Check if File Exists
_, err := os.Stat(mediaPath)
if os.IsNotExist(err) {
// TODO: Different HTTP Response Code?
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
http.ServeFile(w, r, mediaPath)
2021-01-22 05:00:55 +00:00
}