diff --git a/config/config.development.yml b/config/config.development.yml index c70c122..98787b6 100644 --- a/config/config.development.yml +++ b/config/config.development.yml @@ -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 diff --git a/config/config.go b/config/config.go index 7ae71be..f8e808c 100644 --- a/config/config.go +++ b/config/config.go @@ -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"` diff --git a/delivery/grpc/grpc_server.go b/delivery/grpc/grpc_server.go index 2d23706..165df5a 100644 --- a/delivery/grpc/grpc_server.go +++ b/delivery/grpc/grpc_server.go @@ -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) diff --git a/go.mod b/go.mod index dafb062..e2f81b9 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/pkg/object_storage/errors.go b/pkg/object_storage/errors.go new file mode 100644 index 0000000..a2d8509 --- /dev/null +++ b/pkg/object_storage/errors.go @@ -0,0 +1,8 @@ +package object_storage + +import "errors" + +var ( + ErrMaxFilesizeExceeded = errors.New("max file size exceeded") + ErrInvalidFileFormat = errors.New("invalid file format") +) diff --git a/pkg/object_storage/object_storage.go b/pkg/object_storage/object_storage.go new file mode 100644 index 0000000..977fe0e --- /dev/null +++ b/pkg/object_storage/object_storage.go @@ -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 +} diff --git a/pkg/uploader/uploader.go b/pkg/uploader/uploader.go deleted file mode 100644 index 6f823f3..0000000 --- a/pkg/uploader/uploader.go +++ /dev/null @@ -1,97 +0,0 @@ -package uploader - -import ( - "context" - "errors" - "fmt" - "os" - "path/filepath" - - "github.com/google/uuid" - "github.com/kavkaco/Kavka-Core/config" - "github.com/minio/minio-go/v7" - "github.com/minio/minio-go/v7/pkg/credentials" -) - -const ( - UPLOAD_TMP_DIR = "/tmp/uploads" -) - -var ErrMaxFileSize = errors.New("maximum file size") - -type ( - Service struct{ minioClient *minio.Client } - FileUploaded struct { - Name string - Size int64 - } -) - -func NewUploaderService(config *config.Config) *Service { - minioCredentials := config.MinIO - - endpoint := minioCredentials.Endpoint - accessKeyID := minioCredentials.AccessKey - secretAccessKey := minioCredentials.SecretKey - - // Initialize minio client object. - minioClient, err := minio.New(endpoint, &minio.Options{ - Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""), - Secure: false, - }) - if err != nil { - panic(err) - } - - return &Service{minioClient} -} - -func (s *Service) UploadFile(bucketName string, filePath string, maxFileSize *int64) (*FileUploaded, error) { - // Collect objectName, contentType and filePath - fileInfo, statErr := os.Stat(filePath) - if statErr != nil { - return nil, statErr - } - - if maxFileSize != nil { - if fileInfo.Size() > *maxFileSize { - return nil, ErrMaxFileSize - } - } - - objectNameUUID, err := uuid.NewV7() - if err != nil { - return nil, err - } - objectName := objectNameUUID.String() - - contentType := filepath.Ext(filePath) - - // Upload the file - _, err = s.minioClient.FPutObject(context.Background(), bucketName, - objectName, filePath, minio.PutObjectOptions{ContentType: contentType}) - if err != nil { - return nil, err - } - - return &FileUploaded{ - Name: objectName, - Size: fileInfo.Size(), - }, err -} - -func (s *Service) DeleteFile(bucketName string, objectName string) error { - // Delete the file - opts := minio.RemoveObjectOptions{GovernanceBypass: true} - - err := s.minioClient.RemoveObject(context.Background(), bucketName, objectName, opts) - if err != nil { - return err - } - - return nil -} - -func (s *Service) GenerateTMPFilePath(fileName string) string { - return fmt.Sprintf("%s/..%s/%s", config.ConfigsDirPath(), UPLOAD_TMP_DIR, fileName) -} diff --git a/tests/integration/repository/setup_test.go b/tests/integration/repository/setup_test.go index e4af221..5759a5e 100644 --- a/tests/integration/repository/setup_test.go +++ b/tests/integration/repository/setup_test.go @@ -1,7 +1,6 @@ package repository import ( - "fmt" "testing" "github.com/kavkaco/Kavka-Core/database" @@ -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()