Merge branch 'cleanscript' into 'dev'

Cleanscript

See merge request external/heruvym!67
This commit is contained in:
Mikhail 2024-09-24 18:23:52 +00:00
commit 2fb7cd5a02
8 changed files with 190 additions and 6 deletions

@ -119,7 +119,7 @@ func New(ctx context.Context, opts interface{}, ver appInit.Version) (appInit.Co
return nil, err
}
miniostore, err := minio.New(ctx, logger, options.MinioEndpoint, options.MinioSecretKey, options.MinioAccessKey, options.MinioToken, options.MinioRegion, false)
miniostore, err := minio.New(ctx, logger, options.MinioEndpoint, options.MinioAccessKey, options.MinioSecretKey, options.MinioToken, options.MinioRegion, false)
if err != nil {
fmt.Println(err)
return nil, err

@ -24,8 +24,7 @@ func New(ctx context.Context, logger hlog.Logger,
conn, err := minio.New(endpoint,
&minio.Options{
Creds: credentials.NewStaticV4(keyID, accessKey, token),
Secure: useSSL,
Region: region,
Secure: false,
},
)
if err != nil {

@ -249,6 +249,7 @@ func (d *DAL) CreateTicket(
ctx context.Context,
userID,
sessionID,
origin,
title, message string,
files []string,
) (string, error) {
@ -264,6 +265,7 @@ func (d *DAL) CreateTicket(
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
Rate: -1,
Origin: origin,
TopMessage: model.Message{
ID: xid.New().String(),
UserID: userID,

@ -22,6 +22,9 @@ services:
- BB_MONGO_URI=mongodb://$MONGO_USER:$MONGO_PASSWORD@10.8.0.8:27017/?authSource=support
- TELEGRAM_TOKEN=6414077478:AAFk03HezovLT2kO_i9OYswH8Weirsgp9GU
- TELEGRAM_CHAT_ID=1001802261459
- REDIS_HOST=10.8.0.9:6379
- REDIS_PASSWORD=Redalert2
- REDIS_DB=4
networks:
backend_external:
driver: bridge

@ -23,6 +23,9 @@ services:
- JWT_SECRET=$JWT_SECRET
- TELEGRAM_TOKEN=6414077478:AAFk03HezovLT2kO_i9OYswH8Weirsgp9GU
- TELEGRAM_CHAT_ID=1002089014760
- REDIS_HOST=10.8.0.6:6379
- REDIS_PASSWORD=Redalert2
- REDIS_DB=4
networks:
backend_external:
driver: bridge

@ -28,6 +28,7 @@ type Ticket struct {
UserID string `bson:"UserID" json:"user"`
SessionID string `bson:"SessionID" json:"sess"`
AnswererID string `bson:"AnswererID" json:"ans"`
Origin string `bson:"origin" json:"origin"`
State string `bson:"State" json:"state"`
TopMessage Message `bson:"TopMessage" json:"top_message"`

118
script/main.go Normal file

@ -0,0 +1,118 @@
package main
import (
"context"
"fmt"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"heruvym/model"
)
const (
bucketName = "videodata"
minioEndpoint = "localhost:9000"
accessKeyID = "admin"
secretAccessKey = "admin123"
mongoURI = "mongodb://test:test@localhost:27024/?authSource=admin"
mongoDBName = "admin"
collectionName = "verification"
)
func main() {
ctx := context.Background()
clientOptions := options.Client().ApplyURI(mongoURI)
client, err := mongo.Connect(ctx, clientOptions)
if err != nil {
fmt.Println("Ошибка подключения к монго:", err)
return
}
defer client.Disconnect(ctx)
collection := client.Database(mongoDBName).Collection(collectionName)
minioClient, err := minio.New(minioEndpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
Secure: false,
})
if err != nil {
fmt.Println("Ошибка подключения к минио:", err)
return
}
allFiles, err := getAllFilesInBucket(ctx, minioClient)
if err != nil {
fmt.Println("Ошибка при получении списка файлов в бакете:", err)
return
}
messages, err := getMessagesRecords(ctx, collection)
if err != nil {
fmt.Println("Ошибка при получении записей из монго:", err)
return
}
for _, file := range allFiles {
if !checkContains(messages, file.Key) {
fmt.Println("Удаление файла:", file.Key)
err := deleteFileFromBucket(ctx, minioClient, file.Key)
if err != nil {
fmt.Println("Ошибка при удалении файла:", err)
}
}
}
}
// получение всех файлов из минио по бакету
func getAllFilesInBucket(ctx context.Context, client *minio.Client) ([]minio.ObjectInfo, error) {
var files []minio.ObjectInfo
objectCh := client.ListObjects(ctx, bucketName, minio.ListObjectsOptions{})
for object := range objectCh {
if object.Err != nil {
return nil, object.Err
}
files = append(files, object)
}
return files, nil
}
// удаление файла из минио
func deleteFileFromBucket(ctx context.Context, client *minio.Client, fileName string) error {
err := client.RemoveObject(ctx, bucketName, fileName, minio.RemoveObjectOptions{})
return err
}
// получение списка сообщений из монго
func getMessagesRecords(ctx context.Context, collection *mongo.Collection) ([]model.Message, error) {
var records []model.Message
cursor, err := collection.Find(ctx, bson.M{})
if err != nil {
return nil, err
}
defer cursor.Close(ctx)
for cursor.Next(ctx) {
var record model.Message
if err := cursor.Decode(&record); err != nil {
return nil, err
}
records = append(records, record)
}
if err := cursor.Err(); err != nil {
return nil, err
}
return records, nil
}
// проверка на существования файла минио в монге по имени
func checkContains(messages []model.Message, fileKey string) bool {
for _, message := range messages {
for _, file := range message.Files {
if file == fileKey {
return true
}
}
}
return false
}

@ -131,6 +131,7 @@ func (h *Heruvym) CreateTicket(w http.ResponseWriter, r *http.Request) {
ctx,
session.Id,
session.Id,
r.Header["Origin"][0],
request.Title,
request.Message,
[]string{},
@ -157,7 +158,7 @@ func (h *Heruvym) CreateTicket(w http.ResponseWriter, r *http.Request) {
}
go func() {
if session.Id != "" && role == "" {
if session.Id != "" && role != "admin" {
if err == nil && h.notifier != nil {
var userLink, supportLink string
if session.StandardClaims.Issuer != "" {
@ -170,6 +171,11 @@ func (h *Heruvym) CreateTicket(w http.ResponseWriter, r *http.Request) {
supportLink = fmt.Sprintf("https://admin.pena/support/%s", ticketID)
}
} else {
if domain[0] == 's' {
supportLink = fmt.Sprintf("https://sadmin.pena/support/%s", ticketID)
} else {
supportLink = fmt.Sprintf("https://admin.pena/support/%s", ticketID)
}
userLink = "незарегистрированного пользователя"
}
@ -349,7 +355,7 @@ func (h *Heruvym) PutMessage(
role := jwt_adapter.GetRole(ctx)
go func() {
if sess.Id != "" && role == "" {
if sess.Id != "" && role != "admin" {
if err == nil && h.notifier != nil {
var userLink,supportLink string
if sess.StandardClaims.Issuer != "" {
@ -362,6 +368,11 @@ func (h *Heruvym) PutMessage(
supportLink = fmt.Sprintf("https://admin.pena/support/%s", request.TicketID)
}
} else {
if domain[0] == 's' {
supportLink = fmt.Sprintf("https://sadmin.pena/support/%s", request.TicketID)
} else {
supportLink = fmt.Sprintf("https://admin.pena/support/%s", request.TicketID)
}
userLink = "незарегистрированного пользователя"
}
@ -710,7 +721,7 @@ func (h *Heruvym) PutFile(w http.ResponseWriter, r *http.Request) {
}
TimeKey := fmt.Sprintf("sendLockHeruvym:%s", sess.Id)
isNewKey, err := h.redisClient.SetNX(r.Context(), TimeKey, time.Now().Unix(), 5*time.Minute).Result()
isNewKey, err := h.redisClient.SetNX(r.Context(), TimeKey, time.Now().Unix(), 30*time.Second).Result()
if err != nil {
fmt.Println("failed check last upload time in Redis:", err)
w.WriteHeader(http.StatusInternalServerError)
@ -856,6 +867,10 @@ func (h *Heruvym) PutFile(w http.ResponseWriter, r *http.Request) {
if _, err := w.Write(resp); err != nil {
fmt.Println("CAN NOT WRITE", err)
}
m.Lock()
defer m.Unlock()
fileIDs = append(fileIDs, filename)
filenames = append(filenames, name)
return
}
@ -874,6 +889,10 @@ func (h *Heruvym) PutFile(w http.ResponseWriter, r *http.Request) {
}()
}
wg.Wait()
if len(fileIDs) == 0 {
return
}
if errFile != nil {
w.WriteHeader(http.StatusInternalServerError)
@ -914,6 +933,45 @@ func (h *Heruvym) PutFile(w http.ResponseWriter, r *http.Request) {
}
return
}
ctx := r.Context()
domain := ctx.Value(middleware.HostKey).(string)
if domain == "" {
fmt.Println("domain is nil err")
}
role := jwt_adapter.GetRole(ctx)
go func() {
if sess.Id != "" && role != "admin" {
if err == nil && h.notifier != nil {
var userLink, supportLink string
if sess.StandardClaims.Issuer != "" {
fmt.Println("MABNAT", domain)
if domain[0] == 's' {
userLink = fmt.Sprintf("https://sadmin.pena/users/%s", sess.Id)
supportLink = fmt.Sprintf("https://sadmin.pena/support/%s", req.Ticket)
} else {
userLink = fmt.Sprintf("https://admin.pena/users/%s", sess.Id)
supportLink = fmt.Sprintf("https://admin.pena/support/%s", req.Ticket)
}
} else {
if domain[0] == 's' {
supportLink = fmt.Sprintf("https://sadmin.pena/support/%s", req.Ticket)
} else {
supportLink = fmt.Sprintf("https://admin.pena/support/%s", req.Ticket)
}
userLink = "незарегистрированного пользователя"
}
message := fmt.Sprintf("Вам пришло сообщение от %s сссылка на пользователя с %s, ccылка на чат - %s",
userLink, domain, supportLink)
if _, err := h.notifier.Send(tb.ChatID(h.tgChatID), message); err != nil {
fmt.Println("CAN NOT NOTIFY", err)
}
return
}
}
}()
if _, err := w.Write(resp); err != nil {
fmt.Println("CAN NOT WRITE", err)