document.go
3.9 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package document
import (
"errors"
"fmt"
"io"
"mime"
"net/http"
"os"
"time"
web "git.to-net.rs/marko.tikvic/webutility"
)
// Document ...
type Document struct {
ID int64 `json:"id"`
FileName string `json:"fileName"`
Extension string `json:"extension"`
ContentType string `json:"contentType"`
Size int64 `json:"fileSize"`
UploadedBy string `json:"uploadedBy"`
LastModifiedBy string `json:"lastModifiedBy"`
TimeUploaded int64 `json:"timeUploaded"`
TimeLastModified int64 `json:"timeLastModified"`
RoleAccessLevel int64 `json:"accessLevel"`
Description string `json:"description"`
Download *DownloadLink `json:"download"`
Path string `json:"-"`
directory string
data []byte
}
// OpenFileAsDocument ...
func OpenFileAsDocument(path string) (*Document, error) {
d := &Document{Path: path}
f, err := os.Open(d.Path)
if err != nil {
return nil, err
}
defer f.Close()
stats, err := f.Stat()
if err != nil {
return nil, err
}
d.FileName = stats.Name()
d.Size = stats.Size()
d.Extension = web.FileExtension(d.FileName)
d.data = make([]byte, d.Size)
if _, err = f.Read(d.data); err != nil {
return nil, err
}
return d, err
}
// DownloadLink ...
type DownloadLink struct {
Method string `json:"method"`
URL string `json:"url"`
}
// SetDownloadInfo ...
func (d *Document) SetDownloadInfo(method, url string) {
d.Download = &DownloadLink{
Method: method,
URL: url,
}
}
// ServeDocument writes d's buffer to w and sets appropriate headers according to d's content type
// and downloadPrompt.
func ServeDocument(w http.ResponseWriter, d *Document, downloadPrompt bool) error {
f, err := os.Open(d.Path)
if err != nil {
return err
}
defer f.Close()
web.SetContentType(w, mime.TypeByExtension(d.Extension))
web.SetResponseStatus(w, http.StatusOK)
if downloadPrompt {
w.Header().Set("Content-Disposition", "attachment; filename="+d.FileName)
}
buf := make([]byte, d.Size)
if _, err := f.Read(buf); err != nil {
return err
}
w.Header().Set("Content-Length", fmt.Sprintf("%d", d.Size))
web.WriteResponse(w, buf)
return nil
}
// ParseDocument ...
func ParseDocument(req *http.Request) (doc *Document, err error) {
req.ParseMultipartForm(32 << 20)
file, fheader, err := req.FormFile("document")
if err != nil {
return doc, err
}
claims, _ := web.GetTokenClaims(req)
owner := claims.Username
fname := fheader.Filename
fsize := fheader.Size
ftype := fmt.Sprintf("%v", fheader.Header["Content-Type"][0])
fextn := web.FileExtension(fname)
if fextn == "" {
return doc, errors.New("invalid extension")
}
doc = new(Document)
doc.FileName = fname
doc.Size = fsize
doc.ContentType = ftype
doc.Extension = "." + fextn
t := time.Now().Unix()
doc.TimeUploaded = t
doc.TimeLastModified = t
doc.UploadedBy = owner
doc.LastModifiedBy = owner
doc.RoleAccessLevel = 0
doc.data = make([]byte, doc.Size)
if _, err = io.ReadFull(file, doc.data); err != nil {
return doc, err
}
return doc, nil
}
// SaveToFile ...
func (d *Document) SaveToFile(path string) (f *os.File, err error) {
d.Path = path
if web.FileExists(path) {
err = fmt.Errorf("file %s alredy exists", path)
return nil, err
}
if parentDir := web.DirectoryFromPath(path); parentDir != "" {
if err = os.MkdirAll(parentDir, os.ModePerm); err != nil {
if !os.IsExist(err) {
return nil, err
}
}
}
if f, err = os.Create(path); err != nil {
return nil, err
}
if _, err = f.Write(d.data); err != nil {
f.Close()
d.DeleteFile()
return nil, err
}
f.Close()
return f, nil
}
func DeleteDocuments(docs []*Document) error {
for _, d := range docs {
if err := d.DeleteFile(); err != nil {
return err
}
}
return nil
}
// DeleteFile ...
func (d *Document) DeleteFile() error {
return os.Remove(d.Path)
}