2019-06-13 20:26:01 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2019-12-13 16:25:47 +01:00
|
|
|
"path"
|
2019-06-13 20:26:01 +02:00
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2020-01-30 01:53:18 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2020-04-05 22:26:53 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
2020-01-12 14:00:56 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2019-06-13 20:26:01 +02:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
2019-06-14 21:16:59 +02:00
|
|
|
// POST /api/v1/upload/:path
|
2019-06-13 20:26:01 +02:00
|
|
|
func Upload(router *gin.RouterGroup, conf *config.Config) {
|
2019-06-14 21:16:59 +02:00
|
|
|
router.POST("/upload/:path", func(c *gin.Context) {
|
2020-04-13 18:08:21 +02:00
|
|
|
if conf.ReadOnly() || !conf.Settings().Features.Upload {
|
2019-07-02 22:03:23 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusForbidden, ErrReadOnly)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-11 21:10:41 +01:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-13 20:26:01 +02:00
|
|
|
start := time.Now()
|
2019-06-14 21:16:59 +02:00
|
|
|
subPath := c.Param("path")
|
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 {
|
2020-01-07 17:36:49 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": txt.UcFirst(err.Error())})
|
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)
|
|
|
|
var uploads []string
|
2019-06-13 20:26:01 +02:00
|
|
|
|
2019-12-13 16:25:47 +01:00
|
|
|
p := path.Join(conf.ImportPath(), "upload", subPath)
|
2019-06-13 20:26:01 +02:00
|
|
|
|
2019-12-13 16:25:47 +01:00
|
|
|
if err := os.MkdirAll(p, os.ModePerm); err != nil {
|
2020-01-07 17:36:49 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": txt.UcFirst(err.Error())})
|
2019-06-13 20:26:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
2019-12-13 16:25:47 +01:00
|
|
|
filename := path.Join(p, filepath.Base(file.Filename))
|
2019-06-13 20:26:01 +02:00
|
|
|
|
2019-12-15 17:19:16 +01:00
|
|
|
log.Debugf("upload: saving file \"%s\"", file.Filename)
|
|
|
|
|
2019-06-13 20:26:01 +02:00
|
|
|
if err := c.SaveUploadedFile(file, filename); err != nil {
|
2020-01-07 17:36:49 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": txt.UcFirst(err.Error())})
|
2019-06-13 20:26:01 +02:00
|
|
|
return
|
|
|
|
}
|
2019-12-15 17:19:16 +01:00
|
|
|
|
|
|
|
uploads = append(uploads, filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !conf.UploadNSFW() {
|
2020-04-05 22:26:53 +02:00
|
|
|
nd := service.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
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("nsfw: \"%s\" might be offensive", filename)
|
|
|
|
|
|
|
|
containsNSFW = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if containsNSFW {
|
|
|
|
for _, filename := range uploads {
|
|
|
|
if err := os.Remove(filename); err != nil {
|
|
|
|
log.Errorf("nsfw: could not delete \"%s\"", filename)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.AbortWithStatusJSON(http.StatusForbidden, ErrUploadNSFW)
|
|
|
|
return
|
|
|
|
}
|
2019-06-13 20:26:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
|
2019-12-15 17:19:16 +01:00
|
|
|
log.Infof("%d files uploaded in %s", uploaded, elapsed)
|
2019-06-13 20:26:01 +02:00
|
|
|
|
2019-12-15 17:19:16 +01:00
|
|
|
c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("%d files uploaded in %s", uploaded, elapsed)})
|
2019-06-13 20:26:01 +02:00
|
|
|
})
|
|
|
|
}
|