add folder name for s3 to config
This commit is contained in:
parent
9636237212
commit
1fff569009
@ -25,6 +25,7 @@ type ConfigAWS struct {
|
|||||||
SecretAccessKey string `yaml:"secret_access_key"`
|
SecretAccessKey string `yaml:"secret_access_key"`
|
||||||
Region string `yaml:"region"`
|
Region string `yaml:"region"`
|
||||||
Bucket string `yaml:"bucket"`
|
Bucket string `yaml:"bucket"`
|
||||||
|
FolderName string `yaml:"folder_name"`
|
||||||
ContentType string `yaml:"content_type"`
|
ContentType string `yaml:"content_type"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,4 +18,5 @@ config_aws:
|
|||||||
secret_access_key: ~
|
secret_access_key: ~
|
||||||
region: ~
|
region: ~
|
||||||
bucket: ~
|
bucket: ~
|
||||||
|
folder_name: ~
|
||||||
content_type: image/jpeg
|
content_type: image/jpeg
|
||||||
|
@ -18,4 +18,5 @@ config_aws:
|
|||||||
secret_access_key: ~
|
secret_access_key: ~
|
||||||
region: ~
|
region: ~
|
||||||
bucket: ~
|
bucket: ~
|
||||||
|
folder_name: ~
|
||||||
content_type: image/jpeg
|
content_type: image/jpeg
|
||||||
|
@ -8,5 +8,5 @@ create table users
|
|||||||
user_photo_id varchar(100),
|
user_photo_id varchar(100),
|
||||||
created_at timestamp with time zone,
|
created_at timestamp with time zone,
|
||||||
updated_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)
|
||||||
);
|
);
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "github.com/jinzhu/gorm"
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/jinzhu/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
func getConnection(uid string) *Connection {
|
func getConnection(uid string) *Connection {
|
||||||
var connection Connection
|
var connection Connection
|
||||||
@ -79,7 +83,7 @@ func getConnectionById(id int) *Connection {
|
|||||||
return &connection
|
return &connection
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *Users) saveUser() error {
|
func (u *Users) save() error {
|
||||||
return orm.DB.Save(u).Error
|
return orm.DB.Save(u).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,3 +93,8 @@ func getUserByExternalID(eid int) *Users {
|
|||||||
|
|
||||||
return &user
|
return &user
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Expired method
|
||||||
|
func (u *Users) Expired(updateInterval int) bool {
|
||||||
|
return time.Now().After(u.UpdatedAt.Add(time.Hour * time.Duration(updateInterval)))
|
||||||
|
}
|
||||||
|
10
routing.go
10
routing.go
@ -115,6 +115,7 @@ func connectHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func addBotHandler(w http.ResponseWriter, r *http.Request) {
|
func addBotHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
defer r.Body.Close()
|
||||||
setLocale(r.Header.Get("Accept-Language"))
|
setLocale(r.Header.Get("Accept-Language"))
|
||||||
body, err := ioutil.ReadAll(r.Body)
|
body, err := ioutil.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -123,7 +124,6 @@ func addBotHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
logger.Error(err.Error())
|
logger.Error(err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer r.Body.Close()
|
|
||||||
|
|
||||||
var b Bot
|
var b Bot
|
||||||
|
|
||||||
@ -221,6 +221,7 @@ func addBotHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func activityBotHandler(w http.ResponseWriter, r *http.Request) {
|
func activityBotHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
defer r.Body.Close()
|
||||||
setLocale(r.Header.Get("Accept-Language"))
|
setLocale(r.Header.Get("Accept-Language"))
|
||||||
body, err := ioutil.ReadAll(r.Body)
|
body, err := ioutil.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -229,7 +230,6 @@ func activityBotHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
logger.Error(err.Error())
|
logger.Error(err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer r.Body.Close()
|
|
||||||
|
|
||||||
var b Bot
|
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) {
|
func saveHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
defer r.Body.Close()
|
||||||
setLocale(r.Header.Get("Accept-Language"))
|
setLocale(r.Header.Get("Accept-Language"))
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(r.Body)
|
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)
|
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_save"}), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer r.Body.Close()
|
|
||||||
|
|
||||||
var c Connection
|
var c Connection
|
||||||
|
|
||||||
@ -368,6 +368,7 @@ func saveHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func createHandler(w http.ResponseWriter, r *http.Request) {
|
func createHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
defer r.Body.Close()
|
||||||
setLocale(r.Header.Get("Accept-Language"))
|
setLocale(r.Header.Get("Accept-Language"))
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(r.Body)
|
body, err := ioutil.ReadAll(r.Body)
|
||||||
@ -377,7 +378,6 @@ func createHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
logger.Error(err.Error())
|
logger.Error(err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer r.Body.Close()
|
|
||||||
|
|
||||||
var c Connection
|
var c Connection
|
||||||
|
|
||||||
@ -487,6 +487,7 @@ func createHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func activityHandler(w http.ResponseWriter, r *http.Request) {
|
func activityHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
defer r.Body.Close()
|
||||||
setLocale(r.Header.Get("Accept-Language"))
|
setLocale(r.Header.Get("Accept-Language"))
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
res := Response{Success: false}
|
res := Response{Success: false}
|
||||||
@ -516,7 +517,6 @@ func activityHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.Write(jsonString)
|
w.Write(jsonString)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer r.Body.Close()
|
|
||||||
|
|
||||||
var rec v5.ActivityCallback
|
var rec v5.ActivityCallback
|
||||||
|
|
||||||
|
16
telegram.go
16
telegram.go
@ -3,13 +3,12 @@ package main
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go/aws"
|
"github.com/aws/aws-sdk-go/aws"
|
||||||
"github.com/aws/aws-sdk-go/aws/credentials"
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
||||||
"github.com/aws/aws-sdk-go/aws/session"
|
"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) {
|
func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string) {
|
||||||
|
defer r.Body.Close()
|
||||||
b, err := getBotByToken(token)
|
b, err := getBotByToken(token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
raven.CaptureErrorAndWait(err, nil)
|
raven.CaptureErrorAndWait(err, nil)
|
||||||
@ -60,7 +60,6 @@ func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string
|
|||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer r.Body.Close()
|
|
||||||
|
|
||||||
if config.Debug {
|
if config.Debug {
|
||||||
logger.Debugf("telegramWebhookHandler: %v", string(bytes))
|
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)
|
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)
|
fileID, fileURL, err := GetFileIDAndURL(b.Token, update.Message.From.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -103,7 +102,7 @@ func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string
|
|||||||
user.ExternalID = update.Message.From.ID
|
user.ExternalID = update.Message.From.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
err = user.saveUser()
|
err = user.save()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
raven.CaptureErrorAndWait(err, nil)
|
raven.CaptureErrorAndWait(err, nil)
|
||||||
logger.Error(err)
|
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) {
|
func mgWebhookHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
defer r.Body.Close()
|
||||||
bytes, err := ioutil.ReadAll(r.Body)
|
bytes, err := ioutil.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
raven.CaptureErrorAndWait(err, nil)
|
raven.CaptureErrorAndWait(err, nil)
|
||||||
@ -189,7 +189,6 @@ func mgWebhookHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer r.Body.Close()
|
|
||||||
|
|
||||||
if config.Debug {
|
if config.Debug {
|
||||||
logger.Debugf("mgWebhookHandler request: %v", string(bytes))
|
logger.Debugf("mgWebhookHandler request: %v", string(bytes))
|
||||||
@ -338,16 +337,15 @@ func UploadUserAvatar(url string) (picURLs3 string, err error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode >= http.StatusBadRequest {
|
if resp.StatusCode >= http.StatusBadRequest {
|
||||||
return "", errors.New(fmt.Sprintf("get: %v code: %v", url, resp.StatusCode))
|
return "", errors.New(fmt.Sprintf("get: %v code: %v", url, resp.StatusCode))
|
||||||
}
|
}
|
||||||
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
result, err := uploader.Upload(&s3manager.UploadInput{
|
result, err := uploader.Upload(&s3manager.UploadInput{
|
||||||
Bucket: aws.String(config.ConfigAWS.Bucket),
|
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,
|
Body: resp.Body,
|
||||||
ContentType: aws.String(config.ConfigAWS.ContentType),
|
ContentType: aws.String(config.ConfigAWS.ContentType),
|
||||||
ACL: aws.String("public-read"),
|
ACL: aws.String("public-read"),
|
||||||
|
Loading…
Reference in New Issue
Block a user