2022-07-28 15:00:43 +00:00
|
|
|
|
package handlers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
2022-09-15 13:53:55 +00:00
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/Pena-Co-Ltd/amocrm_templategen_back/dal/model"
|
2022-10-11 10:07:29 +00:00
|
|
|
|
"github.com/Pena-Co-Ltd/amocrm_templategen_back/tools"
|
|
|
|
|
YaDisk "github.com/Pena-Co-Ltd/amocrm_templategen_back/yadisk"
|
2022-07-28 15:00:43 +00:00
|
|
|
|
"github.com/gorilla/schema"
|
|
|
|
|
"go.uber.org/zap"
|
2022-08-10 13:53:34 +00:00
|
|
|
|
"golang.org/x/oauth2"
|
2022-07-28 15:00:43 +00:00
|
|
|
|
"net/http"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2022-09-15 13:53:55 +00:00
|
|
|
|
type ReqYaDiskSaveToken struct {
|
2022-07-28 15:00:43 +00:00
|
|
|
|
AccessToken string `json:"access_token" schema:"access_token"`
|
2022-11-24 19:37:47 +00:00
|
|
|
|
Code string `json:"code" schema:"code"`
|
2022-07-28 15:00:43 +00:00
|
|
|
|
ExpiresIn int64 `json:"expires_in" schema:"expires_in"`
|
|
|
|
|
TokenType string `json:"token_type" schema:"token_type"`
|
2022-11-24 19:37:47 +00:00
|
|
|
|
RefreshToken string `json:"refresh_token" schema:"refresh_token"`
|
2022-07-28 15:00:43 +00:00
|
|
|
|
State string `json:"state" schema:"state"`
|
|
|
|
|
Scope string `json:"scope" schema:"scope"`
|
|
|
|
|
Error string `json:"error" schema:"error"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *Handlers) YaDiskSaveToken(w http.ResponseWriter, r *http.Request) {
|
2022-09-15 13:53:55 +00:00
|
|
|
|
var req ReqYaDiskSaveToken
|
2022-07-28 15:00:43 +00:00
|
|
|
|
err := r.ParseForm()
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.reportError(w, err, http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-15 13:53:55 +00:00
|
|
|
|
err = schema.NewDecoder().Decode(&req, r.Form)
|
2022-07-28 15:00:43 +00:00
|
|
|
|
if err != nil {
|
2022-11-19 13:24:30 +00:00
|
|
|
|
h.reportError(w, err, http.StatusBadRequest)
|
2022-07-28 15:00:43 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-15 13:53:55 +00:00
|
|
|
|
if req.Error != "" {
|
|
|
|
|
err = errors.New("YaDiskErr:" + req.Error)
|
2022-11-19 13:24:30 +00:00
|
|
|
|
h.reportError(w, err, http.StatusBadRequest)
|
2022-07-28 15:00:43 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-15 13:53:55 +00:00
|
|
|
|
if req.AccessToken == "" && req.Code == "" {
|
2022-11-19 13:24:30 +00:00
|
|
|
|
err = errors.New("got empty token")
|
2022-07-28 15:00:43 +00:00
|
|
|
|
h.reportError(w, err, http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-11 10:07:29 +00:00
|
|
|
|
if req.State == "" {
|
2022-11-19 13:24:30 +00:00
|
|
|
|
err = errors.New("got empty state")
|
2022-10-11 10:07:29 +00:00
|
|
|
|
h.reportError(w, err, http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var state tools.StateToken
|
|
|
|
|
err = tools.DecryptTokenRC4(req.State, &state)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.reportError(w, err, http.StatusUnauthorized)
|
2022-07-28 15:00:43 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-10 13:53:34 +00:00
|
|
|
|
token := &oauth2.Token{
|
2022-09-15 13:53:55 +00:00
|
|
|
|
AccessToken: req.AccessToken,
|
|
|
|
|
TokenType: req.TokenType,
|
|
|
|
|
RefreshToken: req.RefreshToken,
|
|
|
|
|
Expiry: time.Now().Add(time.Duration(req.ExpiresIn) * time.Second),
|
2022-08-10 13:53:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-15 13:53:55 +00:00
|
|
|
|
yaDiskClient, err := h.YaDisk.NewClient(r.Context(), token, req.Code)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.reportError(w, err, http.StatusForbidden)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
token = yaDiskClient.Token
|
|
|
|
|
|
|
|
|
|
yaUser, err := yaDiskClient.GetUser()
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.reportError(w, err, http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-28 15:00:43 +00:00
|
|
|
|
// Insert/Update token in DB
|
|
|
|
|
_, err = h.dal.YaDisk.InsertOrUpdate(r.Context(), &model.YaDisk{
|
2022-10-11 10:07:29 +00:00
|
|
|
|
UserID: state.UserID,
|
|
|
|
|
Name: fmt.Sprintf("Yandex Disk (%v)", yaUser.Login),
|
2022-09-15 13:53:55 +00:00
|
|
|
|
UID: yaUser.Uid,
|
|
|
|
|
Login: yaUser.Login,
|
|
|
|
|
DisplayName: yaUser.DisplayName,
|
2022-08-10 13:53:34 +00:00
|
|
|
|
AccessToken: token.AccessToken,
|
|
|
|
|
RefreshToken: token.RefreshToken,
|
|
|
|
|
ExpiresIn: token.Expiry,
|
|
|
|
|
TokenType: token.TokenType,
|
2022-10-11 10:07:29 +00:00
|
|
|
|
TemplateFolder: YaDisk.DEFAULT_TEMPLATE_FOLDER,
|
|
|
|
|
SaveFolder: YaDisk.DEFAULT_SAVE_FOLDER,
|
2022-07-28 15:00:43 +00:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.reportError(w, err, 500)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make default directories in YandexDisk
|
2022-10-11 10:07:29 +00:00
|
|
|
|
_, err = yaDiskClient.PutResources(YaDisk.DEFAULT_FOLDER)
|
2022-07-28 15:00:43 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
h.logger.Error("ErrorHandler", zap.Error(err))
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-11 10:07:29 +00:00
|
|
|
|
_, err = yaDiskClient.PutResources(YaDisk.DEFAULT_TEMPLATE_FOLDER)
|
2022-07-28 15:00:43 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
h.logger.Error("ErrorHandler", zap.Error(err))
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-11 10:07:29 +00:00
|
|
|
|
_, err = yaDiskClient.PutResources(YaDisk.DEFAULT_SAVE_FOLDER)
|
2022-07-28 15:00:43 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
h.logger.Error("ErrorHandler", zap.Error(err))
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-11 10:07:29 +00:00
|
|
|
|
http.Redirect(w, r, state.RedirectUrl, http.StatusPermanentRedirect)
|
2022-07-28 15:00:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-11 10:07:29 +00:00
|
|
|
|
type ReqYaDiskSetSettings struct {
|
2022-10-21 20:38:31 +00:00
|
|
|
|
ID string `json:"id"`
|
2022-10-11 10:07:29 +00:00
|
|
|
|
Name string `json:"name"`
|
2022-07-28 15:00:43 +00:00
|
|
|
|
TemplateFolder string `json:"template_folder"`
|
|
|
|
|
SaveFolder string `json:"save_folder"`
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-11 10:07:29 +00:00
|
|
|
|
func (h *Handlers) YaDiskSetSettings(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var req ReqYaDiskSetSettings
|
2022-07-28 15:00:43 +00:00
|
|
|
|
|
2022-10-11 10:07:29 +00:00
|
|
|
|
amoData := getAmoByJwt(r)
|
|
|
|
|
if amoData == nil {
|
2022-07-28 15:00:43 +00:00
|
|
|
|
h.reportError(w, ErrorUnauthorized, http.StatusUnauthorized)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-15 13:53:55 +00:00
|
|
|
|
err := decodePost(&req, r)
|
2022-07-28 15:00:43 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
h.reportError(w, err, http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-21 20:38:31 +00:00
|
|
|
|
if req.ID == "" {
|
|
|
|
|
h.reportError(w, errors.New("id required"), http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-28 15:00:43 +00:00
|
|
|
|
err = h.dal.YaDisk.Update(r.Context(), &model.YaDisk{
|
2022-10-21 20:38:31 +00:00
|
|
|
|
ID: req.ID,
|
2022-10-11 10:07:29 +00:00
|
|
|
|
Name: req.Name,
|
2022-09-15 13:53:55 +00:00
|
|
|
|
TemplateFolder: req.TemplateFolder,
|
2022-10-11 10:07:29 +00:00
|
|
|
|
SaveFolder: req.SaveFolder})
|
2022-07-28 15:00:43 +00:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.reportError(w, err, http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sendResponse(w, 200, nil)
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-15 13:53:55 +00:00
|
|
|
|
// YaDiskGetList - возвращает список хранилищ Yandex закрепленных за пользователем по его UserID из JWT
|
|
|
|
|
func (h *Handlers) YaDiskGetList(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
amoData := getAmoByJwt(r)
|
|
|
|
|
|
2022-10-11 10:07:29 +00:00
|
|
|
|
if amoData == nil {
|
2022-07-28 15:00:43 +00:00
|
|
|
|
h.reportError(w, ErrorUnauthorized, http.StatusUnauthorized)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-11 10:07:29 +00:00
|
|
|
|
yadiskInfo, err := h.dal.YaDisk.GetListByUserID(r.Context(), amoData.UserID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.reportError(w, err, http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sendResponse(w, http.StatusOK, yadiskInfo)
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-21 15:07:49 +00:00
|
|
|
|
type ReqYaDiskGetResources struct {
|
2022-10-11 10:07:29 +00:00
|
|
|
|
ID string `json:"id"` // ID хранилища
|
|
|
|
|
Path string `json:"path"` // Путь до папки. По умолчанию: disk:/
|
|
|
|
|
Limit int `json:"limit"` // По умолчанию: 20
|
|
|
|
|
Offset int `json:"offset"` // По умолчанию: 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *Handlers) YaDiskGetResources(w http.ResponseWriter, r *http.Request) {
|
2022-11-21 15:07:49 +00:00
|
|
|
|
var req ReqYaDiskGetResources
|
2022-10-11 10:07:29 +00:00
|
|
|
|
|
|
|
|
|
amoData := getAmoByJwt(r)
|
|
|
|
|
|
|
|
|
|
if amoData == nil {
|
|
|
|
|
h.reportError(w, ErrorUnauthorized, http.StatusUnauthorized)
|
|
|
|
|
return
|
2022-08-10 13:53:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-11 10:07:29 +00:00
|
|
|
|
err := decodePost(&req, r)
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.reportError(w, err, http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if req.ID == "" {
|
|
|
|
|
h.reportError(w, errors.New("id required"), http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yadisk, err := h.dal.YaDisk.GetByID(r.Context(), req.ID)
|
2022-07-28 15:00:43 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
h.reportError(w, err, http.StatusInternalServerError)
|
2022-09-15 13:53:55 +00:00
|
|
|
|
return
|
2022-07-28 15:00:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-11 10:07:29 +00:00
|
|
|
|
client, err := h.YaDisk.NewClient(r.Context(), yadisk.Token(), "")
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.reportError(w, err, http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resource, err := client.GetResources(req.Path, req.Limit, req.Offset)
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.reportError(w, err, http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sendResponse(w, 200, resource)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ReqYaDiskPutResources struct {
|
|
|
|
|
ID string `json:"id"`
|
|
|
|
|
Path string `json:"path"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *Handlers) YaDiskPutResources(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var req ReqYaDiskPutResources
|
|
|
|
|
|
|
|
|
|
amoData := getAmoByJwt(r)
|
|
|
|
|
|
|
|
|
|
if amoData == nil {
|
|
|
|
|
h.reportError(w, ErrorUnauthorized, http.StatusUnauthorized)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := decodePost(&req, r)
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.reportError(w, err, http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if req.ID == "" {
|
|
|
|
|
h.reportError(w, errors.New("id required"), http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yadisk, err := h.dal.YaDisk.GetByID(r.Context(), req.ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.reportError(w, err, http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client, err := h.YaDisk.NewClient(r.Context(), yadisk.Token(), "")
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.reportError(w, err, http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res, err := client.PutResources(req.Path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.reportError(w, err, http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sendResponse(w, http.StatusOK, res)
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 19:37:47 +00:00
|
|
|
|
type ReqYaDiskUploadResources struct {
|
|
|
|
|
ID string `json:"id" schema:"id"`
|
|
|
|
|
Path string `json:"path" schema:"path"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *Handlers) YaDiskUploadResources(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
// Check form
|
|
|
|
|
fileData, fileHeader, err := r.FormFile("file")
|
|
|
|
|
defer fileData.Close()
|
|
|
|
|
|
|
|
|
|
var req ReqYaDiskUploadResources
|
|
|
|
|
err = schema.NewDecoder().Decode(&req, r.Form)
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.reportError(w, err, http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if req.ID == "" {
|
|
|
|
|
h.reportError(w, errors.New("id required"), http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if req.Path == "" {
|
|
|
|
|
req.Path = "disk:"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var maxSize int64 = 10 << 20 // mb
|
|
|
|
|
if fileHeader.Size > maxSize {
|
|
|
|
|
h.reportError(w, errors.New("max size 10 mb"), http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Upload file to the storage
|
|
|
|
|
storage, err := h.dal.YaDisk.GetByID(r.Context(), req.ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.reportError(w, err, http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client, err := h.YaDisk.NewClient(r.Context(), storage.Token(), "")
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.reportError(w, err, http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
storagePath := fmt.Sprintf("%v/%v", req.Path, fileHeader.Filename)
|
|
|
|
|
|
|
|
|
|
_, err = client.UploadResources(storagePath, fileData)
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.reportError(w, err, http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-11 10:07:29 +00:00
|
|
|
|
type ReqYaDiskDeleteResources struct {
|
|
|
|
|
ID string `json:"ID"`
|
|
|
|
|
Path string `json:"path"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *Handlers) YaDiskDeleteResources(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var req ReqYaDiskPutResources
|
|
|
|
|
|
|
|
|
|
amoData := getAmoByJwt(r)
|
|
|
|
|
|
|
|
|
|
if amoData == nil {
|
|
|
|
|
h.reportError(w, ErrorUnauthorized, http.StatusUnauthorized)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := decodePost(&req, r)
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.reportError(w, err, http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if req.ID == "" {
|
|
|
|
|
h.reportError(w, errors.New("id required"), http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yadisk, err := h.dal.YaDisk.GetByID(r.Context(), req.ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.reportError(w, err, http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client, err := h.YaDisk.NewClient(r.Context(), yadisk.Token(), "")
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.reportError(w, err, http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = client.DeleteResources(req.Path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.reportError(w, err, http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2022-07-28 15:00:43 +00:00
|
|
|
|
}
|