-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy paths3zipper.go
More file actions
374 lines (321 loc) · 9.58 KB
/
s3zipper.go
File metadata and controls
374 lines (321 loc) · 9.58 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package main
import (
"archive/zip"
"context"
"encoding/json"
"errors"
"io"
"log"
"log/slog"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
cfg "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
redigo "github.com/gomodule/redigo/redis"
)
type Configuration struct {
Bucket string
Region string
RedisServerAndPort string
Port int
AccessKey *string
SecretKey *string
}
var configData = Configuration{}
var redisPool *redigo.Pool
var awsS3Client *s3.Client
type RedisFile struct {
FileName string
Folder string
S3Path string
// Optional - we use are Teamwork.com but feel free to rmove
FileId int64 `json:",string"`
ProjectId int64 `json:",string"`
ProjectName string
Modified string
ModifiedTime time.Time
}
var logger *slog.Logger
func initLogger() {
logLevel := os.Getenv("LOG_LEVEL")
var level slog.Level
switch strings.ToLower(logLevel) {
case "debug":
level = slog.LevelDebug
default:
level = slog.LevelInfo
}
handler := slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: level,
})
logger = slog.New(handler)
slog.SetDefault(logger)
}
func main() {
if 1 == 0 {
test()
return
}
initLogger()
configFilePath := os.Getenv("CONFIG_FILE")
if configFilePath == "" {
configFilePath = "/go/src/s3zipper/conf.json"
}
configFile, err := os.Open(configFilePath)
if err != nil {
panic("Error opening conf.json: " + err.Error())
}
slog.Info("Opened configuration file", "path", configFilePath)
decoder := json.NewDecoder(configFile)
err = decoder.Decode(&configData)
if err != nil {
panic("Error reading conf: " + err.Error())
}
slog.Info("Loaded configuration",
"bucket", configData.Bucket,
"region", configData.Region,
"redis", configData.RedisServerAndPort,
"port", configData.Port,
)
initAwsBucket()
InitRedis()
slog.Info("Starting HTTP server", "port", configData.Port)
http.HandleFunc("/", handler)
err = http.ListenAndServe(":"+strconv.Itoa(configData.Port), nil)
if err != nil && err != http.ErrServerClosed {
log.Fatalf("HTTP server error: %s", err)
}
}
func test() {
var err error
var files []*RedisFile
jsonData := "[{\"S3Path\":\"1\\/p23216.tf_A89A5199-F04D-A2DE-5824E635AC398956.Avis_Rent_A_Car_Print_Reservation.pdf\",\"FileVersionId\":\"4164\",\"FileName\":\"Avis Rent A Car_ Print Reservation.pdf\",\"ProjectName\":\"Superman\",\"ProjectId\":\"23216\",\"Folder\":\"\",\"FileId\":\"4169\"},{\"modified\":\"2015-07-18T02:05:04Z\",\"S3Path\":\"1\\/p23216.tf_351310E0-DF49-701F-60601109C2792187.a1.jpg\",\"FileVersionId\":\"4165\",\"FileName\":\"a1.jpg\",\"ProjectName\":\"Superman\",\"ProjectId\":\"23216\",\"Folder\":\"Level 1\\/Level 2 x\\/Level 3\",\"FileId\":\"4170\"}]"
resultByte := []byte(jsonData)
err = json.Unmarshal(resultByte, &files)
if err != nil {
err = errors.New("Error decoding json: " + jsonData)
slog.Error(err.Error())
}
parseFileDates(files)
slog.Info("Test data parsed", "file_count", len(files))
}
func parseFileDates(files []*RedisFile) {
layout := "2006-01-02T15:04:05Z"
for _, file := range files {
t, err := time.Parse(layout, file.Modified)
if err != nil {
slog.Warn("Error parsing date", "date", file.Modified, "file", file.FileName, "error", err)
continue
}
file.ModifiedTime = t
}
}
func initAwsBucket() {
var awsCfg aws.Config
var err error
if configData.AccessKey != nil && configData.SecretKey != nil {
slog.Info("Using static AWS credentials from conf.json")
creds := credentials.NewStaticCredentialsProvider(
*configData.AccessKey,
*configData.SecretKey,
"", // No session token; add if needed
)
awsCfg, err = cfg.LoadDefaultConfig(context.TODO(),
cfg.WithRegion(configData.Region),
cfg.WithCredentialsProvider(creds),
)
} else {
slog.Info("Using default AWS credential provider chain (e.g., IAM role, env vars)")
awsCfg, err = cfg.LoadDefaultConfig(context.TODO(),
cfg.WithRegion(configData.Region),
)
}
if err != nil {
log.Fatalf("Unable to load AWS config: %v", err)
}
awsS3Client = s3.NewFromConfig(awsCfg)
slog.Info("AWS S3 client initialized")
}
func InitRedis() {
redisPool = &redigo.Pool{
MaxIdle: 10,
IdleTimeout: 1 * time.Second,
Dial: func() (redigo.Conn, error) {
slog.Info("Connecting to Redis", "addr", configData.RedisServerAndPort)
return redigo.Dial("tcp", configData.RedisServerAndPort)
},
TestOnBorrow: func(c redigo.Conn, t time.Time) (err error) {
if time.Since(t) < time.Minute {
return nil
}
_, err = c.Do("PING")
if err != nil {
slog.Error("Error connecting to Redis", "error", err)
}
return
},
}
slog.Info("Redis connection pool initialized")
}
// Remove all other unrecognised characters apart from
var makeSafeFileName = regexp.MustCompile(`[#<>:"/\|?*\\]`)
func getFilesFromRedis(ref string) (files []*RedisFile, err error) {
slog.Debug("Fetching files from Redis", "ref", ref)
// Testing - enable to test. Remove later.
if 1 == 0 && ref == "test" {
files = append(files, &RedisFile{FileName: "test.zip", Folder: "", S3Path: "test/test.zip"}) // Edit and duplicate line to test
return
}
redis := redisPool.Get()
defer redis.Close()
// Get the value from Redis
result, err := redis.Do("GET", "zip:"+ref)
if err != nil || result == nil {
err = errors.New("Access Denied (sorry your link has timed out)")
slog.Warn("Redis GET failed or returned nil", "ref", ref)
return
}
// Convert to bytes
var resultByte []byte
var ok bool
if resultByte, ok = result.([]byte); !ok {
err = errors.New("Error converting data stream to bytes")
slog.Error("Type assertion to []byte failed for Redis data")
return
}
// Decode JSON
err = json.Unmarshal(resultByte, &files)
if err != nil {
err = errors.New("Error decoding json: " + string(resultByte))
slog.Error("JSON Unmarshal error", "error", err)
return
}
slog.Debug("Retrieved files from Redis", "count", len(files))
// Convert modified date strings to time objects
parseFileDates(files)
return
}
func handler(w http.ResponseWriter, r *http.Request) {
start := time.Now()
slog.Debug("Handling request", "method", r.Method, "uri", r.RequestURI)
if r.URL.Query().Has("health") {
_, err := w.Write([]byte("OK"))
if err != nil {
slog.Error("Error writing health check response", "error", err)
return
}
slog.Debug("Health check responded OK")
return
}
// Get "ref" URL params
refs, ok := r.URL.Query()["ref"]
if !ok || len(refs) < 1 {
http.Error(w, "S3 File Zipper. Pass ?ref= to use.", 500)
slog.Warn("Missing ref parameter")
return
}
ref := refs[0]
slog.Debug("Request ref", "ref", ref)
// Get "downloadas" URL params
downloadas, ok := r.URL.Query()["downloadas"]
if !ok && len(downloadas) > 0 {
downloadas[0] = makeSafeFileName.ReplaceAllString(downloadas[0], "")
if downloadas[0] == "" {
downloadas[0] = "download.zip"
}
} else {
downloadas = append(downloadas, "download.zip")
}
slog.Debug("Download filename set", "filename", downloadas[0])
files, err := getFilesFromRedis(ref)
if err != nil {
http.Error(w, err.Error(), http.StatusForbidden)
slog.Error("Error fetching files from Redis", "error", err)
return
}
// Start processing the response
w.Header().Add("Content-Disposition", "attachment; filename=\""+downloadas[0]+"\"")
w.Header().Add("Content-Type", "application/zip")
// Loop over files, add them to the zip archive
zipWriter := zip.NewWriter(w)
defer func() {
err := zipWriter.Close()
if err != nil {
slog.Error("Error closing zip writer", "error", err)
} else {
slog.Debug("Zip writer closed successfully")
}
}()
for _, file := range files {
if file.S3Path == "" {
slog.Warn("Skipping file with empty S3Path", "file", file)
continue
}
// Build safe file name
safeFileName := makeSafeFileName.ReplaceAllString(file.FileName, "")
if safeFileName == "" { // Unlikely but just in case
safeFileName = "file"
}
// Read file from S3, log any errors
input := &s3.GetObjectInput{
Bucket: aws.String(configData.Bucket),
Key: aws.String(file.S3Path),
}
slog.Debug("Downloading file from S3", "bucket", configData.Bucket, "key", file.S3Path)
resp, err := awsS3Client.GetObject(context.TODO(), input)
if err != nil {
var noKey *types.NoSuchKey
if errors.As(err, &noKey) {
slog.Warn("File not found in S3", "s3_path", file.S3Path)
} else {
slog.Error("Error downloading file", "s3_path", file.S3Path, "error", err)
}
continue
}
defer resp.Body.Close()
// Build path for file within the zip
zipPath := ""
if file.ProjectId > 0 {
zipPath += strconv.FormatInt(file.ProjectId, 10) + "."
file.ProjectName = makeSafeFileName.ReplaceAllString(file.ProjectName, "")
if file.ProjectName == "" {
file.ProjectName = "Project"
}
zipPath += file.ProjectName + "/"
}
if file.Folder != "" {
zipPath += file.Folder
if !strings.HasSuffix(zipPath, "/") {
zipPath += "/"
}
}
zipPath += safeFileName
h := &zip.FileHeader{
Name: zipPath,
Method: zip.Deflate,
Flags: 0x800,
}
if file.Modified != "" {
h.Modified = file.ModifiedTime
}
f, err := zipWriter.CreateHeader(h)
if err != nil {
slog.Error("Error creating zip header", "zip_path", zipPath, "error", err)
continue
}
slog.Debug("Adding file to zip", "zip_path", zipPath)
_, err = io.Copy(f, resp.Body)
if err != nil {
slog.Error("Error writing file to zip", "zip_path", zipPath, "error", err)
continue
}
}
slog.Debug("Request processed", "duration", time.Since(start))
}