-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_download.go
206 lines (191 loc) · 5.3 KB
/
handle_download.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package main
import (
"archive/zip"
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
func handleDownloadItem(w http.ResponseWriter, r *http.Request) {
_, err := GetTokenFromHeader(w, r)
if err != nil {
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
}
q := r.URL.Query()
id := q.Get("id")
if id == "" {
http.Error(w, "URL에 id를 입력해주세요", http.StatusBadRequest)
return
}
//mongoDB client 연결
client, err := mongo.NewClient(options.Client().ApplyURI(*flagMongoDBURI))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = client.Connect(ctx)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer client.Disconnect(ctx)
err = client.Ping(ctx, readpref.Primary())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
item, err := GetItem(client, id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// 임시 디렉토리를 생성한다.
tempDir, err := ioutil.TempDir("", "zip")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer os.RemoveAll(tempDir)
zipFileName := strings.Join(strings.Split(item.Title, " "), "_") + ".zip"
zipFilePath := filepath.Join(tempDir, zipFileName)
err = genZipfile(zipFilePath, item)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Using Rate(사용률)을 업데이트 한다.
_, err = UpdateUsingRate(client, id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Add("Content-Type", "application/zip")
w.Header().Add("Content-Disposition", fmt.Sprintf("Attachment; filename=%s", zipFileName))
http.ServeFile(w, r, zipFilePath)
}
func handleAPIDownloadZipfile(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method Only GET", http.StatusMethodNotAllowed)
return
}
q := r.URL.Query()
id := q.Get("id")
if id == "" {
http.Error(w, "Need item id", http.StatusBadRequest)
return
}
//mongoDB client 연결
client, err := mongo.NewClient(options.Client().ApplyURI(*flagMongoDBURI))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = client.Connect(ctx)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer client.Disconnect(ctx)
err = client.Ping(ctx, readpref.Primary())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
accesslevel, err := GetAccessLevelFromHeader(r, client)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
if !(accesslevel == "admin" || accesslevel == "default") {
http.Error(w, "Need permission", http.StatusUnauthorized)
return
}
item, err := GetItem(client, id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// 임시 디렉토리를 생성한다.
tempDir, err := ioutil.TempDir("", "zip")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer os.RemoveAll(tempDir)
zipFileName := strings.Join(strings.Split(item.Title, " "), "_") + ".zip"
zipFilePath := filepath.Join(tempDir, zipFileName)
err = genZipfile(zipFilePath, item)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Using Rate(사용률)을 업데이트 한다.
_, err = UpdateUsingRate(client, id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Add("Content-Type", "application/zip")
w.Header().Add("Content-Disposition", fmt.Sprintf("Attachment; filename=%s", zipFileName))
http.ServeFile(w, r, zipFilePath)
}
func genZipfile(zipFilePath string, item Item) error {
// zip 파일을 생성한다.
zipFile, err := os.Create(zipFilePath)
if err != nil {
return err
}
defer zipFile.Close()
// zip 파일에 쓰기할 준비를 한다.
zipWriter := zip.NewWriter(zipFile)
defer zipWriter.Close()
// item의 data경로에 존재하는 파일 리스트를 불러온다.
dataPath := item.OutputDataPath
files, err := ioutil.ReadDir(dataPath)
if err != nil {
return err
}
// 데이터 파일을 돌면서 zip 파일에 데이터 파일 추가한다.
for _, f := range files {
fileName, err := os.Open(dataPath + f.Name())
if err != nil {
return err
}
defer fileName.Close()
// 파일정보를 가지고 온다.
info, err := fileName.Stat()
if err != nil {
return err
}
// 압축할 때 zip 파일에 파일정보를 헤더로 설정한다.
header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
header.Method = zip.Deflate
// 헤더정보를 zip 파일에 쓴다.
writer, err := zipWriter.CreateHeader(header)
if err != nil {
return err
}
// 파일의 실제 내용을 zip 파일에 복사한다.
_, err = io.Copy(writer, fileName)
if err != nil {
return err
}
}
return nil
}