-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.go
143 lines (117 loc) · 3.55 KB
/
upload.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package handler
import (
"encoding/json"
"fmt"
"github.com/gohugonet/hugoverse/internal/domain/admin"
"github.com/gohugonet/hugoverse/pkg/timestamp"
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"time"
)
// StoreFiles stores file uploads at paths like /YYYY/MM/filename.ext
func (s *Handler) StoreFiles(req *http.Request) (map[string]string, error) {
err := req.ParseMultipartForm(1024 * 1024 * 4) // maxMemory 4MB
if err != nil {
return nil, err
}
ts := req.FormValue("timestamp") // timestamp in milliseconds since unix epoch
if ts == "" {
ts = timestamp.Now()
}
// To use for FormValue name:urlPath
urlPaths := make(map[string]string)
if len(req.MultipartForm.File) == 0 {
return urlPaths, nil
}
req.Form.Set("timestamp", ts)
tm, err := timestamp.ConvertToTime(ts)
if err != nil {
return nil, err
}
urlPathPrefix := "api"
uploadDirName := "uploads"
uploadDir := filepath.Join(s.uploadDir, fmt.Sprintf("%d", tm.Year()), fmt.Sprintf("%02d", tm.Month()))
err = os.MkdirAll(uploadDir, os.ModeDir|os.ModePerm)
if err != nil {
return nil, err
}
// loop over all files and save them to disk
for name, fds := range req.MultipartForm.File {
filename, err := s.contentApp.NormalizeString(fds[0].Filename)
if err != nil {
return nil, err
}
src, err := fds[0].Open()
if err != nil {
err := fmt.Errorf("Couldn't open uploaded file: %s", err)
return nil, err
}
defer src.Close()
// check if file at path exists, if so, add timestamp to file
absPath := filepath.Join(uploadDir, filename)
if _, err := os.Stat(absPath); !os.IsNotExist(err) {
filename = fmt.Sprintf("%d-%s", time.Now().Unix(), filename)
absPath = filepath.Join(uploadDir, filename)
}
// save to disk
// (TODO: or check if S3 credentials exist, & save to cloud)
dst, err := os.Create(absPath)
if err != nil {
err := fmt.Errorf("Failed to create destination file for upload: %s", err)
return nil, err
}
// copy file from src to dst on disk
var size int64
if size, err = io.Copy(dst, src); err != nil {
err := fmt.Errorf("Failed to copy uploaded file to destination: %s", err)
return nil, err
}
// add name:urlPath to req.PostForm to be inserted into db
urlPath := fmt.Sprintf("/%s/%s/%d/%02d/%s", urlPathPrefix, uploadDirName, tm.Year(), tm.Month(), filename)
urlPaths[name] = urlPath
// add upload information to db
go s.storeFileInfo(size, filename, urlPath, fds)
}
return urlPaths, nil
}
func (s *Handler) storeFileInfo(size int64, filename, urlPath string, fds []*multipart.FileHeader) {
data := url.Values{
"name": []string{filename},
"path": []string{urlPath},
"content_type": []string{fds[0].Header.Get("Content-Type")},
"content_length": []string{fmt.Sprintf("%d", size)},
}
if err := s.adminApp.NewUpload(data); err != nil {
s.log.Errorf("Error saving file upload record to database: %v", err)
}
}
func (s *Handler) deleteUploadFromDisk(id string) error {
// get data on file
data, err := s.adminApp.GetUpload(id)
if err != nil {
return err
}
// unmarshal data
upload := s.adminApp.UploadCreator()()
if err = json.Unmarshal(data, &upload); err != nil {
return err
}
ut, ok := upload.(admin.Traceable)
if !ok {
return fmt.Errorf("invalid upload type")
}
// split and rebuild path in OS friendly way
// use path to delete the physical file from disk
pathSplit := strings.Split(strings.TrimPrefix(ut.FilePath(), "/api/"), "/")
pathJoin := filepath.Join(pathSplit...)
err = os.Remove(pathJoin)
if err != nil {
return err
}
return nil
}