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

Commit

Permalink
Add case for encrypted files in download
Browse files Browse the repository at this point in the history
  • Loading branch information
dbampalikis committed Nov 1, 2023
1 parent 170f9a0 commit e4c37e5
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions api/sda/sda.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,18 +198,48 @@ func Download(c *gin.Context) {
return
}

// Stitch file and prepare it for streaming
fileStream, err := stitchFile(fileDetails.Header, file, coordinates)
if err != nil {
log.Errorf("could not prepare file for streaming, %s", err)
c.String(http.StatusInternalServerError, "file stream error")
var fileStream io.Reader
switch c.Param("type") {
case "encrypted":
log.Print("Return encrypted file")
fileStream, err = stitchEncryptedFile(fileDetails.Header, file, coordinates)
if err != nil {
log.Errorf("could not prepare file for streaming, %s", err)
c.String(http.StatusInternalServerError, "file stream error")

return
return
}
c.Header("Content-Length", "")
default:
// Stitch file and prepare it for streaming
fileStream, err = stitchFile(fileDetails.Header, file, coordinates)
if err != nil {
log.Errorf("could not prepare file for streaming, %s", err)
c.String(http.StatusInternalServerError, "file stream error")

return
}
}

sendStream(c.Writer, fileStream)
}

// stitchFile stitches the header and file body together for Crypt4GHReader
// and returns a streamable Reader
var stitchEncryptedFile = func(header []byte, file io.ReadCloser, coordinates *headers.DataEditListHeaderPacket) (io.Reader, error) {
log.Debugf("stitching header to file %s for streaming", file)
// Stitch header and file body together
hr := bytes.NewReader(header)

encryptedFile := io.MultiReader(hr, io.MultiReader(hr, file))

log.Print("Encrypted file:", encryptedFile)

log.Debugf("file stream for %s constructed", file)

return encryptedFile, nil
}

// stitchFile stitches the header and file body together for Crypt4GHReader
// and returns a streamable Reader
var stitchFile = func(header []byte, file io.ReadCloser, coordinates *headers.DataEditListHeaderPacket) (*streaming.Crypt4GHReader, error) {
Expand Down

0 comments on commit e4c37e5

Please sign in to comment.