-
Notifications
You must be signed in to change notification settings - Fork 0
/
uploads.go
81 lines (74 loc) · 2.43 KB
/
uploads.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package controllers
import (
"fmt"
"net/http"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/denisbakhtin/projectmanager/config"
"github.com/denisbakhtin/projectmanager/helpers"
"github.com/denisbakhtin/projectmanager/models"
"github.com/gin-gonic/gin"
)
func uploadsPost(c *gin.Context) {
uploader := c.Param("uploader") // form or ckeditor
file, _ := c.FormFile("upload")
ext := filepath.Ext(file.Filename)
if !isExtensionAllowed(ext) || !isValidFilename(file.Filename) {
//TODO: extend the list? Or just prohibit executables
abortWithError(c, http.StatusBadRequest, fmt.Errorf("Unsupported file extension"))
return
}
user := c.MustGet("user").(models.User)
now := time.Now()
subDir := path.Join(fmt.Sprintf("%04d-%02d-%02d", now.Year(), now.Month(), now.Day()), fmt.Sprintf("user_%d", user.ID))
uploadsDir := path.Join(config.UploadPath, subDir)
filename, err := helpers.GetUniqueFilename(uploadsDir, file.Filename)
if err != nil {
abortWithError(c, http.StatusBadRequest, err)
return
}
if err := os.MkdirAll(uploadsDir, 0755); err != nil {
abortWithError(c, http.StatusBadRequest, err)
return
}
if err := c.SaveUploadedFile(file, path.Join(uploadsDir, filename)); err != nil {
abortWithError(c, http.StatusBadRequest, err)
return
}
fileurl := config.UploadPathURL + "/" + subDir + "/" + filename
if strings.Compare(uploader, "ckeditor") == 0 {
ckfunc := c.PostForm("CKEditorFuncNum")
c.String(http.StatusOK, "<script>window.parent.CKEDITOR.tools.callFunction("+ckfunc+", \""+fileurl+"\");</script>")
return
}
c.JSON(http.StatusOK, models.AttachedFile{
Name: filename,
FilePath: path.Join(uploadsDir, filename),
URL: fileurl,
})
}
//fileIsSafeToSave checks if file extension is acceptable
func isExtensionAllowed(fileExt string) bool {
validExtPattern := "(?i)^\\.doc[x]?|\\.jp[e]?g|\\.bmp|\\.zip|\\.rar|\\.gz|\\.gzip|\\.gif|\\.txt|\\.pdf|\\.png$" //(?i) == case insensitive
if matched, _ := regexp.MatchString(validExtPattern, fileExt); matched {
return true
}
return false
}
//isValidForRemoval checks if filename excludes certain dangerous symbols
func isValidFilename(fileName string) bool {
switch {
case fileName == "":
return false
case strings.Contains(fileName, "/") || strings.Contains(fileName, "\\"):
return false //no change dir plz
case strings.Contains(fileName, "*") || strings.Contains(fileName, "?"):
return false //no masks
default:
return true
}
}