package handlers import ( "bytes" "io" "net/http" "github.com/labstack/echo/v4" "git.sr.ht/~cwpearson/replicate-jump-server/store" ) // isImageContentType checks if the content type is a valid image func isImageContentType(contentType string) bool { validTypes := []string{ "image/jpeg", "image/jpg", "image/png", "image/gif", "image/webp", "image/bmp", } for _, validType := range validTypes { if contentType == validType { return true } } return false } func UploadPost(c echo.Context) error { // Get the uploaded file file, err := c.FormFile("image") if err != nil { return echo.NewHTTPError(http.StatusBadRequest, "No image provided") } // Open the file src, err := file.Open() if err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to open image") } defer src.Close() // Read file data var buf bytes.Buffer if _, err := io.Copy(&buf, src); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to read image") } // Detect content type contentType := http.DetectContentType(buf.Bytes()) // Validate it's an image if !isImageContentType(contentType) { return echo.NewHTTPError(http.StatusBadRequest, "File is not a valid image") } // Save image and get ID id := store.Save(buf.Bytes(), contentType) return c.JSON(http.StatusOK, map[string]string{ "id": id, }) }