-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandlers_package.go
191 lines (168 loc) · 4.99 KB
/
handlers_package.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
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
189
190
191
package main
import (
"encoding/json"
"fmt"
"github.com/alvanrahimli/dots-server/dataaccess"
"github.com/alvanrahimli/dots-server/models"
"github.com/alvanrahimli/dots-server/utils"
"github.com/gorilla/mux"
"io"
"net/http"
"os"
"path"
)
func addPackageHandler(w http.ResponseWriter, r *http.Request) {
InfoLogger.Printf("%s: Request received for URL %s", r.Method, r.URL)
if err := r.ParseMultipartForm(32 << 20); err != nil {
_, err := fmt.Fprintf(w, "ParseForm() err: %v", err)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
ErrLogger.Println(err.Error())
return
}
}
token := r.Header.Get("Authorization")
packageName := r.FormValue("name")
packageVersion := r.FormValue("version")
file, fileHeader, fileErr := r.FormFile("archive")
if fileErr != nil {
ErrLogger.Println(fileErr.Error())
http.Error(w, "Could not read archive file", http.StatusBadRequest)
return
}
db := getDbInstance()
defer db.Close()
// Validate token
userId, isValid := utils.ValidateToken(token, db)
if !isValid {
WarnLogger.Printf("User '%s' token validation failed", userId)
http.Error(w, "", http.StatusForbidden)
return
}
user, userErr := dataaccess.FindUserById(db, userId)
if userErr != nil {
ErrLogger.Println(userErr.Error())
http.Error(w, "Could not get user", http.StatusNotFound)
return
}
archiveName := path.Join(models.ArchivesFolderRoot, user.Username, path.Base(fileHeader.Filename))
// Check user's archive folder
_, dirStatErr := os.Stat(path.Dir(archiveName))
if dirStatErr != nil {
if os.IsNotExist(dirStatErr) {
err := os.MkdirAll(path.Dir(archiveName), os.ModePerm)
if err != nil {
ErrLogger.Println(err.Error())
http.Error(w, "Unexpected error occurred", http.StatusInternalServerError)
return
}
} else {
ErrLogger.Println(dirStatErr.Error())
http.Error(w, "Unexpected error occurred", http.StatusInternalServerError)
return
}
}
localFile, createErr := os.Create(archiveName)
if createErr != nil {
ErrLogger.Println(createErr)
http.Error(w, "Could not save archive file", http.StatusInternalServerError)
return
}
defer localFile.Close()
if _, copyErr := io.Copy(localFile, file); copyErr != nil {
ErrLogger.Println(copyErr.Error())
http.Error(w, "Could not save archive file", http.StatusInternalServerError)
return
}
// pack.Name = [email protected]
pack := models.Package{
Id: 0,
Name: fmt.Sprintf("%s@%s.%s", packageName, user.Username, models.RegistryDomain),
Version: packageVersion,
ArchiveName: archiveName,
UserId: userId,
}
InfoLogger.Println(pack.Name)
response := models.HttpResponse{}
validationErrors, isValid := utils.ValidatePackage(&pack)
if !isValid {
w.WriteHeader(http.StatusBadRequest)
response = models.HttpResponse{
Code: 1,
Message: "One or more validation failed",
Data: map[string]interface{}{
"result": validationErrors,
},
}
} else {
// Add package
pack, addErr := dataaccess.AddPackage(&pack, db)
if addErr != nil {
ErrLogger.Println(addErr)
http.Error(w, "Could not add package", http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusOK)
response = models.HttpResponse{
Code: 0,
Message: "Successfully created package",
Data: map[string]interface{}{
"package": pack,
},
}
}
responseJson, jsonErr := json.Marshal(response)
if jsonErr != nil {
ErrLogger.Println(jsonErr.Error())
http.Error(w, "Could not marshal response dto", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
_, writeErr := w.Write(responseJson)
if writeErr != nil {
ErrLogger.Println(writeErr.Error())
http.Error(w, "Could not write response", http.StatusInternalServerError)
return
}
}
func getPackagesHandler(w http.ResponseWriter, r *http.Request) {
InfoLogger.Printf("Request received for URL %s", r.URL)
packageName := mux.Vars(r)["name"]
if packageName == "" {
http.Error(w, "Package name not provided", http.StatusBadRequest)
return
}
db := getDbInstance()
defer db.Close()
packages, getErr := dataaccess.GetPackages(packageName, db)
if getErr != nil {
ErrLogger.Println(getErr.Error())
http.Error(w, "Error occurred while getting packages", http.StatusInternalServerError)
return
}
if len(packages) == 0 {
http.Error(w, "Could not find any package", http.StatusNotFound)
return
}
response := models.HttpResponse{
Code: 0,
Message: fmt.Sprintf("Found %d packages", len(packages)),
Data: map[string]interface{}{
"Packages": packages,
},
}
responseJson, jsonErr := json.Marshal(response)
if jsonErr != nil {
ErrLogger.Println(jsonErr.Error())
http.Error(w, "Could not marshal response dto", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
_, writeErr := w.Write(responseJson)
if writeErr != nil {
ErrLogger.Println(writeErr.Error())
http.Error(w, "Could not write response", http.StatusInternalServerError)
return
}
}