diff --git a/pixelapi/file.go b/pixelapi/file.go index 554a13a..a5f0940 100644 --- a/pixelapi/file.go +++ b/pixelapi/file.go @@ -1,10 +1,7 @@ package pixelapi import ( - "encoding/json" "io" - - "github.com/Fornaxian/log" ) // GetFile makes a file download request and returns a readcloser. Don't forget @@ -29,18 +26,11 @@ type FileInfo struct { } // GetFileInfo gets the FileInfo from the pixeldrain API -func (p *PixelAPI) GetFileInfo(id string) *FileInfo { - body, err := getString(p.apiEndpoint + "/file/" + id + "/info") - +func (p *PixelAPI) GetFileInfo(id string) (resp *FileInfo, err *Error) { + resp = &FileInfo{} + err = getJSON(p.apiEndpoint+"/file/"+id+"/info", resp) if err != nil { - log.Error("req failed: %v", err) - return nil + return nil, err } - var fileInfo FileInfo - err = json.Unmarshal([]byte(body), &fileInfo) - if err != nil { - log.Error("unmarshal failed: %v. json: %s", err, body) - return nil - } - return &fileInfo + return resp, nil } diff --git a/pixelapi/list.go b/pixelapi/list.go index c658182..d4c9c36 100644 --- a/pixelapi/list.go +++ b/pixelapi/list.go @@ -1,9 +1,5 @@ package pixelapi -import ( - "encoding/json" -) - // API error constants const ( ListNotFoundError = "list_not_found" @@ -11,7 +7,6 @@ const ( // List information object from the pixeldrain API type List struct { - Error *ErrorResponse Success bool `json:"success"` ID string `json:"id"` Title string `json:"title"` @@ -32,22 +27,11 @@ 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) *List { - var list = &List{} - body, err := getString(p.apiEndpoint + "/list/" + id) +func (p *PixelAPI) GetList(id string) (resp *List, err *Error) { + resp = &List{} + err = getJSON(p.apiEndpoint+"/list/"+id, resp) if err != nil { - list.Error = errorResponseFromError(err) - return list + return nil, err } - - err = json.Unmarshal([]byte(body), list) - if err != nil { - list.Error = errorResponseFromError(err) - return list - } - - if !list.Success { - list.Error = errorResponseFromJSON(body) - } - return list + return resp, nil } diff --git a/pixelapi/misc.go b/pixelapi/misc.go new file mode 100644 index 0000000..965d8b7 --- /dev/null +++ b/pixelapi/misc.go @@ -0,0 +1,13 @@ +package pixelapi + +type Recaptcha struct { + SiteKey string `json:"site_key"` +} + +func (p *PixelAPI) GetRecaptcha() (resp *Recaptcha, err *Error) { + err = getJSON(p.apiEndpoint+"/misc/recpatcha", resp) + if err != nil { + return nil, err + } + return resp, nil +} diff --git a/pixelapi/pixelapi.go b/pixelapi/pixelapi.go index cc659a8..f97b65f 100644 --- a/pixelapi/pixelapi.go +++ b/pixelapi/pixelapi.go @@ -1,9 +1,141 @@ package pixelapi +import ( + "encoding/json" + "io" + "io/ioutil" + "net/http" + + "github.com/Fornaxian/log" +) + +// PixelAPI is the Pixeldrain API client type PixelAPI struct { apiEndpoint string } +// New creates a new Pixeldrain API client to query the Pixeldrain API with func New(apiEndpoint string) *PixelAPI { return &PixelAPI{apiEndpoint} } + +type Error struct { + ReqError bool + Success bool `json:"success"` + Value string `json:"value"` + Message string `json:"message"` +} + +func (e Error) Error() string { return e.Value } + +func errorResponseFromJSON(j string) *Error { + var r = &Error{} + var err = json.Unmarshal([]byte(j), r) + if err != nil { + r.Success = false + r.ReqError = true + r.Value = err.Error() + } + return r +} +func errorResponseFromError(e error) *Error { + var r = &Error{} + r.Success = false + r.ReqError = true + r.Value = e.Error() + return r +} + +func getJSON(url string, target interface{}) *Error { + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return &Error{ + ReqError: true, + Success: false, + Value: err.Error(), + Message: err.Error(), + } + } + + client := &http.Client{} + + resp, err := client.Do(req) + if err != nil { + return &Error{ + ReqError: true, + Success: false, + Value: err.Error(), + Message: err.Error(), + } + } + + defer resp.Body.Close() + var jdec = json.NewDecoder(resp.Body) + + // Test for client side and server side errors + if resp.StatusCode >= 400 { + var errResp = &Error{ + ReqError: false, + } + err = jdec.Decode(&errResp) + if err != nil { + log.Error("Can't decode this: %v", err) + return &Error{ + ReqError: true, + Success: false, + Value: err.Error(), + Message: err.Error(), + } + } + return errResp + } + + err = jdec.Decode(target) + if 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 nil +} + +func getString(url string) (string, error) { + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return "", err + } + + client := &http.Client{} + + resp, err := client.Do(req) + if err != nil { + return "", err + } + + defer resp.Body.Close() + + bodyBytes, err := ioutil.ReadAll(resp.Body) + + return string(bodyBytes), err +} + +func getRaw(url string) (io.ReadCloser, error) { + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return nil, err + } + + client := &http.Client{} + + resp, err := client.Do(req) + if err != nil { + return nil, err + } + + return resp.Body, err +} diff --git a/pixelapi/request.go b/pixelapi/request.go deleted file mode 100644 index a5486a2..0000000 --- a/pixelapi/request.go +++ /dev/null @@ -1,70 +0,0 @@ -package pixelapi - -import ( - "encoding/json" - "io" - "io/ioutil" - "net/http" -) - -type ErrorResponse struct { - ReqError bool - Success bool `json:"success"` - Value string `json:"value"` - Message *string `json:"message"` - ID *string `json:"id"` -} - -func errorResponseFromJSON(j string) *ErrorResponse { - var r = &ErrorResponse{} - var err = json.Unmarshal([]byte(j), r) - if err != nil { - r.Success = false - r.ReqError = true - r.Value = err.Error() - } - return r -} -func errorResponseFromError(e error) *ErrorResponse { - var r = &ErrorResponse{} - r.Success = false - r.ReqError = true - r.Value = e.Error() - return r -} - -func getString(url string) (string, error) { - req, err := http.NewRequest("GET", url, nil) - if err != nil { - return "", err - } - - client := &http.Client{} - - resp, err := client.Do(req) - if err != nil { - return "", err - } - - defer resp.Body.Close() - - bodyBytes, err := ioutil.ReadAll(resp.Body) - - return string(bodyBytes), err -} - -func getRaw(url string) (io.ReadCloser, error) { - req, err := http.NewRequest("GET", url, nil) - if err != nil { - return nil, err - } - - client := &http.Client{} - - resp, err := client.Do(req) - if err != nil { - return nil, err - } - - return resp.Body, err -} diff --git a/res/static/res/style/layout.css b/res/static/res/style/layout.css index 82322dd..d212c65 100644 --- a/res/static/res/style/layout.css +++ b/res/static/res/style/layout.css @@ -1,4 +1,4 @@ -/* +/* Created on : Jun 3, 2015, 9:33:11 AM Author : Fornax */ @@ -72,7 +72,7 @@ html{ margin: 0 4px; text-decoration: none; font-family: "Lato", sans-serif; - font-weight: bold; + font-weight: bold; font-size: 26px; } .navigation a:hover { @@ -123,8 +123,8 @@ html{ .highlight_middle {background-color: #3a3a3a;} .highlight_dark {background-color: #303030;} -.border-top {border-top: #686868 1px solid;} -.border-bottom {border-bottom: #686868 1px solid;} +.border_top {border-top: #686868 1px solid;} +.border_bottom {border-bottom: #686868 1px solid;} /* Common elements */ @@ -241,9 +241,13 @@ a:hover{ /* Form fields */ +.form_input { + width: 100%; +} + /* BUTTONS */ button, -input[type="submit"], +input[type="submit"], input[type="button"], input[type="color"], select{ @@ -260,19 +264,19 @@ select{ cursor: pointer; } button:hover, -input[type="submit"]:hover, +input[type="submit"]:hover, input[type="button"]:hover, input[type="color"]:hover, select:hover, button:focus, -input[type="submit"]:focus, +input[type="submit"]:focus, input[type="button"]:focus, input[type="color"]:focus, select:focus{ box-shadow: var(--highlight_border), 2px 2px 8px #000000; } button:active, -input[type="submit"]:active, +input[type="submit"]:active, input[type="button"]:active, input[type="color"]:active, select:active{ @@ -281,10 +285,10 @@ select:active{ padding: 8px 8px 4px 12px; } .button_full_width {width: calc(100% - 4px);} -.button_highlight {background: linear-gradient(var(--highlight_color), var(--highlight_color_dark));} -.button_highlight:active{background: linear-gradient(var(--highlight_color_dark), var(--highlight_color));} -.button_red {background: linear-gradient(#821C40, #61152F);} -.button_red:active {background: linear-gradient(#61152F, #821C40);} +.button_highlight {background: linear-gradient(var(--highlight_color), var(--highlight_color_dark)) !important; color: #000000 !important;} +.button_highlight:active{background: linear-gradient(var(--highlight_color_dark), var(--highlight_color)) !important; color: #000000 !important;} +.button_red {background: linear-gradient(#821C40, #61152F) !important;} +.button_red:active {background: linear-gradient(#61152F, #821C40) !important;} /* Dropdown list of the select tag */ option{ diff --git a/res/template/account/login.html b/res/template/account/login.html index 113042f..4126f28 100644 --- a/res/template/account/login.html +++ b/res/template/account/login.html @@ -1,24 +1,22 @@ +{{define "login"}}