-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy patharchives.go
More file actions
215 lines (180 loc) · 4.64 KB
/
archives.go
File metadata and controls
215 lines (180 loc) · 4.64 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"time"
"github.com/gorilla/mux"
"github.com/landjur/golibrary/uuid"
)
type Archive struct {
SavePath string
Name string
Key string
Expire int64
}
var ArchiveStore map[string]Archive
// Given an string id searches for a file and delivers it to the client
func DownloadArchiveHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
key, ok := vars["archiveKey"]
if !ok {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "{\"error\": \"Not found\"}")
return
}
archive, ok := ArchiveStore[key]
if !ok {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "{\"error\": \"Not found\"}")
return
}
if archive.Expire < time.Now().Unix() {
// The archive have expire, clean it up!
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "{\"error\": \"Not found\"}")
return
}
filename := url.QueryEscape(archive.Name)
w.Header().Set("Content-Disposition", "attachment; filename="+filename)
http.ServeFile(w, r, archive.SavePath)
}
// Updates the file name and expire date
func UpdateArchiveHandler(w http.ResponseWriter, r *http.Request) {
if _, ok := CheckAuth(r); !ok {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(w, "{\"error\": \"Unauthorized\"}")
return
}
vars := mux.Vars(r)
if _, ok := vars["archiveKey"]; ok {
// TODO: Actually update the archive info
fmt.Fprintf(w, "{\"status\": \"updated\"}")
return
}
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "{\"error\": \"Not found\"}")
}
// Deletes a file from the server
func deleteFile(filename string) error {
err := os.Remove(filename)
if err != nil {
return err
}
return nil
}
func DeleteArchiveHandler(w http.ResponseWriter, r *http.Request) {
if _, ok := CheckAuth(r); !ok {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(w, "{\"error\": \"Unauthorized\"}")
return
}
vars := mux.Vars(r)
key, ok := vars["archiveKey"]
if !ok {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "{\"error\": \"Bad Request\"}")
return
}
archive, ok := ArchiveStore[key]
if !ok {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "{\"error\": \"Not found\"}")
return
}
err := deleteFile(archive.SavePath)
if err != nil {
log.Println(err)
}
delete(ArchiveStore, key)
fmt.Fprintf(w, "{\"status\": \"deleted\"}")
}
// Lists all the files that the server has available to download
func ListArchiveHandler(w http.ResponseWriter, r *http.Request) {
if _, ok := CheckAuth(r); !ok {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(w, "{\"error\": \"Unauthorized\"}")
return
}
resp := map[string][]interface{}{
"archives": make([]interface{}, 0),
}
now := time.Now().Unix()
for key, archive := range ArchiveStore {
if archive.Expire < now {
err := deleteFile(archive.SavePath)
if err != nil {
log.Println(err)
}
delete(ArchiveStore, key)
}
// Do not send the server path
item := map[string]interface{}{
"Name": archive.Name,
"Key": archive.Key,
"Expire": archive.Expire,
}
resp["archives"] = append(resp["archives"], item)
}
json.NewEncoder(w).Encode(resp)
}
// Uploads a file to the server, returns the status and expire date
func AddArchiveHandler(w http.ResponseWriter, r *http.Request) {
if _, ok := CheckAuth(r); !ok {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(w, "{\"error\": \"Unauthorized\"}")
return
}
r.ParseForm()
file, handler, err := r.FormFile("upload")
if err != nil {
// Bad Request
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "{\"error\": \"Bad Request\"}")
return
}
defer file.Close()
if handler != nil {
key, err := uuid.NewV4()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "{\"error\": \"Internal Server Error\"}")
return
}
keyStr := key.String()
expire := time.Now().Add(time.Hour * 24).Unix()
r.ParseMultipartForm(32 << 20)
// Read the save directory from Conf
path := Conf["saveDir"] + "/" + keyStr
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
log.Printf("Error saving file")
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "{\"error\": \"Internal Server Error\"}")
return
}
defer f.Close()
io.Copy(f, file)
ArchiveStore[keyStr] = Archive{
Key: keyStr,
SavePath: path,
Name: handler.Filename,
Expire: expire,
}
resp := map[string]interface{}{
"Name": handler.Filename,
"Key": keyStr,
"Expire": expire,
}
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(resp)
} else {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "{\"error\": \"Bad Request\"}")
return
}
}