2015-07-29 07:01:23 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-12-28 01:40:20 +00:00
|
|
|
"context"
|
2015-07-29 07:01:23 +00:00
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/flosch/pongo2"
|
|
|
|
"github.com/gorilla/sessions"
|
2017-12-24 19:10:43 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2015-07-29 07:01:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ajaxResponse struct {
|
|
|
|
Error bool `json:"error"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
Data string `json:"data"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a ajaxResponse) Bytes() []byte {
|
|
|
|
out, _ := json.Marshal(a)
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2017-12-28 01:40:20 +00:00
|
|
|
func ajaxGetHandler(c context.Context, res http.ResponseWriter, r *http.Request, session *sessions.Session, ctx *pongo2.Context) (*string, error) {
|
2015-07-29 07:01:23 +00:00
|
|
|
res.Header().Set("Content-Type", "application/json")
|
2017-12-28 01:40:20 +00:00
|
|
|
user, _ := checkLogin(c, r, session)
|
2015-07-29 07:01:23 +00:00
|
|
|
|
2017-12-28 01:40:20 +00:00
|
|
|
if user == nil || !storage.IsPresent(c, user.UserFile) {
|
2015-07-29 07:01:23 +00:00
|
|
|
res.Write(ajaxResponse{Error: true}.Bytes())
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2017-12-28 01:40:20 +00:00
|
|
|
userFileRaw, err := storage.Read(c, user.UserFile)
|
2015-07-29 07:01:23 +00:00
|
|
|
if err != nil {
|
2017-12-24 19:10:43 +00:00
|
|
|
log.WithError(err).Error("Could not read user file from storage")
|
2015-07-29 08:10:46 +00:00
|
|
|
res.Write(ajaxResponse{Error: true}.Bytes())
|
|
|
|
return nil, nil
|
2015-07-29 07:01:23 +00:00
|
|
|
}
|
|
|
|
|
2015-07-29 08:10:46 +00:00
|
|
|
userFile, _ := readDataObject(userFileRaw)
|
2015-07-29 07:01:23 +00:00
|
|
|
|
|
|
|
res.Write(ajaxResponse{Version: userFile.MetaData.Version, Data: userFile.Data}.Bytes())
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2017-12-28 01:40:20 +00:00
|
|
|
func ajaxPostHandler(c context.Context, res http.ResponseWriter, r *http.Request, session *sessions.Session, ctx *pongo2.Context) (*string, error) {
|
2015-07-29 07:01:23 +00:00
|
|
|
res.Header().Set("Content-Type", "application/json")
|
2017-12-28 01:40:20 +00:00
|
|
|
user, _ := checkLogin(c, r, session)
|
2015-07-29 07:01:23 +00:00
|
|
|
|
|
|
|
if user == nil {
|
|
|
|
res.Write(ajaxResponse{Error: true, Type: "login"}.Bytes())
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2017-12-28 01:40:20 +00:00
|
|
|
if !storage.IsPresent(c, user.UserFile) {
|
2015-07-29 07:01:23 +00:00
|
|
|
res.Write(ajaxResponse{Error: true, Type: "register"}.Bytes())
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2017-12-28 01:40:20 +00:00
|
|
|
userFileRaw, err := storage.Read(c, user.UserFile)
|
2015-07-29 07:01:23 +00:00
|
|
|
if err != nil {
|
2017-12-24 19:10:43 +00:00
|
|
|
log.WithError(err).Error("Could not read user file from storage")
|
2015-07-29 08:10:46 +00:00
|
|
|
res.Write(ajaxResponse{Error: true, Type: "storage_error"}.Bytes())
|
|
|
|
return nil, nil
|
2015-07-29 07:01:23 +00:00
|
|
|
}
|
|
|
|
|
2015-07-29 08:10:46 +00:00
|
|
|
userFile, _ := readDataObject(userFileRaw)
|
2015-07-29 07:01:23 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
version = r.FormValue("version")
|
|
|
|
checksum = r.FormValue("checksum")
|
|
|
|
data = r.FormValue("data")
|
|
|
|
)
|
|
|
|
|
|
|
|
if userFile.MetaData.Version != version {
|
|
|
|
res.Write(ajaxResponse{Error: true, Type: "version"}.Bytes())
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if checksum != fmt.Sprintf("%x", sha1.Sum([]byte(data))) {
|
|
|
|
res.Write(ajaxResponse{Error: true, Type: "checksum"}.Bytes())
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2017-12-28 01:40:20 +00:00
|
|
|
if err := storage.Backup(c, user.UserFile); err != nil {
|
2017-12-24 19:10:43 +00:00
|
|
|
log.WithError(err).Error("Could not create backup of user file")
|
2015-07-29 08:10:46 +00:00
|
|
|
res.Write(ajaxResponse{Error: true, Type: "storage_error"}.Bytes())
|
|
|
|
return nil, nil
|
2015-07-29 07:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
userFile.MetaData.Version = checksum
|
|
|
|
userFile.Data = data
|
|
|
|
|
2015-07-29 08:10:46 +00:00
|
|
|
d, _ := userFile.GetData()
|
2015-07-29 07:01:23 +00:00
|
|
|
|
2017-12-28 01:40:20 +00:00
|
|
|
if err := storage.Write(c, user.UserFile, d); err != nil {
|
2017-12-24 19:10:43 +00:00
|
|
|
log.WithError(err).Error("Could not write user file to storage")
|
2015-07-29 08:10:46 +00:00
|
|
|
res.Write(ajaxResponse{Error: true, Type: "storage_error"}.Bytes())
|
|
|
|
return nil, nil
|
2015-07-29 07:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res.Write(ajaxResponse{Version: userFile.MetaData.Version, Data: userFile.Data}.Bytes())
|
|
|
|
return nil, nil
|
|
|
|
}
|