bunch of API changes
This commit is contained in:
@@ -1,16 +0,0 @@
|
||||
FROM ubuntu
|
||||
|
||||
RUN mkdir -p \
|
||||
/bin \
|
||||
/opt/pixeldrain/res
|
||||
|
||||
COPY docker/pixeldrain-web /bin/pixeldrain-web
|
||||
COPY res /opt/pixeldrain/res
|
||||
|
||||
EXPOSE 8081
|
||||
|
||||
ENV api_url_external="" \
|
||||
api_url_internal="" \
|
||||
debug_mode=""
|
||||
|
||||
ENTRYPOINT [ "/bin/pixeldrain-web" ]
|
@@ -1,55 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"fornaxian.com/pixeldrain-web/init/conf"
|
||||
"fornaxian.com/pixeldrain-web/webcontroller"
|
||||
|
||||
"github.com/Fornaxian/log"
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
// This is just a launcher for the web server. During testing the app would
|
||||
// be directly embedded by another Go project. And when deployed it will run
|
||||
// independently.
|
||||
func main() {
|
||||
log.Info("Starting web UI server in docker mode (PID %v)", os.Getpid())
|
||||
|
||||
var webconf = &conf.PixelWebConfig{
|
||||
StaticResourceDir: "/opt/pixeldrain/res/static",
|
||||
TemplateDir: "/opt/pixeldrain/res/template",
|
||||
}
|
||||
|
||||
if webconf.APIURLExternal, _ = os.LookupEnv("api_url_external"); webconf.APIURLExternal == "" {
|
||||
webconf.APIURLExternal = "/api"
|
||||
}
|
||||
if webconf.APIURLInternal, _ = os.LookupEnv("api_url_internal"); webconf.APIURLInternal == "" {
|
||||
panic("internal API URL is required. Please set api_url_internal")
|
||||
}
|
||||
|
||||
// Check debug mode. When debug mode is enabled debug logging will be
|
||||
// enabled, templates will be parsed on every request and analytics tracking
|
||||
// will be disabled
|
||||
switch d, _ := os.LookupEnv("debug_mode"); d {
|
||||
case "1", "yes", "y", "true":
|
||||
webconf.DebugMode = true
|
||||
log.SetLogLevel(log.LevelDebug)
|
||||
default:
|
||||
webconf.DebugMode = false
|
||||
log.SetLogLevel(log.LevelInfo)
|
||||
}
|
||||
|
||||
// Web path prefix
|
||||
prefix, _ := os.LookupEnv("api_path_prefix")
|
||||
|
||||
// Init the http router
|
||||
r := httprouter.New()
|
||||
webcontroller.New(r, prefix, webconf)
|
||||
|
||||
err := http.ListenAndServe(":8081", r)
|
||||
if err != nil {
|
||||
log.Error("Can't listen and serve Pixeldrain Web: %v", err)
|
||||
}
|
||||
}
|
@@ -10,7 +10,7 @@ type IsAdmin struct {
|
||||
|
||||
// UserIsAdmin returns if the logged in user is an admin user
|
||||
func (p *PixelAPI) UserIsAdmin() (resp IsAdmin, err error) {
|
||||
err = p.jsonRequest("GET", p.apiEndpoint+"/admin/is_admin", &resp)
|
||||
err = p.jsonRequest("GET", p.apiEndpoint+"/admin/is_admin", &resp, false)
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
@@ -31,16 +31,16 @@ type AdminGlobals struct {
|
||||
|
||||
// AdminGetGlobals returns if the logged in user is an admin user
|
||||
func (p *PixelAPI) AdminGetGlobals() (resp AdminGlobals, err error) {
|
||||
if err = p.jsonRequest("GET", p.apiEndpoint+"/admin/globals", &resp); err != nil {
|
||||
if err = p.jsonRequest("GET", p.apiEndpoint+"/admin/globals", &resp, false); err != nil {
|
||||
return resp, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// AdminSetGlobals returns if the logged in user is an admin user
|
||||
func (p *PixelAPI) AdminSetGlobals(key, value string) (resp SuccessResponse, err error) {
|
||||
func (p *PixelAPI) AdminSetGlobals(key, value string) (err error) {
|
||||
var form = url.Values{}
|
||||
form.Add("key", key)
|
||||
form.Add("value", value)
|
||||
return resp, p.form("POST", p.apiEndpoint+"/admin/globals", form, &resp, true)
|
||||
return p.form("POST", p.apiEndpoint+"/admin/globals", form, nil, true)
|
||||
}
|
||||
|
@@ -35,9 +35,10 @@ type FileInfo struct {
|
||||
|
||||
// GetFileInfo gets the FileInfo from the pixeldrain API
|
||||
func (p *PixelAPI) GetFileInfo(id string) (resp FileInfo, err error) {
|
||||
return resp, p.jsonRequest("GET", p.apiEndpoint+"/file/"+id+"/info", &resp)
|
||||
return resp, p.jsonRequest("GET", p.apiEndpoint+"/file/"+id+"/info", &resp, false)
|
||||
}
|
||||
|
||||
// PostFileView adds a view to a file
|
||||
func (p *PixelAPI) PostFileView(id, viewtoken string) (err error) {
|
||||
vals := url.Values{}
|
||||
vals.Set("token", viewtoken)
|
||||
|
@@ -27,5 +27,5 @@ type ListFile struct {
|
||||
// GetList get a List from the pixeldrain API. Errors will be available through
|
||||
// List.Error. Standard error checks apply.
|
||||
func (p *PixelAPI) GetList(id string) (resp List, err error) {
|
||||
return resp, p.jsonRequest("GET", p.apiEndpoint+"/list/"+id, &resp)
|
||||
return resp, p.jsonRequest("GET", p.apiEndpoint+"/list/"+id, &resp, false)
|
||||
}
|
||||
|
@@ -8,12 +8,12 @@ type Recaptcha struct {
|
||||
// GetRecaptcha gets the reCaptcha site key from the pixelapi server. If
|
||||
// reCaptcha is disabled the key will be empty
|
||||
func (p *PixelAPI) GetRecaptcha() (resp Recaptcha, err error) {
|
||||
return resp, p.jsonRequest("GET", p.apiEndpoint+"/misc/recaptcha", &resp)
|
||||
return resp, p.jsonRequest("GET", p.apiEndpoint+"/misc/recaptcha", &resp, false)
|
||||
}
|
||||
|
||||
// GetMiscViewToken requests a viewtoken from the server. The viewtoken is valid
|
||||
// for a limited amount of time and can be used to add views to a file.
|
||||
// Viewtokens can only be requested from localhost
|
||||
func (p *PixelAPI) GetMiscViewToken() (resp string, err error) {
|
||||
return resp, p.jsonRequest("GET", p.apiEndpoint+"/misc/viewtoken", &resp)
|
||||
return resp, p.jsonRequest("GET", p.apiEndpoint+"/misc/viewtoken", &resp, false)
|
||||
}
|
||||
|
@@ -26,59 +26,49 @@ func New(apiEndpoint string) *PixelAPI {
|
||||
return &PixelAPI{apiEndpoint: apiEndpoint}
|
||||
}
|
||||
|
||||
// Error is either an error that occurred during the API request
|
||||
// (ReqError = true), or an error that was returned from the Pixeldrain API
|
||||
// (ReqError = false). The Success field is also returned by the Pixeldrain API,
|
||||
// but since this is struct represents an Error it will usually be false.
|
||||
//
|
||||
// When ReqError is true the Value field will contain a Go error message. When
|
||||
// it's false it will contain a Pixeldrain API error code.
|
||||
// Standard response types
|
||||
|
||||
// Error is an error returned by the pixeldrain API. If the request failed
|
||||
// before it could reach the API the error will be on a different type
|
||||
type Error struct {
|
||||
ReqError bool
|
||||
Success bool `json:"success"`
|
||||
Value string `json:"value"`
|
||||
Message string `json:"message"`
|
||||
Extra interface{} `json:"extra,omitempty"`
|
||||
Status int `json:"-"` // One of the http.Status types
|
||||
Success bool `json:"success"`
|
||||
StatusCode string `json:"value"`
|
||||
Message string `json:"message"`
|
||||
|
||||
// In case of the multiple_errors code this array will be populated with
|
||||
// more errors
|
||||
Errors []Error `json:"errors,omitempty"`
|
||||
|
||||
// Metadata regarding the error
|
||||
Extra map[string]interface{} `json:"extra,omitempty"`
|
||||
}
|
||||
|
||||
func (e Error) Error() string { return e.Value }
|
||||
func (e Error) Error() string { return e.StatusCode }
|
||||
|
||||
// SuccessResponse is a generic response the API returns when the action was
|
||||
// successful and there is nothing interesting to report
|
||||
type SuccessResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func (p *PixelAPI) jsonRequest(method, url string, target interface{}) error {
|
||||
req, err := http.NewRequest(method, url, nil)
|
||||
if err != nil {
|
||||
return Error{
|
||||
ReqError: true,
|
||||
Success: false,
|
||||
Value: err.Error(),
|
||||
Message: err.Error(),
|
||||
}
|
||||
}
|
||||
func (p *PixelAPI) do(r *http.Request) (*http.Response, error) {
|
||||
if p.APIKey != "" {
|
||||
req.SetBasicAuth("", p.APIKey)
|
||||
r.SetBasicAuth("", p.APIKey)
|
||||
}
|
||||
if p.RealIP != "" {
|
||||
req.Header.Set("X-Real-IP", p.RealIP)
|
||||
r.Header.Set("X-Real-IP", p.RealIP)
|
||||
}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
return client.Do(r)
|
||||
}
|
||||
|
||||
func (p *PixelAPI) jsonRequest(method, url string, target interface{}, multiErr bool) error {
|
||||
req, err := http.NewRequest(method, url, nil)
|
||||
if err != nil {
|
||||
return Error{
|
||||
ReqError: true,
|
||||
Success: false,
|
||||
Value: err.Error(),
|
||||
Message: err.Error(),
|
||||
}
|
||||
return err
|
||||
}
|
||||
resp, err := p.do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
return parseJSONResponse(resp, target, true)
|
||||
return parseJSONResponse(resp, target, multiErr)
|
||||
}
|
||||
|
||||
func (p *PixelAPI) getString(url string) (string, error) {
|
||||
@@ -86,14 +76,7 @@ func (p *PixelAPI) getString(url string) (string, error) {
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if p.APIKey != "" {
|
||||
req.SetBasicAuth("", p.APIKey)
|
||||
}
|
||||
if p.RealIP != "" {
|
||||
req.Header.Set("X-Real-IP", p.RealIP)
|
||||
}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
resp, err := p.do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -110,14 +93,7 @@ func (p *PixelAPI) getRaw(url string) (io.ReadCloser, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if p.APIKey != "" {
|
||||
req.SetBasicAuth("", p.APIKey)
|
||||
}
|
||||
if p.RealIP != "" {
|
||||
req.Header.Set("X-Real-IP", p.RealIP)
|
||||
}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
resp, err := p.do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -130,56 +106,29 @@ func (p *PixelAPI) form(
|
||||
url string,
|
||||
vals url.Values,
|
||||
target interface{},
|
||||
catchErrors bool,
|
||||
multiErr bool,
|
||||
) error {
|
||||
req, err := http.NewRequest(method, url, strings.NewReader(vals.Encode()))
|
||||
if err != nil {
|
||||
return Error{
|
||||
ReqError: true,
|
||||
Success: false,
|
||||
Value: err.Error(),
|
||||
Message: err.Error(),
|
||||
}
|
||||
return err
|
||||
}
|
||||
if p.APIKey != "" {
|
||||
req.SetBasicAuth("", p.APIKey)
|
||||
}
|
||||
if p.RealIP != "" {
|
||||
req.Header.Set("X-Real-IP", p.RealIP)
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
resp, err := p.do(req)
|
||||
if err != nil {
|
||||
return Error{
|
||||
ReqError: true,
|
||||
Success: false,
|
||||
Value: err.Error(),
|
||||
Message: err.Error(),
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
return parseJSONResponse(resp, target, catchErrors)
|
||||
return parseJSONResponse(resp, target, multiErr)
|
||||
}
|
||||
|
||||
func parseJSONResponse(resp *http.Response, target interface{}, catchErrors bool) error {
|
||||
var err error
|
||||
|
||||
func parseJSONResponse(resp *http.Response, target interface{}, multiErr bool) (err error) {
|
||||
// Test for client side and server side errors
|
||||
if catchErrors && resp.StatusCode >= 400 {
|
||||
var errResp = Error{
|
||||
ReqError: false,
|
||||
}
|
||||
if resp.StatusCode >= 400 {
|
||||
errResp := Error{Status: resp.StatusCode}
|
||||
if err = json.NewDecoder(resp.Body).Decode(&errResp); err != nil {
|
||||
log.Error("Can't decode this: %v", err)
|
||||
return Error{
|
||||
ReqError: true,
|
||||
Success: false,
|
||||
Value: err.Error(),
|
||||
Message: err.Error(),
|
||||
}
|
||||
return err
|
||||
}
|
||||
return errResp
|
||||
}
|
||||
@@ -191,12 +140,8 @@ func parseJSONResponse(resp *http.Response, target interface{}, catchErrors bool
|
||||
if err = json.NewDecoder(resp.Body).Decode(target); err != nil {
|
||||
r, _ := ioutil.ReadAll(resp.Body)
|
||||
log.Error("Can't decode this: %v. %s", err, r)
|
||||
return Error{
|
||||
ReqError: true,
|
||||
Success: false,
|
||||
Value: err.Error(),
|
||||
Message: err.Error(),
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@@ -6,15 +6,6 @@ import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Registration is the response to the UserRegister API. The register API can
|
||||
// return multiple errors, which will be stored in the Errors array. Check for
|
||||
// len(Errors) == 0 to see if an error occurred
|
||||
type Registration struct {
|
||||
Success bool `json:"success"`
|
||||
Message string `json:"message,omitempty"`
|
||||
Errors []Error `json:"errors,omitempty"`
|
||||
}
|
||||
|
||||
// UserRegister registers a new user on the Pixeldrain server. username and
|
||||
// password are always required. email is optional, but without it you will not
|
||||
// be able to reset your password in case you forget it. captcha depends on
|
||||
@@ -24,18 +15,17 @@ type Registration struct {
|
||||
// The register API can return multiple errors, which will be stored in the
|
||||
// Errors array. Check for len(Errors) == 0 to see if an error occurred. If err
|
||||
// != nil it means a connection error occurred
|
||||
func (p *PixelAPI) UserRegister(username, email, password, captcha string) (resp *Registration, err error) {
|
||||
resp = &Registration{}
|
||||
var form = url.Values{}
|
||||
form.Add("username", username)
|
||||
form.Add("email", email)
|
||||
form.Add("password", password)
|
||||
form.Add("recaptcha_response", captcha)
|
||||
err = p.form("POST", p.apiEndpoint+"/user/register", form, resp, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
func (p *PixelAPI) UserRegister(username, email, password, captcha string) (err error) {
|
||||
return p.form(
|
||||
"POST", p.apiEndpoint+"/user/register",
|
||||
url.Values{
|
||||
"username": {username},
|
||||
"email": {email},
|
||||
"password": {password},
|
||||
"recaptcha_response": {captcha},
|
||||
},
|
||||
nil, true,
|
||||
)
|
||||
}
|
||||
|
||||
// Login is the success response to the `user/login` API
|
||||
@@ -48,14 +38,12 @@ type Login struct {
|
||||
// contain the returned API key. If saveKey is true the API key will also be
|
||||
// saved in the client and following requests with this client will be
|
||||
// autenticated
|
||||
func (p *PixelAPI) UserLogin(username, password string, saveKey bool) (resp *Login, err error) {
|
||||
resp = &Login{}
|
||||
func (p *PixelAPI) UserLogin(username, password string, saveKey bool) (resp Login, err error) {
|
||||
var form = url.Values{}
|
||||
form.Add("username", username)
|
||||
form.Add("password", password)
|
||||
err = p.form("POST", p.apiEndpoint+"/user/login", form, resp, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if err = p.form("POST", p.apiEndpoint+"/user/login", form, &resp, true); err != nil {
|
||||
return resp, err
|
||||
}
|
||||
if saveKey {
|
||||
p.APIKey = resp.APIKey
|
||||
@@ -71,61 +59,48 @@ type UserInfo struct {
|
||||
}
|
||||
|
||||
// UserInfo returns information about the logged in user. Requires an API key
|
||||
func (p *PixelAPI) UserInfo() (resp *UserInfo, err error) {
|
||||
resp = &UserInfo{}
|
||||
err = p.jsonRequest("GET", p.apiEndpoint+"/user", resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
func (p *PixelAPI) UserInfo() (resp UserInfo, err error) {
|
||||
return resp, p.jsonRequest("GET", p.apiEndpoint+"/user", &resp, false)
|
||||
}
|
||||
|
||||
// UserSessionDestroy destroys an API key so it can no longer be used to perform
|
||||
// actions
|
||||
func (p *PixelAPI) UserSessionDestroy(key string) (err error) {
|
||||
return p.jsonRequest("DELETE", p.apiEndpoint+"/user/session", nil)
|
||||
return p.jsonRequest("DELETE", p.apiEndpoint+"/user/session", nil, false)
|
||||
}
|
||||
|
||||
// UserFiles is a list of files uploaded by a user
|
||||
type UserFiles struct {
|
||||
Success bool `json:"success"`
|
||||
Files []FileInfo `json:"files"`
|
||||
}
|
||||
|
||||
func (p *PixelAPI) UserFiles(page, limit int) (resp *UserFiles, err error) {
|
||||
resp = &UserFiles{Files: make([]FileInfo, 0)}
|
||||
err = p.jsonRequest(
|
||||
"GET",
|
||||
fmt.Sprintf("%s/user/files?page=%d&limit=%d", p.apiEndpoint, page, limit),
|
||||
resp,
|
||||
// UserFiles gets files uploaded by a user
|
||||
func (p *PixelAPI) UserFiles(page, limit int) (resp UserFiles, err error) {
|
||||
return resp, p.jsonRequest(
|
||||
"GET", fmt.Sprintf("%s/user/files?page=%d&limit=%d", p.apiEndpoint, page, limit), &resp, false,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// UserLists is a list of lists created by a user
|
||||
type UserLists struct {
|
||||
Success bool `json:"success"`
|
||||
Lists []List `json:"lists"`
|
||||
}
|
||||
|
||||
func (p *PixelAPI) UserLists(page, limit int) (resp *UserLists, err error) {
|
||||
resp = &UserLists{Lists: make([]List, 0)}
|
||||
if err = p.jsonRequest(
|
||||
"GET",
|
||||
fmt.Sprintf("%s/user/lists?page=%d&limit=%d", p.apiEndpoint, page, limit),
|
||||
resp,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
// UserLists gets lists created by a user
|
||||
func (p *PixelAPI) UserLists(page, limit int) (resp UserLists, err error) {
|
||||
return resp, p.jsonRequest(
|
||||
"GET", fmt.Sprintf("%s/user/lists?page=%d&limit=%d", p.apiEndpoint, page, limit), &resp, false,
|
||||
)
|
||||
}
|
||||
|
||||
// UserPasswordSet changes the user's password
|
||||
func (p *PixelAPI) UserPasswordSet(oldPW, newPW string) (err error) {
|
||||
var form = url.Values{}
|
||||
form.Add("old_password", oldPW)
|
||||
form.Add("new_password", newPW)
|
||||
return p.form("PUT", p.apiEndpoint+"/user/password", form, nil, true)
|
||||
return p.form(
|
||||
"PUT", p.apiEndpoint+"/user/password",
|
||||
url.Values{"old_password": {oldPW}, "new_password": {newPW}}, nil, true,
|
||||
)
|
||||
}
|
||||
|
||||
// UserEmailReset starts the e-mail change process. An email will be sent to the
|
||||
@@ -141,9 +116,10 @@ func (p *PixelAPI) UserEmailReset(email string, delete bool) (err error) {
|
||||
|
||||
// UserEmailResetConfirm finishes process of changing a user's e-mail address
|
||||
func (p *PixelAPI) UserEmailResetConfirm(key string) (err error) {
|
||||
var form = url.Values{}
|
||||
form.Add("key", key)
|
||||
return p.form("PUT", p.apiEndpoint+"/user/email_reset_cofirm", form, nil, true)
|
||||
return p.form(
|
||||
"PUT", p.apiEndpoint+"/user/email_reset_confirm",
|
||||
url.Values{"key": {key}}, nil, true,
|
||||
)
|
||||
}
|
||||
|
||||
// UserPasswordReset starts the password reset process. An email will be sent
|
||||
|
45
res/include/script/form.vue
Normal file
45
res/include/script/form.vue
Normal file
@@ -0,0 +1,45 @@
|
||||
Vue.component("vform", {
|
||||
template: `
|
||||
<h2>TEST FORM</h2>
|
||||
<form class="highlight_dark" method="POST">
|
||||
<table class="form">
|
||||
<tbody>
|
||||
<tr class="form">
|
||||
<td>Old Password</td>
|
||||
<td>
|
||||
<input id="input_old_password" name="old_password" value="" type="password" autocomplete="current-password" class="form_input">
|
||||
</td>
|
||||
</tr>
|
||||
<tr class="form"></tr>
|
||||
<td>New Password</td>
|
||||
<td>
|
||||
<input id="input_new_password1" name="new_password1" value="" type="password" autocomplete="new-password" class="form_input">
|
||||
</td>
|
||||
</tr>
|
||||
<tr class="form">
|
||||
<td>New Password again</td>
|
||||
<td>
|
||||
<input id="input_new_password2" name="new_password2" value="" type="password" autocomplete="new-password" class="form_input">
|
||||
</td>
|
||||
</tr>
|
||||
<tr class="form">
|
||||
<td colspan="2">
|
||||
we need you to repeat your password so you won't be locked out of your account if you make a typing error
|
||||
</td>
|
||||
</tr>
|
||||
<tr class="form">
|
||||
<td colspan="2" style="text-align: right;">
|
||||
<input type="submit" value="Submit" class="button_highlight" style="float: right;">
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</form>
|
||||
`
|
||||
})
|
||||
|
||||
Vue.component("form_field", {
|
||||
template: `
|
||||
|
||||
`
|
||||
})
|
3
res/include/script/user_settings.vue
Normal file
3
res/include/script/user_settings.vue
Normal file
@@ -0,0 +1,3 @@
|
||||
const appTestForm = new Vue({
|
||||
el: "#test_form"
|
||||
})
|
11965
res/static/script/vue.js
Normal file
11965
res/static/script/vue.js
Normal file
File diff suppressed because it is too large
Load Diff
6
res/static/script/vue.min.js
vendored
Normal file
6
res/static/script/vue.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
@@ -49,15 +49,21 @@
|
||||
|
||||
let breadcrumbs = document.querySelector("#nav_bar > .breadcrumbs")
|
||||
|
||||
if (window.location.href.endsWith("?files")) {
|
||||
breadcrumbs.value = "/{{.Username}}/Files"
|
||||
fm.getUserFiles()
|
||||
} else if (window.location.href.endsWith("?lists")) {
|
||||
breadcrumbs.value = "/{{.Username}}/Lists"
|
||||
fm.getUserLists()
|
||||
} else {
|
||||
alert("invalid file manager type")
|
||||
let hashChange = () => {
|
||||
if (window.location.hash === "#files") {
|
||||
breadcrumbs.value = "/{{.Username}}/Files"
|
||||
fm.getUserFiles()
|
||||
} else if (window.location.hash === "#lists") {
|
||||
breadcrumbs.value = "/{{.Username}}/Lists"
|
||||
fm.getUserLists()
|
||||
} else {
|
||||
alert("invalid file manager type")
|
||||
}
|
||||
}
|
||||
|
||||
hashChange()
|
||||
|
||||
window.addEventListener("hashchange", hashChange)
|
||||
})
|
||||
</script>
|
||||
{{template "analytics"}}
|
||||
|
57
res/template/account/user_settings_vue_test.html
Normal file
57
res/template/account/user_settings_vue_test.html
Normal file
@@ -0,0 +1,57 @@
|
||||
{{define "user_settings_vue_test"}}<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
{{template "meta_tags" .Username}}
|
||||
{{template "user_style" .}}
|
||||
<script>var apiEndpoint = '{{.APIEndpoint}}';</script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
{{template "page_top" .}}
|
||||
<h1>{{.Title}}</h1>
|
||||
<div class="page_content"><div class="limit_width">
|
||||
<h2>TEST FORM</h2>
|
||||
<form id="test_form" class="highlight_dark" method="POST">
|
||||
<table class="form">
|
||||
<tr class="form">
|
||||
<td>Old Password</td>
|
||||
<td>
|
||||
<input id="input_old_password" name="old_password" value="" type="password" autocomplete="current-password" class="form_input">
|
||||
</td>
|
||||
</tr>
|
||||
<tr class="form"></tr>
|
||||
<td>New Password</td>
|
||||
<td>
|
||||
<input id="input_new_password1" name="new_password1" value="" type="password" autocomplete="new-password" class="form_input">
|
||||
</td>
|
||||
</tr>
|
||||
<tr class="form">
|
||||
<td>New Password again</td>
|
||||
<td>
|
||||
<input id="input_new_password2" name="new_password2" value="" type="password" autocomplete="new-password" class="form_input">
|
||||
</td>
|
||||
</tr>
|
||||
<tr class="form">
|
||||
<td colspan="2">
|
||||
we need you to repeat your password so you won't be locked out of your account if you make a typing error
|
||||
</td>
|
||||
</tr>
|
||||
<tr class="form">
|
||||
<td colspan="2" style="text-align: right;">
|
||||
<input type="submit" value="Submit" class="button_highlight" style="float: right;">
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</form>
|
||||
|
||||
{{template "form" .Other.PasswordForm}}
|
||||
{{template "form" .Other.EmailForm}}
|
||||
{{template "form" .Other.UsernameForm}}
|
||||
</div></div>
|
||||
{{template "page_bottom" .}}
|
||||
{{template "analytics"}}
|
||||
|
||||
{{template "vue"}}
|
||||
</body>
|
||||
</html>
|
||||
{{end}}
|
@@ -52,17 +52,17 @@
|
||||
{{end}}
|
||||
</td>
|
||||
{{end}}
|
||||
{{if or (ne $field.Description "") (eq $field.Separator true)}}
|
||||
<tr class="form">
|
||||
<td colspan="2">
|
||||
{{$field.Description}}
|
||||
{{if eq $field.Separator true}}
|
||||
<hr/>
|
||||
{{end}}
|
||||
</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</tr>
|
||||
{{if or (ne $field.Description "") (eq $field.Separator true)}}
|
||||
<tr class="form">
|
||||
<td colspan="2">
|
||||
{{$field.Description}}
|
||||
{{if eq $field.Separator true}}
|
||||
<hr/>
|
||||
{{end}}
|
||||
</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
{{end}}
|
||||
<tr class="form">
|
||||
{{if eq .BackLink ""}}
|
||||
|
@@ -1,10 +0,0 @@
|
||||
{{define "tpl_file_button"}}
|
||||
<template id="tpl_file_button">
|
||||
<a class="file_button" target="_blank">
|
||||
<img src="/api/file/xWwGvj9f/thumbnail?width=80&height=80" alt="24000.jpg" />
|
||||
<span style="color: var(--highlight_color);">24000.jpg</span>
|
||||
<br/>
|
||||
2020-01-20 10:06:56
|
||||
</a>
|
||||
</template>
|
||||
{{end}}
|
@@ -18,3 +18,8 @@ pixeldrain also supports previews for images, videos, audio, PDFs and much more.
|
||||
<meta property="og:image" content="/res/img/pixeldrain_big.png" />
|
||||
<meta property="og:image:type" content="image/png" />
|
||||
{{end}}
|
||||
{{define "vue"}}{{if debugMode}}
|
||||
<script src="/res/script/vue.js"></script>
|
||||
{{else}}
|
||||
<script src="/res/script/vue.min.js"></script>
|
||||
{{end}}{{end}}
|
||||
|
@@ -4,8 +4,8 @@
|
||||
<a href="/">Home</a>
|
||||
<hr />
|
||||
{{if .Authenticated}}<a href="/user">{{.Username}}</a>
|
||||
<a href="/user/filemanager?files">My Files</a>
|
||||
<a href="/user/filemanager?lists">My Lists</a>
|
||||
<a href="/user/filemanager#files">My Files</a>
|
||||
<a href="/user/filemanager#lists">My Lists</a>
|
||||
<a href="/logout">Log out</a>
|
||||
{{else}}
|
||||
<a href="/login">Login</a>
|
||||
|
@@ -69,7 +69,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 err = td.PixelAPI.AdminSetGlobals(v.Name, v.EnteredValue); err != nil {
|
||||
if apiErr, ok := err.(pixelapi.Error); ok {
|
||||
f.SubmitMessages = append(f.SubmitMessages, template.HTML(apiErr.Message))
|
||||
} else {
|
||||
|
@@ -130,11 +130,14 @@ 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.ReqError {
|
||||
log.Error("API request error occurred: %s", err.Value)
|
||||
if err, ok := err.(pixelapi.Error); ok && err.Status == http.StatusNotFound {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
wc.templates.Get().ExecuteTemplate(w, "list_not_found", templateData)
|
||||
} else {
|
||||
log.Error("API request error occurred: %s", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
wc.templates.Get().ExecuteTemplate(w, "500", templateData)
|
||||
}
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
wc.templates.Get().ExecuteTemplate(w, "list_not_found", templateData)
|
||||
return
|
||||
}
|
||||
|
||||
|
@@ -27,6 +27,7 @@ func (wc *WebController) serveLogout(
|
||||
}
|
||||
|
||||
func (wc *WebController) registerForm(td *TemplateData, r *http.Request) (f Form) {
|
||||
var err error
|
||||
// This only runs on the first request
|
||||
if wc.captchaSiteKey == "" {
|
||||
capt, err := td.PixelAPI.GetRecaptcha()
|
||||
@@ -58,7 +59,7 @@ func (wc *WebController) registerForm(td *TemplateData, r *http.Request) (f Form
|
||||
Separator: true,
|
||||
Type: FieldTypeUsername,
|
||||
}, {
|
||||
Name: "e-mail",
|
||||
Name: "email",
|
||||
Label: "E-mail address",
|
||||
Description: "not required. your e-mail address will only be " +
|
||||
"used for password resets and important account " +
|
||||
@@ -66,7 +67,7 @@ func (wc *WebController) registerForm(td *TemplateData, r *http.Request) (f Form
|
||||
Separator: true,
|
||||
Type: FieldTypeEmail,
|
||||
}, {
|
||||
Name: "password1",
|
||||
Name: "password",
|
||||
Label: "Password",
|
||||
Type: FieldTypeNewPassword,
|
||||
}, {
|
||||
@@ -79,10 +80,11 @@ func (wc *WebController) registerForm(td *TemplateData, r *http.Request) (f Form
|
||||
Type: FieldTypeNewPassword,
|
||||
}, {
|
||||
Name: "recaptcha_response",
|
||||
Label: "Turing test (click the white box)",
|
||||
Label: "reCaptcha",
|
||||
Description: "the reCaptcha turing test verifies that you " +
|
||||
"are not an evil robot that is trying to flood the " +
|
||||
"website with fake accounts",
|
||||
"website with fake accounts. Please click the white box " +
|
||||
"to prove that you're not a robot",
|
||||
Separator: true,
|
||||
Type: FieldTypeCaptcha,
|
||||
CaptchaSiteKey: wc.captchaKey(),
|
||||
@@ -94,31 +96,21 @@ func (wc *WebController) registerForm(td *TemplateData, r *http.Request) (f Form
|
||||
}
|
||||
|
||||
if f.ReadInput(r) {
|
||||
if f.FieldVal("password1") != f.FieldVal("password2") {
|
||||
if f.FieldVal("password") != f.FieldVal("password2") {
|
||||
f.SubmitMessages = []template.HTML{
|
||||
"Password verification failed. Please enter the same " +
|
||||
"password in both password fields"}
|
||||
return f
|
||||
}
|
||||
log.Debug("capt: %s", f.FieldVal("recaptcha_response"))
|
||||
resp, err := td.PixelAPI.UserRegister(
|
||||
|
||||
if err = td.PixelAPI.UserRegister(
|
||||
f.FieldVal("username"),
|
||||
f.FieldVal("e-mail"),
|
||||
f.FieldVal("password1"),
|
||||
f.FieldVal("email"),
|
||||
f.FieldVal("password"),
|
||||
f.FieldVal("recaptcha_response"),
|
||||
)
|
||||
if err != nil {
|
||||
if apiErr, ok := err.(pixelapi.Error); ok {
|
||||
f.SubmitMessages = []template.HTML{template.HTML(apiErr.Message)}
|
||||
} else {
|
||||
log.Error("%s", err)
|
||||
f.SubmitMessages = []template.HTML{"Internal Server Error"}
|
||||
}
|
||||
} else if len(resp.Errors) != 0 {
|
||||
// Registration errors occurred
|
||||
for _, rerr := range resp.Errors {
|
||||
f.SubmitMessages = append(f.SubmitMessages, template.HTML(rerr.Message))
|
||||
}
|
||||
); err != nil {
|
||||
formAPIError(err, &f)
|
||||
} else {
|
||||
// Request was a success
|
||||
f.SubmitSuccess = true
|
||||
@@ -162,12 +154,7 @@ 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"), false)
|
||||
if err != nil {
|
||||
if apiErr, ok := err.(pixelapi.Error); ok {
|
||||
f.SubmitMessages = []template.HTML{template.HTML(apiErr.Message)}
|
||||
} else {
|
||||
log.Error("%s", err)
|
||||
f.SubmitMessages = []template.HTML{"Internal Server Error"}
|
||||
}
|
||||
formAPIError(err, &f)
|
||||
} else {
|
||||
log.Debug("key %s", loginResp.APIKey)
|
||||
// Request was a success
|
||||
@@ -223,13 +210,11 @@ func (wc *WebController) passwordResetForm(td *TemplateData, r *http.Request) (f
|
||||
}
|
||||
|
||||
if f.ReadInput(r) {
|
||||
if err := td.PixelAPI.UserPasswordReset(f.FieldVal("email"), f.FieldVal("recaptcha_response")); err != nil {
|
||||
if apiErr, ok := err.(pixelapi.Error); ok {
|
||||
f.SubmitMessages = []template.HTML{template.HTML(apiErr.Message)}
|
||||
} else {
|
||||
log.Error("%s", err)
|
||||
f.SubmitMessages = []template.HTML{"Internal Server Error"}
|
||||
}
|
||||
if err := td.PixelAPI.UserPasswordReset(
|
||||
f.FieldVal("email"),
|
||||
f.FieldVal("recaptcha_response"),
|
||||
); err != nil {
|
||||
formAPIError(err, &f)
|
||||
} else {
|
||||
f.SubmitSuccess = true
|
||||
f.SubmitMessages = []template.HTML{
|
||||
@@ -247,12 +232,12 @@ func (wc *WebController) passwordResetConfirmForm(td *TemplateData, r *http.Requ
|
||||
Title: td.Title,
|
||||
Fields: []Field{
|
||||
{
|
||||
Name: "password1",
|
||||
Label: "password",
|
||||
Name: "new_password",
|
||||
Label: "Password",
|
||||
Type: FieldTypeNewPassword,
|
||||
}, {
|
||||
Name: "password2",
|
||||
Label: "password again",
|
||||
Name: "new_password2",
|
||||
Label: "Password again",
|
||||
Description: "you need to enter your password twice so we " +
|
||||
"can verify that no typing errors were made, which would " +
|
||||
"prevent you from logging into your new account",
|
||||
@@ -271,23 +256,20 @@ func (wc *WebController) passwordResetConfirmForm(td *TemplateData, r *http.Requ
|
||||
}
|
||||
|
||||
if f.ReadInput(r) {
|
||||
if f.FieldVal("password1") != f.FieldVal("password2") {
|
||||
if f.FieldVal("new_password") != f.FieldVal("new_password2") {
|
||||
f.SubmitMessages = []template.HTML{
|
||||
"Password verification failed. Please enter the same " +
|
||||
"password in both password fields"}
|
||||
return f
|
||||
}
|
||||
|
||||
if err := td.PixelAPI.UserPasswordResetConfirm(resetKey, f.FieldVal("password1")); err != nil {
|
||||
if err.Error() == "not_found" {
|
||||
f.SubmitMessages = []template.HTML{template.HTML("Password reset key not found")}
|
||||
} else {
|
||||
log.Error("%s", err)
|
||||
f.SubmitMessages = []template.HTML{"Internal Server Error"}
|
||||
}
|
||||
if err := td.PixelAPI.UserPasswordResetConfirm(resetKey, f.FieldVal("new_password")); err != nil {
|
||||
formAPIError(err, &f)
|
||||
} else {
|
||||
f.SubmitSuccess = true
|
||||
f.SubmitMessages = []template.HTML{"Success! You can now log in with your new password"}
|
||||
f.SubmitMessages = []template.HTML{
|
||||
`Success! You can now <a href="/login">log in</a> with your new password`,
|
||||
}
|
||||
}
|
||||
}
|
||||
return f
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package webcontroller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net/http"
|
||||
|
||||
@@ -9,6 +10,44 @@ import (
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
// formAPIError makes it easier to display errors returned by the pixeldrain
|
||||
// API. TO make use of this function the form fields should be named exactly the
|
||||
// same as the API parameters
|
||||
func formAPIError(err error, f *Form) {
|
||||
fieldLabel := func(name string) string {
|
||||
for _, v := range f.Fields {
|
||||
if v.Name == name {
|
||||
return v.Label
|
||||
}
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
if err, ok := err.(pixelapi.Error); ok {
|
||||
if err.StatusCode == "multiple_errors" {
|
||||
for _, err := range err.Errors {
|
||||
// Modify the message to make it more user-friendly
|
||||
if err.StatusCode == "string_out_of_range" {
|
||||
err.Message = fmt.Sprintf(
|
||||
"%s is too long or too short. Should be between %v and %v characters. Current length: %v",
|
||||
fieldLabel(err.Extra["field"].(string)),
|
||||
err.Extra["min_len"],
|
||||
err.Extra["max_len"],
|
||||
err.Extra["len"],
|
||||
)
|
||||
}
|
||||
|
||||
f.SubmitMessages = append(f.SubmitMessages, template.HTML(err.Message))
|
||||
}
|
||||
} else {
|
||||
f.SubmitMessages = append(f.SubmitMessages, template.HTML(err.Message))
|
||||
}
|
||||
} else {
|
||||
log.Error("Error submitting form: %s", err)
|
||||
f.SubmitMessages = []template.HTML{"Internal Server Error"}
|
||||
}
|
||||
}
|
||||
|
||||
func (wc *WebController) serveUserSettings(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
@@ -44,7 +83,7 @@ func (wc *WebController) passwordForm(td *TemplateData, r *http.Request) (f Form
|
||||
Label: "Old Password",
|
||||
Type: FieldTypeCurrentPassword,
|
||||
}, {
|
||||
Name: "new_password1",
|
||||
Name: "new_password",
|
||||
Label: "New Password",
|
||||
Type: FieldTypeNewPassword,
|
||||
}, {
|
||||
@@ -60,7 +99,7 @@ func (wc *WebController) passwordForm(td *TemplateData, r *http.Request) (f Form
|
||||
}
|
||||
|
||||
if f.ReadInput(r) {
|
||||
if f.FieldVal("new_password1") != f.FieldVal("new_password2") {
|
||||
if f.FieldVal("new_password") != f.FieldVal("new_password2") {
|
||||
f.SubmitMessages = []template.HTML{
|
||||
"Password verification failed. Please enter the same " +
|
||||
"password in both new password fields"}
|
||||
@@ -71,14 +110,9 @@ func (wc *WebController) passwordForm(td *TemplateData, r *http.Request) (f Form
|
||||
// form
|
||||
if err := td.PixelAPI.UserPasswordSet(
|
||||
f.FieldVal("old_password"),
|
||||
f.FieldVal("new_password1"),
|
||||
f.FieldVal("new_password"),
|
||||
); err != nil {
|
||||
if apiErr, ok := err.(pixelapi.Error); ok {
|
||||
f.SubmitMessages = []template.HTML{template.HTML(apiErr.Message)}
|
||||
} else {
|
||||
log.Error("%s", err)
|
||||
f.SubmitMessages = []template.HTML{"Internal Server Error"}
|
||||
}
|
||||
formAPIError(err, &f)
|
||||
} else {
|
||||
// Request was a success
|
||||
f.SubmitSuccess = true
|
||||
@@ -111,12 +145,7 @@ func (wc *WebController) emailForm(td *TemplateData, r *http.Request) (f Form) {
|
||||
f.FieldVal("new_email"),
|
||||
false,
|
||||
); err != nil {
|
||||
if apiErr, ok := err.(pixelapi.Error); ok {
|
||||
f.SubmitMessages = []template.HTML{template.HTML(apiErr.Message)}
|
||||
} else {
|
||||
log.Error("%s", err)
|
||||
f.SubmitMessages = []template.HTML{"Internal Server Error"}
|
||||
}
|
||||
formAPIError(err, &f)
|
||||
} else {
|
||||
// Request was a success
|
||||
f.SubmitSuccess = true
|
||||
@@ -131,18 +160,18 @@ func (wc *WebController) serveEmailConfirm(
|
||||
r *http.Request,
|
||||
p httprouter.Params,
|
||||
) {
|
||||
var err error
|
||||
var status string
|
||||
if key, err := wc.getAPIKey(r); err == nil {
|
||||
api := pixelapi.New(wc.apiURLInternal)
|
||||
api.APIKey = key
|
||||
err = api.UserEmailResetConfirm(r.FormValue("key"))
|
||||
if err != nil && err.Error() == "not_found" {
|
||||
status = "not_found"
|
||||
} else if err != nil {
|
||||
status = "internal_error"
|
||||
} else {
|
||||
status = "success"
|
||||
}
|
||||
|
||||
api := pixelapi.New(wc.apiURLInternal)
|
||||
err = api.UserEmailResetConfirm(r.FormValue("key"))
|
||||
if err != nil && err.Error() == "not_found" {
|
||||
status = "not_found"
|
||||
} else if err != nil {
|
||||
log.Error("E-mail reset fail: %s", err)
|
||||
status = "internal_error"
|
||||
} else {
|
||||
status = "success"
|
||||
}
|
||||
|
||||
td := wc.newTemplateData(w, r)
|
||||
@@ -171,12 +200,7 @@ func (wc *WebController) usernameForm(td *TemplateData, r *http.Request) (f Form
|
||||
|
||||
if f.ReadInput(r) {
|
||||
if err := td.PixelAPI.UserSetUsername(f.FieldVal("new_username")); err != nil {
|
||||
if apiErr, ok := err.(pixelapi.Error); ok {
|
||||
f.SubmitMessages = []template.HTML{template.HTML(apiErr.Message)}
|
||||
} else {
|
||||
log.Error("%s", err)
|
||||
f.SubmitMessages = []template.HTML{"Internal Server Error"}
|
||||
}
|
||||
formAPIError(err, &f)
|
||||
} else {
|
||||
// Request was a success
|
||||
f.SubmitSuccess = true
|
||||
|
Reference in New Issue
Block a user