Skip to content

Commit 8dc676f

Browse files
committed
Put route, bug fixes
1 parent 342107a commit 8dc676f

File tree

5 files changed

+126
-12
lines changed

5 files changed

+126
-12
lines changed

proxy/handlers/files.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
package handlers
22

33
import (
4-
"github.com/sio2project/ft-to-s3/v1/utils"
5-
64
"net/http"
5+
"strings"
6+
7+
"github.com/sio2project/ft-to-s3/v1/utils"
78
)
89

910
func Files(w http.ResponseWriter, r *http.Request, logger *utils.LoggerObject, bucketName string) {
1011
if r.Method == http.MethodPut {
1112
Put(w, r, logger, bucketName)
13+
} else if r.Method == http.MethodGet {
14+
if strings.HasPrefix(r.URL.Path, "/files/") {
15+
GetFiles(w, r, logger, bucketName)
16+
} else if strings.HasPrefix(r.URL.Path, "/list/") {
17+
GetList(w, r, logger, bucketName)
18+
} else {
19+
w.WriteHeader(http.StatusBadRequest)
20+
}
1221
} else {
1322
w.WriteHeader(http.StatusMethodNotAllowed)
1423
}

proxy/handlers/get_files.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package handlers
2+
3+
import (
4+
"github.com/sio2project/ft-to-s3/v1/storage"
5+
"github.com/sio2project/ft-to-s3/v1/utils"
6+
"io"
7+
"net/http"
8+
"strconv"
9+
)
10+
11+
func GetFiles(w http.ResponseWriter, r *http.Request, logger *utils.LoggerObject, bucketName string) {
12+
path := r.URL.Path[len("/files/"):]
13+
result := storage.Get(bucketName, logger, path)
14+
if result.Err != nil {
15+
logger.Error("Error", result.Err)
16+
w.WriteHeader(http.StatusInternalServerError)
17+
return
18+
}
19+
if !result.Found {
20+
w.WriteHeader(http.StatusNotFound)
21+
return
22+
}
23+
if result.Gziped {
24+
w.Header().Set("Content-Encoding", "gzip")
25+
}
26+
w.Header().Set("Logical-Size", strconv.FormatInt(result.LogicalSize, 10))
27+
w.Header().Set("Last-Modified", toRFC2822(result.LastModified))
28+
w.WriteHeader(http.StatusOK)
29+
_, err := io.Copy(w, result.File)
30+
if err != nil {
31+
logger.Error("Error", err)
32+
}
33+
}

proxy/handlers/get_list.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package handlers
2+
3+
import (
4+
"github.com/sio2project/ft-to-s3/v1/utils"
5+
"net/http"
6+
)
7+
8+
func GetList(w http.ResponseWriter, r *http.Request, logger *utils.LoggerObject, bucketName string) {
9+
//path := r.URL.Path[len("/list/"):]
10+
}

storage/main.go

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ package storage
33
import (
44
"bytes"
55
"context"
6-
"io"
7-
86
"github.com/minio/minio-go/v7"
97
"github.com/sio2project/ft-to-s3/v1/db"
108
"github.com/sio2project/ft-to-s3/v1/utils"
9+
"io"
1110
)
1211

1312
func Store(bucketName string, logger *utils.LoggerObject, path string, reader io.Reader, version int64, size int64,
1413
compressed bool, sha256Digest string, logicalSize int64) (int64, error) {
14+
logger.Debug("storage.Store called on", bucketName+":"+path)
15+
1516
session := db.GetSession()
1617
defer session.Close()
1718
fileMutex := db.GetMutex(session, bucketName+":"+path)
@@ -25,33 +26,37 @@ func Store(bucketName string, logger *utils.LoggerObject, path string, reader io
2526
if version <= dbModified {
2627
return dbModified, nil
2728
}
29+
logger.Debug("Version is greater than dbModified")
2830

2931
oldFile, err := db.GetHashForPath(bucketName, path)
3032
if err != nil {
3133
return 0, err
3234
}
3335

3436
options := minio.PutObjectOptions{}
37+
var data bytes.Buffer
3538
if sha256Digest == "" || logicalSize == -1 {
36-
var data []byte
39+
var tempData []byte
40+
teeReader := io.TeeReader(reader, &data)
3741
if compressed {
38-
data, err = utils.ReadGzip(reader)
42+
tempData, err = utils.ReadGzip(teeReader)
3943
if err != nil {
4044
return 0, err
4145
}
4246
} else {
43-
data, err = io.ReadAll(reader)
47+
tempData, err = io.ReadAll(teeReader)
4448
if err != nil {
4549
return 0, err
4650
}
4751
}
4852

49-
sha256Digest = utils.Sha256Checksum(data)
50-
logicalSize = int64(len(data))
51-
reader = bytes.NewReader(data)
53+
sha256Digest = utils.Sha256Checksum(tempData)
54+
logicalSize = int64(len(tempData))
5255
}
5356
if compressed {
57+
logger.Debug("Setting ContentEncoding to gzip")
5458
options.ContentEncoding = "gzip"
59+
options.ContentType = "application/gzip"
5560
}
5661

5762
refCount, err := db.GetRefCount(bucketName, sha256Digest)
@@ -60,10 +65,10 @@ func Store(bucketName string, logger *utils.LoggerObject, path string, reader io
6065
}
6166

6267
if refCount == 0 {
63-
logger.Debug("Storing with options ", options)
68+
logger.Debug("Storing with options", options)
6469
minioClient := GetClient()
6570

66-
_, err = minioClient.PutObject(context.Background(), bucketName, sha256Digest, reader, size, options)
71+
_, err = minioClient.PutObject(context.Background(), bucketName, sha256Digest, &data, size, options)
6772
if err != nil {
6873
return 0, err
6974
}
@@ -98,3 +103,48 @@ func deleteByHash(bucketName string, logger *utils.LoggerObject, path string, lo
98103
logger.Debug("DeleteByHash called on ", path)
99104
return nil
100105
}
106+
107+
func Get(bucketName string, logger *utils.LoggerObject, path string) *GetResult {
108+
logger.Debug("storage.Get called on", bucketName+":"+path)
109+
110+
fileHash, err := db.GetHashForPath(bucketName, path)
111+
if err != nil {
112+
return &GetResult{Err: err}
113+
}
114+
if fileHash == "" {
115+
return &GetResult{Found: false}
116+
}
117+
118+
lastModified, err := db.GetModified(bucketName, path)
119+
if err != nil {
120+
return &GetResult{Err: err}
121+
}
122+
123+
minioClient := GetClient()
124+
info, err := minioClient.StatObject(context.Background(), bucketName, fileHash, minio.StatObjectOptions{})
125+
if err != nil {
126+
minioErr := minio.ToErrorResponse(err)
127+
if minioErr.Code == "NoSuchKey" {
128+
return &GetResult{Found: false}
129+
}
130+
return &GetResult{Err: err}
131+
}
132+
gziped := info.ContentType == "application/gzip"
133+
134+
reader, err := minioClient.GetObject(context.Background(), bucketName, fileHash, minio.GetObjectOptions{})
135+
if err != nil {
136+
minioErr := minio.ToErrorResponse(err)
137+
if minioErr.Code == "NoSuchKey" {
138+
return &GetResult{Found: false}
139+
}
140+
return &GetResult{Err: err}
141+
}
142+
143+
return &GetResult{
144+
Found: true,
145+
File: reader,
146+
Gziped: gziped,
147+
LastModified: lastModified,
148+
LogicalSize: info.Size,
149+
}
150+
}

storage/structs.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package storage
2+
3+
import "io"
4+
5+
type GetResult struct {
6+
Found bool
7+
File io.Reader
8+
Gziped bool
9+
LastModified int64
10+
LogicalSize int64
11+
Err error
12+
}

0 commit comments

Comments
 (0)