-
Notifications
You must be signed in to change notification settings - Fork 23
/
get.go
108 lines (86 loc) · 3.14 KB
/
get.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
package handlers
import (
"fmt"
"net/http"
"time"
"github.com/go-redis/redis"
"github.com/gofiber/fiber/v2"
uuid "github.com/gofrs/uuid"
"github.com/red-gold/telar-core/pkg/log"
"github.com/red-gold/telar-core/utils"
appConfig "github.com/red-gold/telar-web/micros/storage/config"
)
var redisClient *redis.Client
func init() {
}
// @Summary Download a file from the storage
// @Description Download a file from the storage
// @Tags download
// @Produce json
// @Security JWT
// @Param Authorization header string true "Authentication" default(Bearer <Add_token_here>)
// @Param dir path string true "Directory name"
// @Param name path string true "File name"
// @Param uid path string true "User ID"
// @Success 302
// @Failure 400 {object} utils.TelarError
// @Router /{uid}/{dir}/{name} [get]
func GetFileHandle(c *fiber.Ctx) error {
storageConfig := &appConfig.StorageConfig
// Initialize Redis Connection
if redisClient == nil {
redisPassword, redisErr := utils.ReadSecret("redis-pwd")
if redisErr != nil {
fmt.Printf("\n\ncouldn't get payload-secret: %s\n\n", redisErr.Error())
}
fmt.Println(storageConfig.RedisAddress)
fmt.Println(redisPassword)
redisClient = redis.NewClient(&redis.Options{
Addr: storageConfig.RedisAddress,
Password: redisPassword,
DB: 0,
})
pong, err := redisClient.Ping().Result()
fmt.Println(pong, err)
}
log.Info("File Upload Endpoint Hit")
dirName := c.Params("dir")
if dirName == "" {
errorMessage := fmt.Sprintf("Directory name is required!")
log.Error(errorMessage)
return c.Status(http.StatusBadRequest).JSON(utils.Error("dirNameRequired", "Directory name is required!"))
}
log.Info("Directory name: %s", dirName)
fileName := c.Params("name")
if fileName == "" {
errorMessage := fmt.Sprintf("File name is required!")
log.Error(errorMessage)
return c.Status(http.StatusBadRequest).JSON(utils.Error("fileNameRequired", "File name is required!"))
}
log.Info("File name: %s", fileName)
userId := c.Params("uid")
if userId == "" {
errorMessage := fmt.Sprintf("User Id is required!")
log.Error(errorMessage)
return c.Status(http.StatusBadRequest).JSON(utils.Error("fileNameRequired", "User id is required!"))
}
log.Info("\n User ID: %s", userId)
userUUID, uuidErr := uuid.FromString(userId)
if uuidErr != nil {
errorMessage := fmt.Sprintf("UUID Error %s", uuidErr.Error())
log.Error(errorMessage)
return c.Status(http.StatusBadRequest).JSON(utils.Error("uuidError", "can not parseUser id!"))
}
objectName := fmt.Sprintf("%s/%s/%s", userUUID, dirName, fileName)
// Generate download URL
downloadURL, urlErr := generateV4GetObjectSignedURL(storageConfig.BucketName, objectName, storageConfig.StorageSecret)
if urlErr != nil {
fmt.Println(urlErr.Error())
}
cacheSince := time.Now().Format(http.TimeFormat)
cacheUntil := time.Now().Add(time.Second * time.Duration(cacheTimeout)).Format(http.TimeFormat)
c.Set("Cache-Control", fmt.Sprintf("max-age:%d, public", cacheTimeout))
c.Set("Last-Modified", cacheSince)
c.Set("Expires", cacheUntil)
return c.Redirect(downloadURL, http.StatusTemporaryRedirect)
}