fix get viewtoken

This commit is contained in:
Wim Brand
2020-02-21 14:23:29 +01:00
parent 4de17609bb
commit 9cfe675d16
3 changed files with 21 additions and 7 deletions

View File

@@ -3,6 +3,10 @@ package webcontroller
import (
"net/http"
"fornaxian.com/pixeldrain-api/util"
"fornaxian.com/pixeldrain-web/pixelapi"
"github.com/Fornaxian/log"
"github.com/julienschmidt/httprouter"
)
@@ -11,12 +15,17 @@ func (wc *WebController) serveAdClick(w http.ResponseWriter, r *http.Request, p
// Redirect the user to the target page
http.Redirect(w, r, r.URL.Query().Get("target"), http.StatusTemporaryRedirect)
// Get a view token
td := wc.newTemplateData(w, r)
vt := viewTokenOrBust(td.PixelAPI)
api := pixelapi.New(wc.apiURLInternal)
// Get the view token without authentication
vt := viewTokenOrBust(api)
// Apply authentication and register the view
api.APIKey, _ = wc.getAPIKey(r)
api.RealIP = util.RemoteAddress(r)
// Log a view on the file
if err := td.PixelAPI.PostFileView(p.ByName("id"), vt); err != nil {
if err := api.PostFileView(p.ByName("id"), vt); err != nil {
log.Warn("Failed to log view")
}
}