fetch new uploads faster instead of failing

This commit is contained in:
Niko Storni 2020-05-30 02:02:11 +02:00
parent a064138cd8
commit 1fb03f82ce

40
main.go
View file

@ -8,6 +8,7 @@ import (
"fmt"
"io"
"io/ioutil"
"mime"
"net/http"
"os"
"os/user"
@ -93,13 +94,30 @@ func view(c *gin.Context) {
channelShortID := ""
var claim *chainquery.Claim
var err error
contentType := mime.TypeByExtension(filepath.Ext(claimNameWithExt))
inUploads, err := isFileInDir(uploadsDir, claimNameWithExt)
if err != nil {
logrus.Errorln(errors.FullTrace(err))
_ = c.AbortWithError(http.StatusInternalServerError, err)
return
}
inDownloads := false
if !inUploads {
inDownloads, err = isFileInDir(downloadsDir, claimNameWithExt)
if err != nil {
logrus.Errorln(errors.FullTrace(err))
_ = c.AbortWithError(http.StatusInternalServerError, err)
return
}
}
mustDownload := !inUploads && !inDownloads
if mustDownload {
if strings.Contains(id, "@") {
parts := strings.Split(id, ":")
channelName = parts[0]
if len(parts) > 1 {
channelShortID = parts[1]
}
claim, err = cqApi.ResolveClaimByChannel(claimName, channelShortID, channelName)
} else {
claim, err = cqApi.ResolveClaim(claimName, id)
@ -117,23 +135,9 @@ func view(c *gin.Context) {
c.Redirect(301, fmt.Sprintf("https://cdn.lbryplayer.xyz/content/claims/%s/%s/stream", claimName, id))
return
}
contentType = claim.ContentType
}
inUploads, err := isFileInDir(uploadsDir, claimNameWithExt)
if err != nil {
logrus.Errorln(errors.FullTrace(err))
_ = c.AbortWithError(http.StatusInternalServerError, err)
return
}
inDownloads := false
if !inUploads {
inDownloads, err = isFileInDir(downloadsDir, claimNameWithExt)
if err != nil {
logrus.Errorln(errors.FullTrace(err))
_ = c.AbortWithError(http.StatusInternalServerError, err)
return
}
}
mustDownload := !inUploads && !inDownloads
if mustDownload {
err = downloadStream(claim.SdHash, claimNameWithExt)
if err != nil {
@ -166,7 +170,7 @@ func view(c *gin.Context) {
_ = c.AbortWithError(http.StatusInternalServerError, errors.Err(err))
return
}
c.DataFromReader(http.StatusOK, f.Size(), claim.ContentType, reader, nil)
c.DataFromReader(http.StatusOK, f.Size(), contentType, reader, nil)
return
}