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

Commit

Permalink
try to fix coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
blankdots committed Dec 14, 2023
1 parent a01afe7 commit 4e3ef56
Showing 1 changed file with 80 additions and 4 deletions.
84 changes: 80 additions & 4 deletions api/sda/sda.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,30 +207,106 @@ func Download(c *gin.Context) {
}

// Stitch file and prepare it for streaming
fileStream, err := stitchFile(fileDetails.Header, file, coordinates)
fileStream, err := stitchFile(fileDetails.Header, file)
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)
// Get query params
qStart := r.URL.Query().Get("startCoordinate")

Check failure on line 219 in api/sda/sda.go

View workflow job for this annotation

GitHub Actions / Build and test (1.20)

undefined: r

Check failure on line 219 in api/sda/sda.go

View workflow job for this annotation

GitHub Actions / Check code (1.20)

undefined: r
qEnd := r.URL.Query().Get("endCoordinate")

Check failure on line 220 in api/sda/sda.go

View workflow job for this annotation

GitHub Actions / Build and test (1.20)

undefined: r

Check failure on line 220 in api/sda/sda.go

View workflow job for this annotation

GitHub Actions / Check code (1.20)

undefined: r

// Parse and verify coordinates are valid
if len(qStart) > 0 && len(qEnd) > 0 {
start, err := strconv.ParseUint(qStart, 10, 64)
if err != nil {
log.Errorf("failed to convert start coordinate %d to integer, %s", start, err)

return nil, errors.New("startCoordinate must be an integer")

Check failure on line 228 in api/sda/sda.go

View workflow job for this annotation

GitHub Actions / Build and test (1.20)

too many return values

Check failure on line 228 in api/sda/sda.go

View workflow job for this annotation

GitHub Actions / Check code (1.20)

too many return values
}
end, err := strconv.ParseUint(qEnd, 10, 64)
if err != nil {
log.Errorf("failed to convert end coordinate %d to integer, %s", end, err)

return nil, errors.New("endCoordinate must be an integer")

Check failure on line 234 in api/sda/sda.go

View workflow job for this annotation

GitHub Actions / Build and test (1.20)

too many return values

Check failure on line 234 in api/sda/sda.go

View workflow job for this annotation

GitHub Actions / Check code (1.20)

too many return values
}
if end < start {
log.Errorf("endCoordinate=%d must be greater than startCoordinate=%d", end, start)

return nil, errors.New("endCoordinate must be greater than startCoordinate")

Check failure on line 239 in api/sda/sda.go

View workflow job for this annotation

GitHub Actions / Build and test (1.20)

too many return values

Check failure on line 239 in api/sda/sda.go

View workflow job for this annotation

GitHub Actions / Check code (1.20)

too many return values
}

if start != 0 {
// We don't want to read from start, skip ahead to where we should be
if _, err := reader.Seek(start, 0); err != nil {

Check failure on line 244 in api/sda/sda.go

View workflow job for this annotation

GitHub Actions / Build and test (1.20)

undefined: reader

Check failure on line 244 in api/sda/sda.go

View workflow job for this annotation

GitHub Actions / Check code (1.20)

undefined: reader
return err
}
}

// Calculate how much we should read (if given)
togo := end - start

buf := make([]byte, 4096)

// Loop until we've read what we should (if no/faulty end given, that's EOF)
for end == 0 || togo > 0 {
rbuf := buf[:]

if end != 0 && togo < 4096 {
// If we don't want to read as much as 4096 bytes
rbuf = buf[:togo]
}
r, err := fileStream.Read(rbuf)
togo -= int64(r)

Check failure on line 263 in api/sda/sda.go

View workflow job for this annotation

GitHub Actions / Build and test (1.20)

invalid operation: togo -= int64(r) (mismatched types uint64 and int64)

Check failure on line 263 in api/sda/sda.go

View workflow job for this annotation

GitHub Actions / Check code (1.20)

invalid operation: togo -= int64(r) (mismatched types uint64 and int64)

// Nothing more to read?
if err == io.EOF && r == 0 {
// Fall out without error if we had EOF (if we got any data, do one
// more lap in the loop)
return nil

Check failure on line 269 in api/sda/sda.go

View workflow job for this annotation

GitHub Actions / Build and test (1.20)

too many return values

Check failure on line 269 in api/sda/sda.go

View workflow job for this annotation

GitHub Actions / Check code (1.20)

too many return values
}

if err != nil && err != io.EOF {
// An error we want to signal?
return err

Check failure on line 274 in api/sda/sda.go

View workflow job for this annotation

GitHub Actions / Build and test (1.20)

too many return values

Check failure on line 274 in api/sda/sda.go

View workflow job for this annotation

GitHub Actions / Check code (1.20)

too many return values
}

wbuf := rbuf[:r]
for len(wbuf) > 0 {
// Loop until we've written all that we could read,
// fall out on error
sendStream(c.Writer, wbuf)

Check failure on line 281 in api/sda/sda.go

View workflow job for this annotation

GitHub Actions / Build and test (1.20)

cannot use wbuf (variable of type []byte) as io.Reader value in argument to sendStream: []byte does not implement io.Reader (missing method Read)

if err != nil {
return err
}
wbuf = wbuf[w:]
}
}

} else {
return
}

}

// 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) {
var stitchFile = func(header []byte, file io.ReadCloser) (*streaming.Crypt4GHReader, error) {
log.Debugf("stitching header to file %s for streaming", file)
// Stitch header and file body together
hr := bytes.NewReader(header)
mr := io.MultiReader(hr, file)
c4ghr, err := streaming.NewCrypt4GHReader(mr, *config.Config.App.Crypt4GHKey, coordinates)
c4ghr, err := streaming.NewCrypt4GHReader(mr, *config.Config.App.Crypt4GHKey, nil)
if err != nil {
log.Errorf("failed to create Crypt4GH stream reader, %v", err)

return nil, err
}
defer c4ghr.Close()
log.Debugf("file stream for %s constructed", file)

return c4ghr, nil
Expand Down

0 comments on commit 4e3ef56

Please sign in to comment.