77 "io"
88 "mime/multipart"
99 "os"
10+ "strings"
1011
1112 "github.com/aws/aws-sdk-go-v2/aws"
1213 "github.com/aws/aws-sdk-go-v2/config"
@@ -19,6 +20,7 @@ type KatapultStorageService struct {
1920 bucketName string
2021 region string
2122 endpoint string
23+ isDev bool
2224}
2325
2426func NewKatapultStorageService () * KatapultStorageService {
@@ -27,6 +29,7 @@ func NewKatapultStorageService() *KatapultStorageService {
2729 endpoint := os .Getenv ("KATAPULT_ENDPOINT" )
2830 accessKey := os .Getenv ("KATAPULT_ACCESS_KEY" )
2931 secretKey := os .Getenv ("KATAPULT_SECRET_KEY" )
32+ isDev := os .Getenv ("APP_ENV" ) == "dev"
3033
3134 // Configure AWS SDK
3235 cfg , err := config .LoadDefaultConfig (context .TODO (),
@@ -47,9 +50,31 @@ func NewKatapultStorageService() *KatapultStorageService {
4750 bucketName : bucketName ,
4851 region : region ,
4952 endpoint : endpoint ,
53+ isDev : isDev ,
5054 }
5155}
5256
57+ func (s * KatapultStorageService ) getObjectKey (taskID uint , filename string , fileType string ) string {
58+ var path string
59+ if fileType == "mesh" {
60+ path = fmt .Sprintf ("objects/%d/%s" , taskID , filename )
61+ } else {
62+ path = fmt .Sprintf ("uploads/%d/%s" , taskID , filename )
63+ }
64+
65+ if s .isDev {
66+ return "development/" + path
67+ }
68+ return path
69+ }
70+
71+ func (s * KatapultStorageService ) getFilePath (filepath string ) string {
72+ if s .isDev && ! strings .HasPrefix (filepath , "development/" ) {
73+ return "development/" + filepath
74+ }
75+ return filepath
76+ }
77+
5378func (s * KatapultStorageService ) UploadFile (file * multipart.FileHeader , taskID uint , fileType string ) (string , error ) {
5479 src , err := file .Open ()
5580 if err != nil {
@@ -86,7 +111,7 @@ func (s *KatapultStorageService) GetFile(filepath string) (io.ReadCloser, error)
86111 // Get object from S3
87112 result , err := s .client .GetObject (context .TODO (), & s3.GetObjectInput {
88113 Bucket : aws .String (s .bucketName ),
89- Key : aws .String (filepath ),
114+ Key : aws .String (s . getFilePath ( filepath ) ),
90115 })
91116 if err != nil {
92117 return nil , fmt .Errorf ("failed to get file: %w" , err )
@@ -109,10 +134,3 @@ func (s *KatapultStorageService) DeleteFile(taskID uint, filename string) error
109134
110135 return nil
111136}
112-
113- func (s * KatapultStorageService ) getObjectKey (taskID uint , filename string , fileType string ) string {
114- if fileType == "mesh" {
115- return fmt .Sprintf ("objects/%d/%s" , taskID , filename )
116- }
117- return fmt .Sprintf ("uploads/%d/%s" , taskID , filename )
118- }
0 commit comments