Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
Add function for returning encrypted files
Browse files Browse the repository at this point in the history
  • Loading branch information
dbampalikis committed Nov 1, 2023
1 parent f4a577b commit 170f9a0
Showing 1 changed file with 47 additions and 8 deletions.
55 changes: 47 additions & 8 deletions api/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,22 @@ func ListObjects(c *gin.Context) {
})
}

func getFileInfo(c *gin.Context) (fileInfo *database.FileInfo, err error) {
// Get file info for the given file path (or abort)
fileInfo, err = database.GetDatasetFileInfo(c.Param("dataset"), c.Param("filename")+".c4gh")
if err != nil {
if err.Error() == "sql: no rows in result set" {
c.AbortWithStatus(http.StatusNotFound)
} else {
c.AbortWithStatus(http.StatusInternalServerError)
}

return
}

return fileInfo, nil
}

// GetObject respondes to an S3 GetObject request. This request returns S3
// objects. This is done by first fetching any file that matches the dataset +
// filename request from the database and then passing the fileID to the
Expand All @@ -196,15 +212,31 @@ func ListObjects(c *gin.Context) {
func GetObject(c *gin.Context) {
log.Debugf("S3 GetObject request, context: %v", c.Params)

// Get file info for the given file path (or abort)
fileInfo, err := database.GetDatasetFileInfo(c.Param("dataset"), c.Param("filename")+".c4gh")
fileInfo, err := getFileInfo(c)
if err != nil {
if err.Error() == "sql: no rows in result set" {
c.AbortWithStatus(http.StatusNotFound)
} else {
c.AbortWithStatus(http.StatusInternalServerError)
}
return
}

// Set a param so that Download knows to add S3 headers
c.Set("S3", true)

// set the fileID so that download knows what file to download
c.Params = append(c.Params, gin.Param{Key: "fileid", Value: fileInfo.FileID})

// Download the file
sda.Download(c)
}

// GetEncryptedObject respondes to an S3 GetObject request for encrypted files.
// This request returns S3 objects. This is done by first fetching any file that matches the dataset +
// filename request from the database and then passing the fileID to the
// SDA Download function.
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetObject.html
func GetEcnryptedObject(c *gin.Context) {
log.Debugf("S3 GetEncryptedObject request, context: %v", c.Params)

fileInfo, err := getFileInfo(c)
if err != nil {
return
}

Expand All @@ -214,6 +246,9 @@ func GetObject(c *gin.Context) {
// set the fileID so that download knows what file to download
c.Params = append(c.Params, gin.Param{Key: "fileid", Value: fileInfo.FileID})

// set the encrypted parameter so that download gets the encrypted file instead
c.Params = append(c.Params, gin.Param{Key: "type", Value: "encrypted"})

// Download the file
sda.Download(c)
}
Expand Down Expand Up @@ -294,7 +329,11 @@ func Download(c *gin.Context) {
ListBuckets(c)

case c.Param("filename") != "":
GetObject(c)
if strings.Contains(c.Request.URL.String(), "encrypted") {
GetEcnryptedObject(c)
} else {
GetObject(c)
}

default:
log.Warningf("Got unknown S3 request: %v", c.Request)
Expand Down

0 comments on commit 170f9a0

Please sign in to comment.