Browse Source

Merge pull request #246 from dutchcoders/ISSUE-240

ISSUE-240 negative max metadata instead of big enough
tags/v1.1.0
Andrea Spacca 4 years ago
committed by GitHub
parent
commit
82fc38856d
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 14 deletions
  1. +23
    -14
      server/handlers.go

+ 23
- 14
server/handlers.go View File

@@ -377,9 +377,9 @@ type Metadata struct {
func MetadataForRequest(contentType string, r *http.Request) Metadata { func MetadataForRequest(contentType string, r *http.Request) Metadata {
metadata := Metadata{ metadata := Metadata{
ContentType: contentType, ContentType: contentType,
MaxDate: time.Now().Add(time.Hour * 24 * 365 * 10),
MaxDate: time.Time{},
Downloads: 0, Downloads: 0,
MaxDownloads: 99999999,
MaxDownloads: -1,
DeletionToken: Encode(10000000+int64(rand.Intn(1000000000))) + Encode(10000000+int64(rand.Intn(1000000000))), DeletionToken: Encode(10000000+int64(rand.Intn(1000000000))) + Encode(10000000+int64(rand.Intn(1000000000))),
} }


@@ -572,10 +572,19 @@ func getURL(r *http.Request) *url.URL {
return u return u
} }


func calcRemainingLimits(metadata Metadata) (int, int) {
remainingDownloads := metadata.MaxDownloads - metadata.Downloads
timeDifference := metadata.MaxDate.Sub(time.Now())
remainingDays := int(timeDifference.Hours()/24) + 1
func (metadata Metadata) remainingLimitHeaderValues() (remainingDownloads, remainingDays string) {
if metadata.MaxDate.IsZero() {
remainingDays = "n/a"
} else {
timeDifference := metadata.MaxDate.Sub(time.Now())
remainingDays = strconv.Itoa(int(timeDifference.Hours()/24) + 1)
}

if metadata.MaxDownloads == -1 {
remainingDownloads = "n/a"
} else {
remainingDownloads = strconv.Itoa(metadata.MaxDownloads - metadata.Downloads)
}


return remainingDownloads, remainingDays return remainingDownloads, remainingDays
} }
@@ -616,9 +625,9 @@ func (s *Server) CheckMetadata(token, filename string, increaseDownload bool) (M


if err := json.NewDecoder(r).Decode(&metadata); err != nil { if err := json.NewDecoder(r).Decode(&metadata); err != nil {
return metadata, err return metadata, err
} else if metadata.Downloads >= metadata.MaxDownloads {
} else if metadata.MaxDownloads != -1 && metadata.Downloads >= metadata.MaxDownloads {
return metadata, errors.New("MaxDownloads expired.") return metadata, errors.New("MaxDownloads expired.")
} else if time.Now().After(metadata.MaxDate) {
} else if !metadata.MaxDate.IsZero() && time.Now().After(metadata.MaxDate) {
return metadata, errors.New("MaxDate expired.") return metadata, errors.New("MaxDate expired.")
} else { } else {
// todo(nl5887): mutex? // todo(nl5887): mutex?
@@ -900,13 +909,13 @@ func (s *Server) headHandler(w http.ResponseWriter, r *http.Request) {
return return
} }


remainingDownloads, remainingDays := calcRemainingLimits(metadata)
remainingDownloads, remainingDays := metadata.remainingLimitHeaderValues()


w.Header().Set("Content-Type", contentType) w.Header().Set("Content-Type", contentType)
w.Header().Set("Content-Length", strconv.FormatUint(contentLength, 10)) w.Header().Set("Content-Length", strconv.FormatUint(contentLength, 10))
w.Header().Set("Connection", "close") w.Header().Set("Connection", "close")
w.Header().Set("X-Remaining-Downloads", strconv.Itoa(remainingDownloads))
w.Header().Set("X-Remaining-Days", strconv.Itoa(remainingDays))
w.Header().Set("X-Remaining-Downloads", remainingDownloads)
w.Header().Set("X-Remaining-Days", remainingDays)
} }


func (s *Server) getHandler(w http.ResponseWriter, r *http.Request) { func (s *Server) getHandler(w http.ResponseWriter, r *http.Request) {
@@ -944,14 +953,14 @@ func (s *Server) getHandler(w http.ResponseWriter, r *http.Request) {
disposition = "attachment" disposition = "attachment"
} }


remainingDownloads, remainingDays := calcRemainingLimits(metadata)
remainingDownloads, remainingDays := metadata.remainingLimitHeaderValues()


w.Header().Set("Content-Type", contentType) w.Header().Set("Content-Type", contentType)
w.Header().Set("Content-Length", strconv.FormatUint(contentLength, 10)) w.Header().Set("Content-Length", strconv.FormatUint(contentLength, 10))
w.Header().Set("Content-Disposition", fmt.Sprintf("%s; filename=\"%s\"", disposition, filename)) w.Header().Set("Content-Disposition", fmt.Sprintf("%s; filename=\"%s\"", disposition, filename))
w.Header().Set("Connection", "keep-alive") w.Header().Set("Connection", "keep-alive")
w.Header().Set("X-Remaining-Downloads", strconv.Itoa(remainingDownloads))
w.Header().Set("X-Remaining-Days", strconv.Itoa(remainingDays))
w.Header().Set("X-Remaining-Downloads", remainingDownloads)
w.Header().Set("X-Remaining-Days", remainingDays)


if w.Header().Get("Range") == "" { if w.Header().Get("Range") == "" {
if _, err = io.Copy(w, reader); err != nil { if _, err = io.Copy(w, reader); err != nil {


Loading…
Cancel
Save