Add apps page

This commit is contained in:
2021-01-12 14:07:55 +01:00
parent 4dd819d49f
commit ad03e0758b
13 changed files with 298 additions and 79 deletions

61
webcontroller/misc.go Normal file
View File

@@ -0,0 +1,61 @@
package webcontroller
import (
"encoding/base64"
"fmt"
"net/http"
"github.com/Fornaxian/log"
"github.com/julienschmidt/httprouter"
)
// ServeFileViewerDemo is a dummy API response that responds with info about a
// non-existent demo file. This is required by the a-ads ad network to allow for
// automatic checking of the presence of the ad unit on this page.
func (wc *WebController) serveShareXConfig(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
templateData := wc.newTemplateData(w, r)
w.Header().Add("Content-Disposition", "attachment; filename=pixeldrain.com.sxcu")
if templateData.Authenticated {
sess, err := templateData.PixelAPI.UserSessionCreate()
if err != nil {
log.Error("Failed to create user session: %s", err)
wc.templates.Get().ExecuteTemplate(w, "500", templateData)
return
}
w.Write([]byte(fmt.Sprintf(
`{
"Version": "12.4.1",
"DestinationType": "ImageUploader, TextUploader, FileUploader",
"RequestMethod": "POST",
"RequestURL": "https://pixeldrain.com/api/file",
"Headers": {
"Authorization": "Basic %s"
},
"Body": "MultipartFormData",
"FileFormName": "file",
"URL": "https://pixeldrain.com/u/$json:id$",
"ThumbnailURL": "https://pixeldrain.com/api/file/$json:id$/thumbnail"
}
`,
base64.StdEncoding.EncodeToString([]byte(
templateData.User.Username+":"+sess.AuthKey.String(),
)),
)))
} else {
w.Write([]byte(fmt.Sprintf(
`{
"Version": "12.4.1",
"DestinationType": "ImageUploader, TextUploader, FileUploader",
"RequestMethod": "POST",
"RequestURL": "https://pixeldrain.com/api/file",
"Body": "MultipartFormData",
"FileFormName": "file",
"URL": "https://pixeldrain.com/u/$json:id$",
"ThumbnailURL": "https://pixeldrain.com/api/file/$json:id$/thumbnail"
}
`,
)))
}
}

View File

@@ -71,7 +71,7 @@ func (wc *WebController) newTemplateData(w http.ResponseWriter, r *http.Request)
if err.Error() == "authentication_required" || err.Error() == "authentication_failed" {
// Disable API authentication
t.PixelAPI = wc.api
t.PixelAPI = wc.api.RealIP(util.RemoteAddress(r))
// Remove the authentication cookie
log.Debug("Deleting invalid API key")

View File

@@ -148,8 +148,10 @@ func (wc *WebController) loginForm(td *TemplateData, r *http.Request) (f Form) {
}
if f.ReadInput(r) {
loginResp, err := td.PixelAPI.UserLogin(f.FieldVal("username"), f.FieldVal("password"))
if err != nil {
if session, err := td.PixelAPI.UserLogin(
f.FieldVal("username"),
f.FieldVal("password"),
); err != nil {
formAPIError(err, &f)
} else {
// Request was a success
@@ -159,7 +161,7 @@ func (wc *WebController) loginForm(td *TemplateData, r *http.Request) (f Form) {
// Set the autentication cookie
f.Extra.SetCookie = &http.Cookie{
Name: "pd_auth_key",
Value: loginResp.APIKey,
Value: session.AuthKey.String(),
Path: "/",
Expires: time.Now().AddDate(50, 0, 0),
Domain: wc.sessionCookieDomain,

View File

@@ -123,6 +123,7 @@ func New(
{GET, "acknowledgements" /**/, wc.serveMarkdown("acknowledgements.md", false)},
{GET, "business" /* */, wc.serveMarkdown("business.md", false)},
{GET, "server_status" /* */, wc.serveTemplate("server_status", false)},
{GET, "apps" /* */, wc.serveTemplate("apps", false)},
// User account pages
{GET, "register" /* */, wc.serveForm(wc.registerForm, false)},
@@ -164,6 +165,9 @@ func New(
{GET, "click/:id" /* */, wc.serveAdClick},
{GET, "campaign/:id" /* */, wc.serveCampaignPartner},
{GET, "ad/revenuehits" /**/, wc.serveTemplate("revenuehits", false)},
// Misc
{GET, "misc/sharex/pixeldrain.com.sxcu", wc.serveShareXConfig},
} {
r.Handle(h.method, prefix+"/"+h.path, h.handler)
}