2024-03-05 19:15:53 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
|
"github.com/minio/minio-go/v7"
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
|
"go-humas-be/app/module/article_files/mapper"
|
|
|
|
|
"go-humas-be/app/module/article_files/repository"
|
|
|
|
|
"go-humas-be/app/module/article_files/request"
|
|
|
|
|
"go-humas-be/app/module/article_files/response"
|
2024-03-06 14:00:43 +00:00
|
|
|
minioStorage "go-humas-be/config/config"
|
2024-03-05 19:15:53 +00:00
|
|
|
"go-humas-be/utils/paginator"
|
|
|
|
|
"io"
|
|
|
|
|
"log"
|
2024-05-07 17:56:49 +00:00
|
|
|
"math/rand"
|
2024-03-05 19:15:53 +00:00
|
|
|
"mime"
|
2024-03-31 23:25:59 +00:00
|
|
|
"path/filepath"
|
|
|
|
|
"strconv"
|
2024-03-05 19:15:53 +00:00
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// ArticleFilesService
|
|
|
|
|
type articleFilesService struct {
|
2024-03-06 14:00:43 +00:00
|
|
|
Repo repository.ArticleFilesRepository
|
|
|
|
|
Log zerolog.Logger
|
|
|
|
|
MinioStorage *minioStorage.MinioStorage
|
2024-03-05 19:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ArticleFilesService define interface of IArticleFilesService
|
|
|
|
|
type ArticleFilesService interface {
|
|
|
|
|
All(req request.ArticleFilesQueryRequest) (articleFiles []*response.ArticleFilesResponse, paging paginator.Pagination, err error)
|
|
|
|
|
Show(id uint) (articleFiles *response.ArticleFilesResponse, err error)
|
2024-03-31 23:25:59 +00:00
|
|
|
Save(c *fiber.Ctx, id uint) error
|
2024-03-05 19:15:53 +00:00
|
|
|
Update(id uint, req request.ArticleFilesUpdateRequest) (err error)
|
|
|
|
|
Delete(id uint) error
|
2024-05-07 17:56:49 +00:00
|
|
|
Viewer(c *fiber.Ctx) error
|
2024-03-05 19:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewArticleFilesService init ArticleFilesService
|
2024-03-06 14:00:43 +00:00
|
|
|
func NewArticleFilesService(repo repository.ArticleFilesRepository, log zerolog.Logger, minioStorage *minioStorage.MinioStorage) ArticleFilesService {
|
2024-03-05 19:15:53 +00:00
|
|
|
|
|
|
|
|
return &articleFilesService{
|
2024-03-06 14:00:43 +00:00
|
|
|
Repo: repo,
|
|
|
|
|
Log: log,
|
|
|
|
|
MinioStorage: minioStorage,
|
2024-03-05 19:15:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// All implement interface of ArticleFilesService
|
|
|
|
|
func (_i *articleFilesService) All(req request.ArticleFilesQueryRequest) (articleFiless []*response.ArticleFilesResponse, paging paginator.Pagination, err error) {
|
|
|
|
|
results, paging, err := _i.Repo.GetAll(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, result := range results {
|
|
|
|
|
articleFiless = append(articleFiless, mapper.ArticleFilesResponseMapper(result))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (_i *articleFilesService) Show(id uint) (articleFiles *response.ArticleFilesResponse, err error) {
|
|
|
|
|
result, err := _i.Repo.FindOne(id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return mapper.ArticleFilesResponseMapper(result), nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-31 23:25:59 +00:00
|
|
|
func (_i *articleFilesService) Save(c *fiber.Ctx, id uint) (err error) {
|
2024-03-06 14:00:43 +00:00
|
|
|
bucketName := _i.MinioStorage.Cfg.ObjectStorage.MinioStorage.BucketName
|
2024-03-05 19:15:53 +00:00
|
|
|
|
|
|
|
|
form, err := c.MultipartForm()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
files := form.File["files"]
|
|
|
|
|
|
|
|
|
|
// Create minio connection.
|
2024-03-06 14:00:43 +00:00
|
|
|
minioClient, err := _i.MinioStorage.ConnectMinio()
|
2024-03-05 19:15:53 +00:00
|
|
|
if err != nil {
|
|
|
|
|
// Return status 500 and minio connection error.
|
|
|
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
|
|
|
|
"error": true,
|
|
|
|
|
"msg": err.Error(),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Iterasi semua file yang diunggah
|
|
|
|
|
for _, file := range files {
|
|
|
|
|
|
|
|
|
|
_i.Log.Info().Str("timestamp", time.Now().
|
|
|
|
|
Format(time.RFC3339)).Str("Service:Resource", "Uploader:: loop1").
|
|
|
|
|
Interface("data", file).Msg("")
|
|
|
|
|
|
|
|
|
|
src, err := file.Open()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer src.Close()
|
|
|
|
|
|
2024-05-07 17:56:49 +00:00
|
|
|
filename := filepath.Base(file.Filename)
|
|
|
|
|
filenameAlt := filepath.Clean(filename[:len(filename)-len(filepath.Ext(filename))])
|
|
|
|
|
filename = strings.ReplaceAll(filename, " ", "")
|
|
|
|
|
filenameWithoutExt := filepath.Clean(filename[:len(filename)-len(filepath.Ext(filename))])
|
|
|
|
|
extension := filepath.Ext(file.Filename)[1:]
|
|
|
|
|
|
|
|
|
|
rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
|
randUniqueId := rand.Intn(1000000)
|
|
|
|
|
|
|
|
|
|
newFilenameWithoutExt := filenameWithoutExt + "_" + strconv.Itoa(randUniqueId)
|
|
|
|
|
newFilename := newFilenameWithoutExt + "." + extension
|
|
|
|
|
|
|
|
|
|
objectName := "articles/upload/" + newFilename
|
2024-05-07 07:48:46 +00:00
|
|
|
fileSize := strconv.FormatInt(file.Size, 10)
|
2024-03-31 23:25:59 +00:00
|
|
|
|
|
|
|
|
req := request.ArticleFilesCreateRequest{
|
|
|
|
|
ArticleId: id,
|
2024-05-07 07:48:46 +00:00
|
|
|
FilePath: &objectName,
|
2024-05-07 17:56:49 +00:00
|
|
|
FileName: &newFilename,
|
|
|
|
|
FileAlt: &filenameAlt,
|
2024-05-07 07:48:46 +00:00
|
|
|
Size: &fileSize,
|
2024-03-31 23:25:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = _i.Repo.Create(req.ToEntity())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-05 19:15:53 +00:00
|
|
|
// Upload file ke MinIO
|
2024-03-31 23:25:59 +00:00
|
|
|
_, err = minioClient.PutObject(context.Background(), bucketName, objectName, src, file.Size, minio.PutObjectOptions{})
|
2024-03-05 19:15:53 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_i.Log.Info().Str("timestamp", time.Now().
|
|
|
|
|
Format(time.RFC3339)).Str("Service:Resource", "User:All").
|
|
|
|
|
Interface("data", "Successfully uploaded").Msg("")
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-31 23:25:59 +00:00
|
|
|
func (_i *articleFilesService) Update(id uint, req request.ArticleFilesUpdateRequest) (err error) {
|
|
|
|
|
_i.Log.Info().Interface("data", req).Msg("")
|
|
|
|
|
return _i.Repo.Update(id, req.ToEntity())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (_i *articleFilesService) Delete(id uint) error {
|
2024-04-16 02:08:00 +00:00
|
|
|
result, err := _i.Repo.FindOne(id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
result.IsActive = false
|
|
|
|
|
return _i.Repo.Update(id, result)
|
2024-03-31 23:25:59 +00:00
|
|
|
}
|
|
|
|
|
|
2024-05-07 17:56:49 +00:00
|
|
|
func (_i *articleFilesService) Viewer(c *fiber.Ctx) (err error) {
|
|
|
|
|
filename := c.Params("filename")
|
|
|
|
|
result, err := _i.Repo.FindByFilename(filename)
|
2024-03-31 23:25:59 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-05 19:15:53 +00:00
|
|
|
ctx := context.Background()
|
2024-03-06 14:00:43 +00:00
|
|
|
bucketName := _i.MinioStorage.Cfg.ObjectStorage.MinioStorage.BucketName
|
2024-05-07 07:48:46 +00:00
|
|
|
objectName := *result.FilePath
|
2024-03-05 19:15:53 +00:00
|
|
|
|
|
|
|
|
_i.Log.Info().Str("timestamp", time.Now().
|
|
|
|
|
Format(time.RFC3339)).Str("Service:Resource", "Article:Uploads").
|
|
|
|
|
Interface("data", objectName).Msg("")
|
|
|
|
|
|
|
|
|
|
// Create minio connection.
|
2024-03-06 14:00:43 +00:00
|
|
|
minioClient, err := _i.MinioStorage.ConnectMinio()
|
2024-03-05 19:15:53 +00:00
|
|
|
if err != nil {
|
|
|
|
|
// Return status 500 and minio connection error.
|
|
|
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
|
|
|
|
"error": true,
|
|
|
|
|
"msg": err.Error(),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fileContent, err := minioClient.GetObject(ctx, bucketName, objectName, minio.GetObjectOptions{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalln(err)
|
|
|
|
|
}
|
|
|
|
|
defer fileContent.Close()
|
|
|
|
|
|
|
|
|
|
// Tentukan Content-Type berdasarkan ekstensi file
|
|
|
|
|
contentType := mime.TypeByExtension("." + getFileExtension(objectName))
|
|
|
|
|
if contentType == "" {
|
|
|
|
|
contentType = "application/octet-stream" // fallback jika tidak ada tipe MIME yang cocok
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.Set("Content-Type", contentType)
|
|
|
|
|
|
|
|
|
|
if _, err := io.Copy(c.Response().BodyWriter(), fileContent); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getFileExtension(filename string) string {
|
|
|
|
|
// split file name
|
|
|
|
|
parts := strings.Split(filename, ".")
|
|
|
|
|
|
|
|
|
|
// jika tidak ada ekstensi, kembalikan string kosong
|
|
|
|
|
if len(parts) == 1 || (len(parts) == 2 && parts[0] == "") {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ambil ekstensi terakhir
|
|
|
|
|
return parts[len(parts)-1]
|
|
|
|
|
}
|