Initial commit

This commit is contained in:
Carl Pearson
2025-05-30 05:35:27 -06:00
commit fa7c416f66
16 changed files with 1080 additions and 0 deletions

19
handlers/image.go Normal file
View File

@@ -0,0 +1,19 @@
package handlers
import (
"net/http"
"git.sr.ht/~cwpearson/replicate-jump-server/store"
"github.com/labstack/echo/v4"
)
func ImageGet(c echo.Context) error {
id := c.Param("id")
image, exists := store.Get(id)
if !exists {
return echo.NewHTTPError(http.StatusNotFound, "Image not found or expired")
}
return c.Blob(http.StatusOK, image.ContentType, image.Data)
}

23
handlers/middleware.go Normal file
View File

@@ -0,0 +1,23 @@
package handlers
import (
"net/http"
"github.com/labstack/echo/v4"
"git.sr.ht/~cwpearson/replicate-jump-server/config"
)
// AuthMiddleware validates the bearer token
func AuthMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
auth := c.Request().Header.Get("Authorization")
expectedAuth := "Bearer " + config.Bearer()
if auth != expectedAuth {
return echo.NewHTTPError(http.StatusUnauthorized, "Invalid or missing bearer token")
}
return next(c)
}
}

66
handlers/upload.go Normal file
View File

@@ -0,0 +1,66 @@
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,
})
}