Fixed API to work with unlimited downloads and unlimited time #46

This commit is contained in:
Marc Ole Bulling
2022-02-26 20:52:53 +01:00
parent ac18cde0a8
commit 43a3a67906
5 changed files with 17 additions and 18 deletions

View File

@@ -206,7 +206,7 @@ func GetFile(id string) (models.File, bool) {
if !ok {
return emptyResult, false
}
if isExpiredFile(file, time.Now().Unix()) {
if IsExpiredFile(file, time.Now().Unix()) {
return emptyResult, false
}
if !FileExists(file, configuration.Get().DataDir) {
@@ -338,7 +338,7 @@ func CleanUp(periodic bool) {
datastorage.RunGarbageCollection()
}
func isExpiredFile(file models.File, timeNow int64) bool {
func IsExpiredFile(file models.File, timeNow int64) bool {
return (file.ExpireAt < timeNow && !file.UnlimitedTime) ||
(file.DownloadsRemaining < 1 && !file.UnlimitedDownloads)
}
@@ -347,7 +347,7 @@ func isExpiredFileWithoutDownload(file models.File, timeNow int64) bool {
if downloadstatus.IsCurrentlyDownloading(file) {
return false
}
return isExpiredFile(file, timeNow)
return IsExpiredFile(file, timeNow)
}
func deleteSource(file models.File, dataDir string) {

View File

@@ -75,23 +75,20 @@ func changeFriendlyName(w http.ResponseWriter, request apiRequest) {
key.FriendlyName = request.friendlyName
datastorage.SaveApiKey(key, false)
}
sendOk(w)
}
func deleteFile(w http.ResponseWriter, request apiRequest) {
ok := storage.DeleteFile(request.fileId, true)
if ok {
sendOk(w)
} else {
if !ok {
sendError(w, http.StatusBadRequest, "Invalid id provided.")
}
}
func list(w http.ResponseWriter) {
var validFiles []models.File
sendOk(w)
timeNow := time.Now().Unix()
for _, element := range datastorage.GetAllMetadata() {
if element.ExpireAt > time.Now().Unix() && element.DownloadsRemaining > 0 {
if !storage.IsExpiredFile(element, timeNow) {
validFiles = append(validFiles, element)
}
}
@@ -106,7 +103,6 @@ func upload(w http.ResponseWriter, request apiRequest, maxMemory int) {
sendError(w, http.StatusBadRequest, err.Error())
return
}
sendOk(w)
}
func isAuthorisedForApi(w http.ResponseWriter, request apiRequest) bool {
@@ -122,10 +118,6 @@ func sendError(w http.ResponseWriter, errorInt int, errorMessage string) {
_, _ = w.Write([]byte("{\"Result\":\"error\",\"ErrorMessage\":\"" + errorMessage + "\"}"))
}
func sendOk(w http.ResponseWriter) {
w.WriteHeader(http.StatusOK)
}
type apiRequest struct {
apiKey string
requestUrl string

View File

@@ -59,6 +59,13 @@ func parseConfig(values formOrHeader, setNewDefaults bool) models.UploadRequest
unlimitedDownload := values.Get("isUnlimitedDownload") == "true"
unlimitedTime := values.Get("isUnlimitedTime") == "true"
if allowedDownloadsInt == 0 {
unlimitedDownload = true
}
if expiryDaysInt == 0 {
unlimitedTime = true
}
if setNewDefaults {
values := models.LastUploadValues{
Downloads: allowedDownloadsInt,

View File

@@ -247,11 +247,11 @@
},
"allowedDownloads": {
"type": "integer",
"description": "How many downloads are allowed. Last used value from web interface will be used if empty."
"description": "How many downloads are allowed. Last used value from web interface will be used if empty. Unlimited if 0 is passed."
},
"expiryDays": {
"type": "integer",
"description": "How many days the file will be stored. Last used value from web interface will be used if empty."
"description": "How many days the file will be stored. Last used value from web interface will be used if empty. Unlimited if 0 is passed."
},
"password": {
"type": "string",

View File

@@ -247,11 +247,11 @@
},
"allowedDownloads": {
"type": "integer",
"description": "How many downloads are allowed. Last used value from web interface will be used if empty."
"description": "How many downloads are allowed. Last used value from web interface will be used if empty. Unlimited if 0 is passed."
},
"expiryDays": {
"type": "integer",
"description": "How many days the file will be stored. Last used value from web interface will be used if empty."
"description": "How many days the file will be stored. Last used value from web interface will be used if empty. Unlimited if 0 is passed."
},
"password": {
"type": "string",