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