photoprism/internal/api/upload.go

119 lines
2.6 KiB
Go
Raw Normal View History

2019-06-13 20:26:01 +02:00
package api
import (
"net/http"
"os"
"path"
2019-06-13 20:26:01 +02:00
"path/filepath"
"time"
"github.com/gin-gonic/gin"
"github.com/photoprism/photoprism/internal/acl"
"github.com/photoprism/photoprism/internal/event"
"github.com/photoprism/photoprism/internal/get"
"github.com/photoprism/photoprism/internal/i18n"
"github.com/photoprism/photoprism/pkg/clean"
2019-06-13 20:26:01 +02:00
)
// Upload adds files to the import folder, from where supported file types are moved to the originals folders.
//
// POST /api/v1/upload/:path
func Upload(router *gin.RouterGroup) {
router.POST("/upload/:token", func(c *gin.Context) {
conf := get.Config()
if conf.ReadOnly() || !conf.Settings().Features.Upload {
Abort(c, http.StatusForbidden, i18n.ErrReadOnly)
return
}
s := AuthAny(c, acl.ResourceFiles, acl.Permissions{acl.ActionManage, acl.ActionUpload})
if s.Abort(c) {
return
}
2019-06-13 20:26:01 +02:00
start := time.Now()
token := clean.Token(c.Param("token"))
2019-06-13 20:26:01 +02:00
f, err := c.MultipartForm()
2019-06-13 20:26:01 +02:00
if err != nil {
log.Errorf("upload: %s", err)
AbortBadRequest(c)
2019-06-13 20:26:01 +02:00
return
}
event.Publish("upload.start", event.Data{"time": start})
files := f.File["files"]
uploaded := len(files)
var uploads []string
2019-06-13 20:26:01 +02:00
uploadDir := path.Join(conf.ImportPath(), UploadPath, s.RefID+token)
2019-06-13 20:26:01 +02:00
if err = os.MkdirAll(uploadDir, os.ModePerm); err != nil {
log.Errorf("upload: failed creating folder %s", clean.Log(filepath.Base(uploadDir)))
AbortBadRequest(c)
2019-06-13 20:26:01 +02:00
return
}
for _, file := range files {
filename := path.Join(uploadDir, filepath.Base(file.Filename))
2019-06-13 20:26:01 +02:00
log.Debugf("upload: saving file %s", clean.Log(file.Filename))
2019-06-13 20:26:01 +02:00
if err := c.SaveUploadedFile(file, filename); err != nil {
log.Errorf("upload: failed saving file %s", clean.Log(filepath.Base(file.Filename)))
AbortBadRequest(c)
2019-06-13 20:26:01 +02:00
return
}
uploads = append(uploads, filename)
}
if !conf.UploadNSFW() {
nd := get.NsfwDetector()
containsNSFW := false
for _, filename := range uploads {
labels, err := nd.File(filename)
if err != nil {
log.Debug(err)
continue
}
if labels.IsSafe() {
continue
}
log.Infof("nsfw: %s might be offensive", clean.Log(filename))
containsNSFW = true
}
if containsNSFW {
for _, filename := range uploads {
if err := os.Remove(filename); err != nil {
log.Errorf("nsfw: could not delete %s", clean.Log(filename))
}
}
Abort(c, http.StatusForbidden, i18n.ErrOffensiveUpload)
return
}
2019-06-13 20:26:01 +02:00
}
elapsed := int(time.Since(start).Seconds())
2019-06-13 20:26:01 +02:00
msg := i18n.Msg(i18n.MsgFilesUploadedIn, uploaded, elapsed)
2019-06-13 20:26:01 +02:00
log.Info(msg)
c.JSON(http.StatusOK, i18n.Response{Code: http.StatusOK, Msg: msg})
2019-06-13 20:26:01 +02:00
})
}