go-judge/cmd/executorserver/file_handler.go
criyle 7951f37bd4 Implements the draft executor server
- Add new command executorserver as the draft executor server
- Move shared pool logic into /pkg/pool
2020-03-04 02:12:26 -05:00

76 lines
1.3 KiB
Go

package main
import (
"io/ioutil"
"mime"
"net/http"
"path"
"github.com/gin-gonic/gin"
)
func fileGet(c *gin.Context) {
ids := getAllFileID()
c.JSON(http.StatusOK, ids)
}
func filePost(c *gin.Context) {
fh, err := c.FormFile("file")
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
f, err := fh.Open()
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
b, err := ioutil.ReadAll(f)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
id, err := addFile(b, fh.Filename)
c.JSON(http.StatusOK, id)
}
func fileIDGet(c *gin.Context) {
type fileURI struct {
FileID string `uri:"fid"`
}
var uri fileURI
if err := c.ShouldBindUri(&uri); err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
f, ok := getFile(uri.FileID)
if !ok {
c.AbortWithStatus(http.StatusNotFound)
return
}
typ := mime.TypeByExtension(path.Ext(f.FileName))
c.Data(http.StatusOK, typ, f.Content)
}
func fileIDDelete(c *gin.Context) {
type fileURI struct {
FileID string `uri:"fid"`
}
var uri fileURI
if err := c.ShouldBindUri(&uri); err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
ok := removeFile(uri.FileID)
if !ok {
c.AbortWithStatus(http.StatusNotFound)
return
}
c.Status(http.StatusOK)
}