Upload hooks

This commit is contained in:
simon987
2019-03-23 14:28:51 -04:00
parent 532a7fe726
commit 2b41ce4edb
7 changed files with 100 additions and 13 deletions

View File

@@ -7,7 +7,9 @@ import (
"github.com/valyala/fasthttp"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
)
@@ -133,12 +135,32 @@ func (api *WebApi) Upload(ctx *fasthttp.RequestCtx) {
logrus.WithError(err).Error("Error while closing file")
}
mu.(*sync.RWMutex).Unlock()
mu.(*sync.RWMutex).RLock()
executeUploadHook(slot)
mu.(*sync.RWMutex).RUnlock()
})
if err != nil {
logrus.WithError(err).Error("Error while upgrading connexion")
}
}
func executeUploadHook(slot UploadSlot) {
path := filepath.Join(WorkDir, slot.FileName)
commandStr := strings.Replace(slot.UploadHook, "$1", "\""+path+"\"", -1)
cmd := exec.Command("bash", "-c", commandStr)
output, err := cmd.CombinedOutput()
logrus.WithFields(logrus.Fields{
"output": string(output),
"err": err,
"commandStr": commandStr,
}).Info("Execute upload hook")
}
func (api *WebApi) ReadUploadSlot(ctx *fasthttp.RequestCtx) {
tokenStr := string(ctx.Request.Header.Peek("X-Upload-Token"))
@@ -191,9 +213,11 @@ func (api *WebApi) ReadUploadSlot(ctx *fasthttp.RequestCtx) {
func (api *WebApi) allocateUploadSlot(req *AllocateUploadSlotRequest) error {
slot := &UploadSlot{
MaxSize: req.MaxSize,
FileName: req.FileName,
Token: req.Token,
MaxSize: req.MaxSize,
FileName: req.FileName,
Token: req.Token,
ToDisposeDate: req.ToDisposeDate,
UploadHook: req.UploadHook,
}
logrus.WithFields(logrus.Fields{