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

Commit

Permalink
cleanup coords
Browse files Browse the repository at this point in the history
  • Loading branch information
MalinAhlberg committed Feb 14, 2024
1 parent 946af3b commit bb7c57c
Showing 1 changed file with 5 additions and 23 deletions.
28 changes: 5 additions & 23 deletions api/sda/sda.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,26 +197,17 @@ func Download(c *gin.Context) {
return
}

// Calculate the content length
contentLength := fileDetails.DecryptedSize
log.Debug("decrypted size", fileDetails.DecryptedSize)
if c.Param("type") == "encrypted" {
if start == 0 && end == 0 {
log.Debug("using archive size", fileDetails.ArchiveSize)
contentLength = fileDetails.ArchiveSize
} else {
htsgetRange := c.GetHeader("Range")
log.Debug("got header", htsgetRange)
start, end = calculateEncryptedCoords(start, end, htsgetRange, fileDetails)
contentLength = int(end)
log.Debug("calculated end to", end)
}
contentLength = fileDetails.ArchiveSize
start, end = calculateEncryptedCoords(start, end, c.GetHeader("Range"), fileDetails)
}
if start == 0 && end == 0 {
c.Header("Content-Length", fmt.Sprint(contentLength))
} else {
// Calculate how much we should read (if given)
togo := end - start
log.Debug("partial file! set togo to", togo)
c.Header("Content-Length", fmt.Sprint(togo))
}

Expand Down Expand Up @@ -362,17 +353,15 @@ var sendStream = func(reader io.Reader, writer http.ResponseWriter, start, end i
}

// Calculates the start and end coordinats to use. If a range is set in HTTP headers,
// it will be used as is. If not, url params (`endCoord`, `startCoord`) will be checked,
// it will be used as is. If not, the functions parameters will be used,
// and adjusted to match the data block boundaries of the encrypted file.
var calculateEncryptedCoords = func(start, end int64, htsget_range string, fileDetails *database.FileDownload) (int64, int64) {
if htsget_range != "" {
log.Debug("trim", strings.TrimPrefix(htsget_range, "bytes="))
startEnd := strings.Split(strings.TrimPrefix(htsget_range, "bytes="), "-")
if len(startEnd) > 1 {
a, errA := strconv.ParseInt(startEnd[0], 10, 64)
b, errB := strconv.ParseInt(startEnd[1], 10, 64)
if errA == nil && errB == nil {
log.Debug("all good, use range", a, b)
if errA == nil && errB == nil && a < b {

return a, b
}
Expand All @@ -384,16 +373,9 @@ var calculateEncryptedCoords = func(start, end int64, htsget_range string, fileD
if end > 0 {
var packageSize float64 = 65564 // 64KiB+28 but TODO 28 is for chacha20_ietf_poly1305
togo := end - start
log.Debug("headlength size: ", headlength.Size())
bodysize := math.Max(float64(togo-headlength.Size()), 0)
log.Debug("body size: ", bodysize)
log.Debug("#packages: ", math.Ceil(bodysize/packageSize))
endCoord := packageSize * math.Ceil(bodysize/packageSize)
log.Debug("endCoord: ", endCoord)
bodyEnd = int64(math.Min(float64(bodyEnd), endCoord))
log.Debug("body end: ", bodyEnd)
}
log.Debug("setting end: ", headlength.Len()+int(bodyEnd))

return start, headlength.Size() + bodyEnd
}

0 comments on commit bb7c57c

Please sign in to comment.