From e4c37e5a263b5ba7b767acb79f206176ee75d6be Mon Sep 17 00:00:00 2001 From: dbampalikis Date: Wed, 1 Nov 2023 09:19:09 +0100 Subject: [PATCH] Add case for encrypted files in download --- api/sda/sda.go | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/api/sda/sda.go b/api/sda/sda.go index 614a11e..a47e094 100644 --- a/api/sda/sda.go +++ b/api/sda/sda.go @@ -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) {