Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seekable S3 support, second take #826

Merged
merged 2 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions sda-download/api/sda/sda.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,20 @@ func Download(c *gin.Context) {
}
}

wholeFile := true
if start != 0 || end != 0 {
wholeFile = false
}

// Get archive file handle
file, err := Backend.NewFileReader(fileDetails.ArchivePath)
var file io.Reader

if wholeFile {
file, err = Backend.NewFileReader(fileDetails.ArchivePath)
} else {
file, err = Backend.NewFileReadSeeker(fileDetails.ArchivePath)
}

if err != nil {
log.Errorf("could not find archive file %s, %s", fileDetails.ArchivePath, err)
c.String(http.StatusInternalServerError, "archive error")
Expand Down Expand Up @@ -368,9 +380,21 @@ func Download(c *gin.Context) {
return
}
log.Debugf("Reencrypted c4gh file header = %v", newHeader)

newHr := bytes.NewReader(newHeader)
fileStream = io.MultiReader(newHr, file)

if wholeFile {
fileStream = io.MultiReader(newHr, file)
} else {
seeker, _ := file.(io.ReadSeeker)
fileStream, err = storage.SeekableMultiReader(newHr, seeker)
if err != nil {
log.Errorf("Failed to construct SeekableMultiReader, reason: %v", err)
c.String(http.StatusInternalServerError, "file decoding error")

return
}
}
default:
// Reencrypt header for use with our temporary key
newHeader, err := reencryptHeader(fileDetails.Header, config.Config.App.Crypt4GHPublicKeyB64)
Expand All @@ -381,8 +405,22 @@ func Download(c *gin.Context) {
return
}

encryptedFileReader := io.MultiReader(bytes.NewReader(newHeader), file)
c4ghfileStream, err := streaming.NewCrypt4GHReader(encryptedFileReader, config.Config.App.Crypt4GHPrivateKey, nil)
newHr := bytes.NewReader(newHeader)

if wholeFile {
fileStream = io.MultiReader(newHr, file)
} else {
seeker, _ := file.(io.ReadSeeker)
fileStream, err = storage.SeekableMultiReader(newHr, seeker)
if err != nil {
log.Errorf("Failed to construct SeekableMultiReader, reason: %v", err)
c.String(http.StatusInternalServerError, "file decoding error")

return
}
}

c4ghfileStream, err := streaming.NewCrypt4GHReader(fileStream, config.Config.App.Crypt4GHPrivateKey, nil)
defer c4ghfileStream.Close()
if err != nil {
log.Errorf("could not prepare file for streaming, %s", err)
Expand Down
Loading
Loading