photoprism/internal/api/upload.go
2019-06-14 15:29:12 -07:00

54 lines
1.3 KiB
Go

package api
import (
"fmt"
"net/http"
"os"
"path/filepath"
"time"
"github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/util"
log "github.com/sirupsen/logrus"
"github.com/gin-gonic/gin"
)
// POST /api/v1/upload/:path
func Upload(router *gin.RouterGroup, conf *config.Config) {
router.POST("/upload/:path", func(c *gin.Context) {
start := time.Now()
subPath := c.Param("path")
form, err := c.MultipartForm()
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": util.UcFirst(err.Error())})
return
}
files := form.File["files"]
path := fmt.Sprintf("%s/upload/%s", conf.ImportPath(), subPath)
if err := os.MkdirAll(path, os.ModePerm); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": util.UcFirst(err.Error())})
return
}
for _, file := range files {
filename := fmt.Sprintf("%s/%s", path, filepath.Base(file.Filename))
if err := c.SaveUploadedFile(file, filename); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": util.UcFirst(err.Error())})
return
}
}
elapsed := time.Since(start)
log.Infof("%d files uploaded in %s", len(files), elapsed)
c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("%d files uploaded in %s", len(files), elapsed)})
})
}