-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileupload.go
159 lines (129 loc) · 4.61 KB
/
fileupload.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"github.com/gorilla/mux"
"github.com/mehmetkule/go-restapi/internal/dto"
"github.com/mehmetkule/go-restapi/internal/store"
"github.com/mehmetkule/go-restapi/logger"
"io/ioutil"
"mime/multipart"
"net/http"
)
const maxuploadsize = 32 << 20
// AddFile adds file to database and creates a json response of the data
func (app *App) AddFile(writer http.ResponseWriter, req *http.Request) {
// read the request body and request param
logger.Logger().Debug("Adding File application")
params := mux.Vars(req)
parentID := params["parent_id"]
if parentID == "" {
app.RenderErrorResponse(writer, http.StatusBadRequest, nil, "Parent id is not defined")
return
}
//req.Body = http.MaxBytesReader(writer, req.Body, maxuploadsize)
if err := req.ParseMultipartForm(maxuploadsize); err != nil {
app.RenderErrorResponse(writer, http.StatusBadRequest, err, "The uploaded file is too big. Please choose an file that's less than 1MB in size")
return
}
data := req.MultipartForm.File["file"]
var request dto.FileResponse
_, err := request.ValidateFile(req)
if err != nil {
app.RenderErrorResponse(writer, http.StatusBadGateway, err, err.Error())
return
}
// read data from multipartform to byte array
files, errResponse := app.readData(data)
if errResponse != nil {
app.RenderErrorResponse(writer, errResponse.Status, errResponse.Error, errResponse.Message)
}
// database process
response, errResponse := app.filesRepo.InsertFiles(files, parentID)
if errResponse != nil {
app.RenderErrorResponse(writer, errResponse.Status, errResponse.Error, errResponse.Message)
return
}
// render output
app.RenderJSON(writer, http.StatusOK, response)
}
// FindFile finds file from database with id and creates a json response of the data
func (app *App) FindFile(writer http.ResponseWriter, req *http.Request) {
// read the request param
logger.Logger().Debug("Finding file")
params := mux.Vars(req)
id := params["id"]
// database process
response, errResponse := app.filesRepo.FindFile(id)
if errResponse != nil {
app.RenderErrorResponse(writer, errResponse.Status, errResponse.Error, errResponse.Message)
return
}
// render output
app.RenderJSON(writer, http.StatusOK, response)
}
// FindFiles finds all files from database with parent id and creates a json response of the data
func (app *App) FindFiles(writer http.ResponseWriter, req *http.Request) {
// read the request param
logger.Logger().Debug("Finding files")
params := mux.Vars(req)
parentID := params["parent_id"]
// database process
response, errResponse := app.filesRepo.FindFiles(parentID)
if errResponse != nil {
app.RenderErrorResponse(writer, errResponse.Status, errResponse.Error, errResponse.Message)
return
}
// render output
app.RenderJSON(writer, http.StatusOK, response)
}
// DeleteFile deletes project item from database and creates a json response of the data
func (app *App) DeleteFile(writer http.ResponseWriter, req *http.Request) {
// read the request param
params := mux.Vars(req)
id := params["id"]
// database process
errResponse := app.filesRepo.DeleteFileWithID(id)
if errResponse != nil {
app.RenderErrorResponse(writer, errResponse.Status, errResponse.Error, errResponse.Message)
return
}
// render output
app.RenderJSON(writer, http.StatusOK, "Delete File successful")
}
// DeleteFiles deletes project item from database and creates a json response of the data
func (app *App) DeleteFiles(writer http.ResponseWriter, req *http.Request) {
// read the request param
params := mux.Vars(req)
parentID := params["parent_id"]
// database process
errResponse := app.filesRepo.DeleteFilesWithParent(parentID)
if errResponse != nil {
app.RenderErrorResponse(writer, errResponse.Status, errResponse.Error, errResponse.Message)
return
}
// render output
app.RenderJSON(writer, http.StatusOK, "Delete File successful")
}
// readData reading data from multipart and creates a array byte response of the file
func (app *App) readData(files []*multipart.FileHeader) ([]store.Document, *dto.ErrorResponse) {
var filesData []store.Document
for _, f := range files {
// open file
file, err := f.Open()
if err != nil {
return nil, &dto.ErrorResponse{Status: http.StatusInternalServerError, Error: err, Message: "Failed to open file"}
}
// read file
fileData, err := ioutil.ReadAll(file)
if err != nil {
if file != nil {
file.Close()
}
return nil, &dto.ErrorResponse{Status: http.StatusInternalServerError, Error: err, Message: "Failed to read file"}
}
filesData = append(filesData, store.Document{Name: f.Filename, Data: fileData})
if file != nil {
file.Close()
}
}
return filesData, nil
}