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

56
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,31 +94,7 @@ func view(c *gin.Context) {
channelShortID := "" channelShortID := ""
var claim *chainquery.Claim var claim *chainquery.Claim
var err error var err error
if strings.Contains(id, "@") { contentType := mime.TypeByExtension(filepath.Ext(claimNameWithExt))
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)
}
if err != nil {
if errors.Is(err, chainquery.ClaimNotFoundErr) {
_ = c.AbortWithError(http.StatusNotFound, err)
} else {
logrus.Errorln(errors.FullTrace(err))
_ = c.AbortWithError(http.StatusInternalServerError, err)
}
return
}
if !strings.Contains(claim.ContentType, "image/") {
c.Redirect(301, fmt.Sprintf("https://cdn.lbryplayer.xyz/content/claims/%s/%s/stream", claimName, id))
return
}
inUploads, err := isFileInDir(uploadsDir, claimNameWithExt) inUploads, err := isFileInDir(uploadsDir, claimNameWithExt)
if err != nil { if err != nil {
logrus.Errorln(errors.FullTrace(err)) logrus.Errorln(errors.FullTrace(err))
@ -134,6 +111,33 @@ func view(c *gin.Context) {
} }
} }
mustDownload := !inUploads && !inDownloads 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)
}
if err != nil {
if errors.Is(err, chainquery.ClaimNotFoundErr) {
_ = c.AbortWithError(http.StatusNotFound, err)
} else {
logrus.Errorln(errors.FullTrace(err))
_ = c.AbortWithError(http.StatusInternalServerError, err)
}
return
}
if !strings.Contains(claim.ContentType, "image/") {
c.Redirect(301, fmt.Sprintf("https://cdn.lbryplayer.xyz/content/claims/%s/%s/stream", claimName, id))
return
}
contentType = claim.ContentType
}
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
} }