From 1fff569009b9da38f4690af021bd8cc4207acb9f Mon Sep 17 00:00:00 2001 From: DmitryZagorulko Date: Fri, 8 Jun 2018 13:57:51 +0300 Subject: [PATCH] add folder name for s3 to config --- config.go | 1 + config.yml.dist | 1 + config_test.yml.dist | 1 + migrations/1528208070_app.up.sql | 2 +- repository.go | 13 +++++++++++-- routing.go | 10 +++++----- telegram.go | 16 +++++++--------- 7 files changed, 27 insertions(+), 17 deletions(-) diff --git a/config.go b/config.go index 6b8c6af..917e0dd 100644 --- a/config.go +++ b/config.go @@ -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"` } diff --git a/config.yml.dist b/config.yml.dist index 0618529..04b3e09 100644 --- a/config.yml.dist +++ b/config.yml.dist @@ -18,4 +18,5 @@ config_aws: secret_access_key: ~ region: ~ bucket: ~ + folder_name: ~ content_type: image/jpeg diff --git a/config_test.yml.dist b/config_test.yml.dist index 113781d..208cc8f 100644 --- a/config_test.yml.dist +++ b/config_test.yml.dist @@ -18,4 +18,5 @@ config_aws: secret_access_key: ~ region: ~ bucket: ~ + folder_name: ~ content_type: image/jpeg diff --git a/migrations/1528208070_app.up.sql b/migrations/1528208070_app.up.sql index ebaf04d..ff491f5 100644 --- a/migrations/1528208070_app.up.sql +++ b/migrations/1528208070_app.up.sql @@ -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) ); diff --git a/repository.go b/repository.go index 615cec0..24b22b6 100644 --- a/repository.go +++ b/repository.go @@ -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))) +} diff --git a/routing.go b/routing.go index 998f690..cfd578e 100644 --- a/routing.go +++ b/routing.go @@ -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 diff --git a/telegram.go b/telegram.go index 1846469..c531c2d 100644 --- a/telegram.go +++ b/telegram.go @@ -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"),