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" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"mime"
"net/http" "net/http"
"os" "os"
"os/user" "os/user"
@ -93,13 +94,30 @@ func view(c *gin.Context) {
channelShortID := "" channelShortID := ""
var claim *chainquery.Claim var claim *chainquery.Claim
var err error 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, "@") { if strings.Contains(id, "@") {
parts := strings.Split(id, ":") parts := strings.Split(id, ":")
channelName = parts[0] channelName = parts[0]
if len(parts) > 1 { if len(parts) > 1 {
channelShortID = parts[1] channelShortID = parts[1]
} }
claim, err = cqApi.ResolveClaimByChannel(claimName, channelShortID, channelName) claim, err = cqApi.ResolveClaimByChannel(claimName, channelShortID, channelName)
} else { } else {
claim, err = cqApi.ResolveClaim(claimName, id) 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)) c.Redirect(301, fmt.Sprintf("https://cdn.lbryplayer.xyz/content/claims/%s/%s/stream", claimName, id))
return 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 { if mustDownload {
err = downloadStream(claim.SdHash, claimNameWithExt) err = downloadStream(claim.SdHash, claimNameWithExt)
if err != nil { if err != nil {
@ -166,7 +170,7 @@ func view(c *gin.Context) {
_ = c.AbortWithError(http.StatusInternalServerError, errors.Err(err)) _ = c.AbortWithError(http.StatusInternalServerError, errors.Err(err))
return return
} }
c.DataFromReader(http.StatusOK, f.Size(), claim.ContentType, reader, nil) c.DataFromReader(http.StatusOK, f.Size(), contentType, reader, nil)
return return
} }