use separate pd client

This commit is contained in:
2020-06-07 21:12:48 +02:00
parent 843eaf51f9
commit 1a53def1ef
32 changed files with 185 additions and 692 deletions

View File

@@ -4,10 +4,8 @@ import (
"net/http"
"time"
"fornaxian.com/pixeldrain-api/api/apiclient"
"fornaxian.com/pixeldrain-api/util"
"fornaxian.com/pixeldrain-web/pixelapi"
"github.com/Fornaxian/log"
"github.com/julienschmidt/httprouter"
)
@@ -17,9 +15,7 @@ func (wc *WebController) serveAdClick(w http.ResponseWriter, r *http.Request, p
w.Header().Set("Referrer-Policy", "origin")
http.Redirect(w, r, r.URL.Query().Get("target"), http.StatusTemporaryRedirect)
// wc.templates.Get().ExecuteTemplate(w, "hide_refer", r.URL.Query().Get("target"))
api := pixelapi.New(wc.apiURLInternal)
api := apiclient.New(wc.apiURLInternal)
// The Real IP is used in the API server to determine that the view is not
// fake

View File

@@ -5,7 +5,7 @@ import (
"html/template"
"net/http"
"fornaxian.com/pixeldrain-web/pixelapi"
"fornaxian.com/pixeldrain-api/api/apiclient"
"github.com/Fornaxian/log"
)
@@ -13,7 +13,7 @@ func (wc *WebController) adminGlobalsForm(td *TemplateData, r *http.Request) (f
if isAdmin, err := td.PixelAPI.UserIsAdmin(); err != nil {
td.Title = err.Error()
return Form{Title: td.Title}
} else if !isAdmin.IsAdmin {
} else if !isAdmin {
td.Title = ";)"
return Form{Title: td.Title}
}
@@ -70,7 +70,7 @@ func (wc *WebController) adminGlobalsForm(td *TemplateData, r *http.Request) (f
// Value changed, try to update global setting
if err = td.PixelAPI.AdminSetGlobals(v.Name, v.EnteredValue); err != nil {
if apiErr, ok := err.(pixelapi.Error); ok {
if apiErr, ok := err.(apiclient.Error); ok {
f.SubmitMessages = append(f.SubmitMessages, template.HTML(apiErr.Message))
} else {
log.Error("%s", err)

View File

@@ -6,13 +6,11 @@ import (
"net/http"
"strings"
"fornaxian.com/pixeldrain-api/api/apiclient"
"fornaxian.com/pixeldrain-api/util"
"fornaxian.com/pixeldrain-web/pixelapi"
"github.com/Fornaxian/log"
"github.com/microcosm-cc/bluemonday"
"github.com/julienschmidt/httprouter"
"github.com/microcosm-cc/bluemonday"
"gopkg.in/russross/blackfriday.v2"
)
@@ -23,7 +21,7 @@ func (wc *WebController) serveFilePreview(w http.ResponseWriter, r *http.Request
return
}
api := pixelapi.New(wc.apiURLInternal)
api := apiclient.New(wc.apiURLInternal)
api.APIKey, _ = wc.getAPIKey(r)
api.RealIP = util.RemoteAddress(r)

View File

@@ -9,10 +9,10 @@ import (
"strings"
"time"
pdmimetype "github.com/Fornaxian/pd_mime_type"
"fornaxian.com/pixeldrain-web/pixelapi"
"fornaxian.com/pixeldrain-api/api/apiclient"
"fornaxian.com/pixeldrain-api/api/apitype"
"github.com/Fornaxian/log"
pdmimetype "github.com/Fornaxian/pd_mime_type"
"github.com/julienschmidt/httprouter"
)
@@ -47,6 +47,7 @@ type viewerData struct {
CaptchaKey string
ViewToken string
AdType int
ShowAds bool
APIResponse interface{}
}
@@ -62,13 +63,13 @@ func (wc *WebController) serveFileViewer(w http.ResponseWriter, r *http.Request,
templateData := wc.newTemplateData(w, r)
var finfo []pixelapi.ListFile
var finfo []apitype.ListFile
for _, id := range ids {
inf, err := templateData.PixelAPI.GetFileInfo(id)
if err != nil {
continue
}
finfo = append(finfo, pixelapi.ListFile{FileInfo: inf})
finfo = append(finfo, apitype.ListFile{FileInfo: inf})
}
if len(finfo) == 0 {
@@ -85,7 +86,8 @@ func (wc *WebController) serveFileViewer(w http.ResponseWriter, r *http.Request,
CaptchaKey: wc.captchaKey(),
ViewToken: wc.viewTokenOrBust(),
AdType: adType(),
APIResponse: pixelapi.List{
ShowAds: finfo[0].ShowAds,
APIResponse: apitype.ListInfo{
Success: true,
Title: "Multiple files",
DateCreated: time.Now(),
@@ -99,6 +101,7 @@ func (wc *WebController) serveFileViewer(w http.ResponseWriter, r *http.Request,
CaptchaKey: wc.captchaKey(),
ViewToken: wc.viewTokenOrBust(),
AdType: adType(),
ShowAds: finfo[0].ShowAds,
APIResponse: finfo[0].FileInfo,
}
}
@@ -123,6 +126,7 @@ func (wc *WebController) serveFileViewerDemo(w http.ResponseWriter, r *http.Requ
Type: "file",
CaptchaKey: wc.captchaSiteKey,
AdType: 0, // Always show a-ads on the demo page
ShowAds: true,
APIResponse: map[string]interface{}{
"id": "demo",
"name": "Demo file",
@@ -148,7 +152,7 @@ func (wc *WebController) serveListViewer(w http.ResponseWriter, r *http.Request,
var templateData = wc.newTemplateData(w, r)
var list, err = templateData.PixelAPI.GetList(p.ByName("id"))
if err != nil {
if err, ok := err.(pixelapi.Error); ok && err.Status == http.StatusNotFound {
if err, ok := err.(apiclient.Error); ok && err.Status == http.StatusNotFound {
w.WriteHeader(http.StatusNotFound)
wc.templates.Get().ExecuteTemplate(w, "list_not_found", templateData)
} else {
@@ -158,6 +162,10 @@ func (wc *WebController) serveListViewer(w http.ResponseWriter, r *http.Request,
}
return
}
if len(list.Files) == 0 {
w.WriteHeader(http.StatusNotFound)
wc.templates.Get().ExecuteTemplate(w, "list_not_found", templateData)
}
templateData.Title = fmt.Sprintf("%s ~ pixeldrain", list.Title)
templateData.OGData = metadataFromList(list)
@@ -166,6 +174,7 @@ func (wc *WebController) serveListViewer(w http.ResponseWriter, r *http.Request,
CaptchaKey: wc.captchaSiteKey,
ViewToken: wc.viewTokenOrBust(),
AdType: adType(),
ShowAds: list.Files[0].ShowAds,
APIResponse: list,
}
@@ -238,7 +247,7 @@ func (wc *WebController) serveSkynetViewer(w http.ResponseWriter, r *http.Reques
return
}
contentRange = strings.TrimPrefix(contentRange, "bytes ")
size, err := strconv.ParseUint(strings.Split(contentRange, "/")[1], 10, 64)
size, err := strconv.ParseInt(strings.Split(contentRange, "/")[1], 10, 64)
if err != nil {
panic(err)
}
@@ -256,19 +265,14 @@ func (wc *WebController) serveSkynetViewer(w http.ResponseWriter, r *http.Reques
templateData.Other = viewerData{
Type: "skylink",
AdType: adType(),
APIResponse: pixelapi.FileInfo{
Success: true,
ID: p.ByName("id"),
Name: name,
Size: size,
Views: 0,
BandwidthUsed: 0,
DateUpload: time.Now(),
DateLastView: time.Now(),
MimeType: fileType,
MimeImage: "",
ThumbnailHREF: "",
Availability: "",
APIResponse: apitype.FileInfo{
Success: true,
ID: p.ByName("id"),
Name: name,
Size: size,
DateUpload: time.Now(),
DateLastView: time.Now(),
MimeType: fileType,
},
}

View File

@@ -4,7 +4,7 @@ import (
"html/template"
"strings"
"fornaxian.com/pixeldrain-web/pixelapi"
"fornaxian.com/pixeldrain-api/api/apitype"
)
type ogRule struct {
@@ -34,7 +34,7 @@ func (o linkRule) HTML() template.HTML {
return template.HTML(`<link rel="` + o.Rel + `" href="` + o.HREF + `"/>` + "\n")
}
func metadataFromFile(f pixelapi.FileInfo) (meta template.HTML) {
func metadataFromFile(f apitype.FileInfo) (meta template.HTML) {
meta += ogRule{"og:title", f.Name}.HTML()
meta += ogRule{"og:site_name", "pixeldrain"}.HTML()
meta += ogRule{"og:description", "View '" + f.Name + "' on pixeldrain"}.HTML()
@@ -79,7 +79,7 @@ func metadataFromFile(f pixelapi.FileInfo) (meta template.HTML) {
}
return meta
}
func metadataFromList(l pixelapi.List) (meta template.HTML) {
func metadataFromList(l apitype.ListInfo) (meta template.HTML) {
meta += ogRule{"og:type", "website"}.HTML()
meta += ogRule{"og:title", l.Title}.HTML()
meta += ogRule{"og:site_name", "pixeldrain"}.HTML()

View File

@@ -14,8 +14,9 @@ import (
"strings"
"time"
"fornaxian.com/pixeldrain-api/api/apiclient"
"fornaxian.com/pixeldrain-api/api/apitype"
"fornaxian.com/pixeldrain-api/util"
"fornaxian.com/pixeldrain-web/pixelapi"
"github.com/Fornaxian/log"
)
@@ -23,13 +24,12 @@ import (
// the field Other you can pass your own template-specific variables.
type TemplateData struct {
Authenticated bool
Username string
Email string
User apitype.UserInfo
UserAgent string
Style pixeldrainStyleSheet
UserStyle template.CSS
APIEndpoint template.URL
PixelAPI *pixelapi.PixelAPI
PixelAPI *apiclient.PixelAPI
Hostname template.HTML
// Only used on file viewer page
@@ -46,12 +46,11 @@ type TemplateData struct {
func (wc *WebController) newTemplateData(w http.ResponseWriter, r *http.Request) (t *TemplateData) {
t = &TemplateData{
Authenticated: false,
Username: "",
UserAgent: r.UserAgent(),
Style: userStyle(r),
UserStyle: template.CSS(userStyle(r).String()),
APIEndpoint: template.URL(wc.apiURLExternal),
PixelAPI: pixelapi.New(wc.apiURLInternal),
PixelAPI: apiclient.New(wc.apiURLInternal),
Hostname: template.HTML(wc.hostname),
URLQuery: r.URL.Query(),
}
@@ -64,7 +63,7 @@ func (wc *WebController) newTemplateData(w http.ResponseWriter, r *http.Request)
// and stuff like that
if key, err := wc.getAPIKey(r); err == nil {
t.PixelAPI.APIKey = key // Use the user's API key for all requests
uinf, err := t.PixelAPI.UserInfo()
t.User, err = t.PixelAPI.UserInfo()
if err != nil {
// This session key doesn't work, or the backend is down, user
// cannot be authenticated
@@ -96,8 +95,6 @@ func (wc *WebController) newTemplateData(w http.ResponseWriter, r *http.Request)
// Authentication succeeded
t.Authenticated = true
t.Username = uinf.Username
t.Email = uinf.Email
}
return t
@@ -245,7 +242,7 @@ func (tm *TemplateManager) sub(a, b interface{}) int {
return detectInt(a) - detectInt(b)
}
func (tm *TemplateManager) formatData(i interface{}) string {
return util.FormatData(uint64(detectInt(i)))
return util.FormatData(int64(detectInt(i)))
}
func detectInt(i interface{}) int {

View File

@@ -5,7 +5,7 @@ import (
"net/http"
"time"
"fornaxian.com/pixeldrain-web/pixelapi"
"fornaxian.com/pixeldrain-api/api/apiclient"
"github.com/Fornaxian/log"
"github.com/julienschmidt/httprouter"
)
@@ -16,7 +16,7 @@ func (wc *WebController) serveLogout(
p httprouter.Params,
) {
if key, err := wc.getAPIKey(r); err == nil {
var api = pixelapi.New(wc.apiURLInternal)
var api = apiclient.New(wc.apiURLInternal)
api.APIKey = key
if err = api.UserSessionDestroy(key); err != nil {
log.Warn("logout failed for session '%s': %s", key, err)

View File

@@ -5,7 +5,7 @@ import (
"html/template"
"net/http"
"fornaxian.com/pixeldrain-web/pixelapi"
"fornaxian.com/pixeldrain-api/api/apiclient"
"github.com/Fornaxian/log"
"github.com/julienschmidt/httprouter"
)
@@ -23,7 +23,7 @@ func formAPIError(err error, f *Form) {
return name
}
if err, ok := err.(pixelapi.Error); ok {
if err, ok := err.(apiclient.Error); ok {
if err.StatusCode == "multiple_errors" {
for _, err := range err.Errors {
// Modify the message to make it more user-friendly
@@ -169,7 +169,7 @@ func (wc *WebController) serveEmailConfirm(
var err error
var status string
api := pixelapi.New(wc.apiURLInternal)
api := apiclient.New(wc.apiURLInternal)
err = api.UserEmailResetConfirm(r.FormValue("key"))
if err != nil && err.Error() == "not_found" {
status = "not_found"

View File

@@ -8,10 +8,9 @@ import (
"strings"
"time"
"github.com/google/uuid"
"fornaxian.com/pixeldrain-web/pixelapi"
"fornaxian.com/pixeldrain-api/api/apiclient"
"github.com/Fornaxian/log"
"github.com/google/uuid"
"github.com/julienschmidt/httprouter"
)
@@ -36,7 +35,7 @@ type WebController struct {
// This API client should only be used for system functions like getting
// view tokens. It has no authentication and no IP forwarding
systemPixelAPI *pixelapi.PixelAPI
systemPixelAPI *apiclient.PixelAPI
}
// New initializes a new WebController by registering all the request handlers
@@ -58,7 +57,7 @@ func New(
apiURLExternal: apiURLExternal,
sessionCookieDomain: sessionCookieDomain,
httpClient: &http.Client{Timeout: time.Minute * 10},
systemPixelAPI: pixelapi.New(apiURLInternal),
systemPixelAPI: apiclient.New(apiURLInternal),
}
wc.templates = NewTemplateManager(resourceDir, apiURLExternal, debugMode)
wc.templates.ParseTemplates(false)
@@ -188,7 +187,7 @@ func (wc *WebController) serveForm(
// The handler retuns the form which will be rendered
td.Form = handler(td, r)
td.Form.Username = td.Username
td.Form.Username = td.User.Username
// Execute the extra actions if any
if td.Form.Extra.SetCookie != nil {
@@ -249,7 +248,7 @@ func (wc *WebController) getAPIKey(r *http.Request) (key string, err error) {
func (wc *WebController) captchaKey() string {
// This only runs on the first request
if wc.captchaSiteKey == "" {
var api = pixelapi.New(wc.apiURLInternal)
var api = apiclient.New(wc.apiURLInternal)
capt, err := api.GetRecaptcha()
if err != nil {
log.Error("Error getting recaptcha key: %s", err)