1
0
mirror of synced 2024-11-21 20:16:02 +03:00

add folder name for s3 to config

This commit is contained in:
DmitryZagorulko 2018-06-08 13:57:51 +03:00
parent 9636237212
commit 1fff569009
7 changed files with 27 additions and 17 deletions

View File

@ -25,6 +25,7 @@ type ConfigAWS struct {
SecretAccessKey string `yaml:"secret_access_key"`
Region string `yaml:"region"`
Bucket string `yaml:"bucket"`
FolderName string `yaml:"folder_name"`
ContentType string `yaml:"content_type"`
}

View File

@ -18,4 +18,5 @@ config_aws:
secret_access_key: ~
region: ~
bucket: ~
folder_name: ~
content_type: image/jpeg

View File

@ -18,4 +18,5 @@ config_aws:
secret_access_key: ~
region: ~
bucket: ~
folder_name: ~
content_type: image/jpeg

View File

@ -8,5 +8,5 @@ create table users
user_photo_id varchar(100),
created_at timestamp with time zone,
updated_at timestamp with time zone,
constraint users_key unique(external_id, user_photo_url, user_photo_id)
constraint users_key unique(external_id)
);

View File

@ -1,6 +1,10 @@
package main
import "github.com/jinzhu/gorm"
import (
"time"
"github.com/jinzhu/gorm"
)
func getConnection(uid string) *Connection {
var connection Connection
@ -79,7 +83,7 @@ func getConnectionById(id int) *Connection {
return &connection
}
func (u *Users) saveUser() error {
func (u *Users) save() error {
return orm.DB.Save(u).Error
}
@ -89,3 +93,8 @@ func getUserByExternalID(eid int) *Users {
return &user
}
//Expired method
func (u *Users) Expired(updateInterval int) bool {
return time.Now().After(u.UpdatedAt.Add(time.Hour * time.Duration(updateInterval)))
}

View File

@ -115,6 +115,7 @@ func connectHandler(w http.ResponseWriter, r *http.Request) {
}
func addBotHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
setLocale(r.Header.Get("Accept-Language"))
body, err := ioutil.ReadAll(r.Body)
if err != nil {
@ -123,7 +124,6 @@ func addBotHandler(w http.ResponseWriter, r *http.Request) {
logger.Error(err.Error())
return
}
defer r.Body.Close()
var b Bot
@ -221,6 +221,7 @@ func addBotHandler(w http.ResponseWriter, r *http.Request) {
}
func activityBotHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
setLocale(r.Header.Get("Accept-Language"))
body, err := ioutil.ReadAll(r.Body)
if err != nil {
@ -229,7 +230,6 @@ func activityBotHandler(w http.ResponseWriter, r *http.Request) {
logger.Error(err.Error())
return
}
defer r.Body.Close()
var b Bot
@ -323,6 +323,7 @@ func settingsHandler(w http.ResponseWriter, r *http.Request, uid string) {
}
func saveHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
setLocale(r.Header.Get("Accept-Language"))
body, err := ioutil.ReadAll(r.Body)
@ -331,7 +332,6 @@ func saveHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_save"}), http.StatusInternalServerError)
return
}
defer r.Body.Close()
var c Connection
@ -368,6 +368,7 @@ func saveHandler(w http.ResponseWriter, r *http.Request) {
}
func createHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
setLocale(r.Header.Get("Accept-Language"))
body, err := ioutil.ReadAll(r.Body)
@ -377,7 +378,6 @@ func createHandler(w http.ResponseWriter, r *http.Request) {
logger.Error(err.Error())
return
}
defer r.Body.Close()
var c Connection
@ -487,6 +487,7 @@ func createHandler(w http.ResponseWriter, r *http.Request) {
}
func activityHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
setLocale(r.Header.Get("Accept-Language"))
w.Header().Set("Content-Type", "application/json")
res := Response{Success: false}
@ -516,7 +517,6 @@ func activityHandler(w http.ResponseWriter, r *http.Request) {
w.Write(jsonString)
return
}
defer r.Body.Close()
var rec v5.ActivityCallback

View File

@ -3,13 +3,12 @@ package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"time"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
@ -30,6 +29,7 @@ func GetBotName(bot *tgbotapi.BotAPI) string {
}
func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string) {
defer r.Body.Close()
b, err := getBotByToken(token)
if err != nil {
raven.CaptureErrorAndWait(err, nil)
@ -60,7 +60,6 @@ func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string
w.WriteHeader(http.StatusInternalServerError)
return
}
defer r.Body.Close()
if config.Debug {
logger.Debugf("telegramWebhookHandler: %v", string(bytes))
@ -76,7 +75,7 @@ func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string
user := getUserByExternalID(update.Message.From.ID)
if time.Now().After(user.UpdatedAt.Add(time.Hour*time.Duration(config.UpdateInterval))) || user.ID == 0 {
if user.Expired(config.UpdateInterval) || user.ID == 0 {
fileID, fileURL, err := GetFileIDAndURL(b.Token, update.Message.From.ID)
if err != nil {
@ -103,7 +102,7 @@ func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string
user.ExternalID = update.Message.From.ID
}
err = user.saveUser()
err = user.save()
if err != nil {
raven.CaptureErrorAndWait(err, nil)
logger.Error(err)
@ -182,6 +181,7 @@ func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string
}
func mgWebhookHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
bytes, err := ioutil.ReadAll(r.Body)
if err != nil {
raven.CaptureErrorAndWait(err, nil)
@ -189,7 +189,6 @@ func mgWebhookHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
return
}
defer r.Body.Close()
if config.Debug {
logger.Debugf("mgWebhookHandler request: %v", string(bytes))
@ -338,16 +337,15 @@ func UploadUserAvatar(url string) (picURLs3 string, err error) {
if err != nil {
return
}
defer resp.Body.Close()
if resp.StatusCode >= http.StatusBadRequest {
return "", errors.New(fmt.Sprintf("get: %v code: %v", url, resp.StatusCode))
}
defer resp.Body.Close()
result, err := uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(config.ConfigAWS.Bucket),
Key: aws.String(fmt.Sprintf("%v.jpg", GenerateToken())),
Key: aws.String(fmt.Sprintf("%v/%v.jpg", config.ConfigAWS.FolderName, GenerateToken())),
Body: resp.Body,
ContentType: aws.String(config.ConfigAWS.ContentType),
ACL: aws.String("public-read"),