Skip to content

Commit

Permalink
refactor: object_storage library improved and also a little changes a…
Browse files Browse the repository at this point in the history
…t configs
  • Loading branch information
tahadostifam committed Dec 1, 2024
1 parent c49e9f9 commit cf16959
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 107 deletions.
8 changes: 5 additions & 3 deletions config/config.development.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ redis:
db: 0

minio:
endpoint: 127.0.0.1:9000
access_key: uD3h2W3CONpYe8Tb4nbp
secret_key: fKF98iAk1eDSpdtdJWcJHgivjpF4Su4HCdgkpYvQ
url: localhost:9000
access_key: tC3RlaTuUcoZDnJS21Tw
secret_key: MXwXGlfLABWHHCH8CIqNWxb9UDASLRpm9zpuBale
api: s3v4
path: aut

logger:
file_name: kavka.logs
Expand Down
5 changes: 3 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,12 @@ type (
}

MinIO struct {
Endpoint string `koanf:"endpoint"`
Url string `koanf:"url"`
AccessKey string `koanf:"access_key"`
SecretKey string `koanf:"secret_key"`
Api string `koanf:"api"`
Path string `koanf:"path"`
}

Email struct {
SenderEmail string `koanf:"sender_email"`
Password string `koanf:"password"`
Expand Down
1 change: 0 additions & 1 deletion delivery/grpc/grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func NewGrpcServer(cfg *config.HTTP, router *http.ServeMux, services *Services)
searchGrpcHandler := grpc_handlers.NewSearchGrpcHandler(log.NewSubLogger("message-handler"), services.SearchService)
searchGrpcRoute, searchGrpcRouter := searchv1connect.NewSearchServiceHandler(searchGrpcHandler, interceptors)

fmt.Println(authGrpcRoute)
router.Handle(authGrpcRoute, authGrpcRouter)
router.Handle(chatGrpcRoute, chatGrpcRouter)
router.Handle(eventsGrpcRoute, eventsGrpcRouter)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ require (
github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.6.0
github.com/google/uuid v1.6.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
Expand Down
8 changes: 8 additions & 0 deletions pkg/object_storage/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package object_storage

import "errors"

var (
ErrMaxFilesizeExceeded = errors.New("max file size exceeded")
ErrInvalidFileFormat = errors.New("invalid file format")
)
87 changes: 87 additions & 0 deletions pkg/object_storage/object_storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package object_storage

import (
"bytes"
"context"
"os"

"github.com/kavkaco/Kavka-Core/config"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)

const TEMP_DIR = "tmp"

type Object struct {
Name string
Size int
}

type Service struct {
minioClient *minio.Client
TempDirPath string
}

func New(cfg *config.Config) (*Service, error) {
minioClient, err := minio.New(cfg.MinIO.Url, &minio.Options{
Creds: credentials.NewStaticV4(cfg.MinIO.AccessKey, cfg.MinIO.SecretKey, ""),
Secure: false,
})
if err != nil {
return nil, err
}

wd, err := os.Getwd()
if err != nil {
return nil, err
}

tempDirPath := wd + "/" + TEMP_DIR + "/"

return &Service{minioClient, tempDirPath}, nil
}

func (s *Service) Upload(ctx context.Context, bucketName string, objectName string, fileData []byte, fileSize int64, contentType string) (*Object, error) {
br := bytes.NewReader(fileData)

// Upload the file
_, err := s.minioClient.PutObject(ctx, bucketName,
objectName, br, fileSize, minio.PutObjectOptions{
ContentType: contentType,
})
if err != nil {
return nil, err
}

return &Object{
Name: objectName,
Size: len(fileData),
}, err
}

func (s *Service) Delete(ctx context.Context, bucketName string, objectName string) error {
opts := minio.RemoveObjectOptions{GovernanceBypass: true}

err := s.minioClient.RemoveObject(ctx, bucketName, objectName, opts)
if err != nil {
return err
}

return nil
}

func (s *Service) Get(ctx context.Context, bucketName string, objectName string) (string, string, error) {
filePath := s.TempDirPath + objectName

err := s.minioClient.FGetObject(ctx, bucketName, objectName, filePath, minio.GetObjectOptions{})
if err != nil {
return "", "", err
}

stat, err := s.minioClient.StatObject(ctx, bucketName, objectName, minio.GetObjectOptions{})
if err != nil {
return "", "", err
}

return filePath, stat.ContentType, nil
}
97 changes: 0 additions & 97 deletions pkg/uploader/uploader.go

This file was deleted.

3 changes: 0 additions & 3 deletions tests/integration/repository/setup_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package repository

import (
"fmt"
"testing"

"github.com/kavkaco/Kavka-Core/database"
Expand All @@ -12,8 +11,6 @@ var db *mongo.Database

func TestMain(m *testing.M) {
database.GetMongoDBTestInstance(func(_db *mongo.Database) {
fmt.Print("\n")

db = _db

m.Run()
Expand Down

0 comments on commit cf16959

Please sign in to comment.