Refactor
This commit is contained in:
1 parent
bceaa3b6df
commit
937216bda6
11 files changed
+350
-213
No files matched your search
+179
@@ -0,0 +1,179 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"git.bbr-dev.info/brajkovic/resource_manager/domain/resize"
|
||||
"git.bbr-dev.info/brajkovic/resource_manager/domain/resource"
|
||||
"github.com/gin-gonic/gin"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type UploadRequest struct {
|
||||
Content string `json:"content"`
|
||||
Path string `json:"path"`
|
||||
Properties struct {
|
||||
Overwrite bool `json:"overwrite"`
|
||||
MimeType string `json:"mimeType"`
|
||||
} `json:"properties"`
|
||||
Resize *struct {
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
Type string `json:"type"`
|
||||
} `json:"resize"`
|
||||
}
|
||||
|
||||
func NoMethod() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
c.JSON(http.StatusNotFound, gin.H{
|
||||
"status": 404,
|
||||
"created": time.Now(),
|
||||
"message": "no handler for method '" + c.Request.Method + "'",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func NoRoute() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
c.JSON(http.StatusNotFound, gin.H{
|
||||
"status": 404,
|
||||
"created": time.Now(),
|
||||
"message": "no handler for " + c.Request.Method + " '" + c.Request.URL.RequestURI() + "'",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func HandleUpload(resourceManager resource.Manager) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
var request UploadRequest
|
||||
if err := c.BindJSON(&request); err != nil {
|
||||
c.AbortWithStatusJSON(400, gin.H{"error": "bad request"})
|
||||
return
|
||||
}
|
||||
content, err := base64.StdEncoding.DecodeString(request.Content)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(400, gin.H{"error": "bad request"})
|
||||
return
|
||||
}
|
||||
if resize.IsResizable(readMimeType(request.Path, request.Properties.MimeType)) && request.Resize != nil {
|
||||
content, err = resize.ResizeImage(content, resize.Resize{
|
||||
Height: request.Resize.Height,
|
||||
Width: request.Resize.Width,
|
||||
Type: mapResizeType(request.Resize.Type),
|
||||
})
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(400, gin.H{"error": "bad request"})
|
||||
return
|
||||
}
|
||||
}
|
||||
resourceManager.Upload(resource.UploadRequest{
|
||||
Buffer: bytes.NewBuffer(content),
|
||||
Path: request.Path,
|
||||
MimeType: request.Properties.MimeType,
|
||||
Overwrite: request.Properties.Overwrite,
|
||||
})
|
||||
// we return this as success
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
}
|
||||
|
||||
func HandleCopy(resourceManager resource.Manager) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
from := c.Query("from")
|
||||
to := c.Query("to")
|
||||
overwrite := c.Query("overwrite") == "true"
|
||||
if err := resourceManager.Copy(from, to, overwrite); err != nil {
|
||||
log.Println(err)
|
||||
c.AbortWithStatus(500)
|
||||
} else {
|
||||
c.Status(201)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func HandlePresign(resourceManager resource.Manager) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
path := c.Query("path")
|
||||
url, err := resourceManager.Presign(c, path)
|
||||
if err != nil {
|
||||
c.AbortWithStatus(404)
|
||||
return
|
||||
}
|
||||
if c.Query("redirect") == "true" {
|
||||
c.Redirect(http.StatusTemporaryRedirect, url)
|
||||
} else {
|
||||
c.JSON(200, gin.H{"url": url})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func HandleDownload(resourceManager resource.Manager) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
path := c.Query("path")
|
||||
data, err := resourceManager.Download(c, path)
|
||||
if err == nil {
|
||||
c.Header("content-disposition", "inline; filename=\""+filename(path)+"\"")
|
||||
c.Data(200, readMimeType(path, ""), data)
|
||||
} else {
|
||||
c.AbortWithStatus(http.StatusNotFound)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func HandleDelete(resourceManager resource.Manager) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
path := c.Query("path")
|
||||
if err := resourceManager.Delete(path); err != nil {
|
||||
c.AbortWithError(400, err)
|
||||
} else {
|
||||
c.Status(204)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var mimeTypes = map[string]string{
|
||||
"jpg": "image/jpg",
|
||||
"jpeg": "image/jpeg",
|
||||
"png": "image/png",
|
||||
"gif": "image/gif",
|
||||
"json": "application/json",
|
||||
"pdf": "application/pdf",
|
||||
"html": "text/html",
|
||||
"xml": "application/xml",
|
||||
}
|
||||
|
||||
func readMimeType(path string, mimeType string) string {
|
||||
if mimeType != "" {
|
||||
return mimeType
|
||||
}
|
||||
parts := strings.Split(path, ".")
|
||||
fileType := strings.ToLower(parts[len(parts)-1])
|
||||
if value, exists := mimeTypes[fileType]; exists {
|
||||
return value
|
||||
}
|
||||
return "application/octet-stream"
|
||||
}
|
||||
|
||||
func mapResizeType(resizeType string) resize.ResizeType {
|
||||
switch resizeType {
|
||||
case "cover":
|
||||
return resize.Cover
|
||||
case "contain":
|
||||
return resize.Contain
|
||||
case "exact_height":
|
||||
return resize.ExactHeight
|
||||
case "exact_width":
|
||||
return resize.ExactWidth
|
||||
default:
|
||||
return resize.Exact
|
||||
}
|
||||
}
|
||||
|
||||
func filename(path string) string {
|
||||
substrings := strings.Split(path, "/")
|
||||
return substrings[len(substrings)-1]
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"git.bbr-dev.info/brajkovic/resource_manager/domain/cache"
|
||||
"git.bbr-dev.info/brajkovic/resource_manager/domain/resource"
|
||||
"github.com/gin-gonic/gin"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func SetupServer() *gin.Engine {
|
||||
server := createServer()
|
||||
RegisterRoutes(server)
|
||||
return server
|
||||
}
|
||||
|
||||
func createServer() *gin.Engine {
|
||||
server := gin.New()
|
||||
server.NoRoute(NoRoute())
|
||||
server.NoMethod(NoMethod())
|
||||
server.Use(gin.Recovery())
|
||||
return server
|
||||
}
|
||||
|
||||
func RegisterRoutes(server *gin.Engine) {
|
||||
cacheManager := cache.NewManager()
|
||||
expiration := loadExpiration()
|
||||
|
||||
log.Println("Presign | expiration set to " + expiration.String())
|
||||
|
||||
resourceManager := resource.NewManager(cacheManager, expiration)
|
||||
|
||||
server.POST("/api/v1/upload", HandleUpload(resourceManager))
|
||||
server.GET("/api/v1/download", HandleDownload(resourceManager))
|
||||
server.GET("/api/v1/presign", HandlePresign(resourceManager))
|
||||
server.PUT("/api/v1/copy", HandleCopy(resourceManager))
|
||||
server.DELETE("/api/v1/delete", HandleDelete(resourceManager))
|
||||
}
|
||||
|
||||
func loadExpiration() time.Duration {
|
||||
if value := os.Getenv("PRESIGN_DURATION"); value != "" {
|
||||
duration, err := time.ParseDuration(value)
|
||||
if err != nil {
|
||||
return duration
|
||||
}
|
||||
}
|
||||
// default duration
|
||||
return 1 * time.Hour
|
||||
}
|
||||
Reference in new issue
Block a user