2024-03-05 19:15:53 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
2024-04-28 18:39:43 +00:00
|
|
|
"context"
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
|
"github.com/minio/minio-go/v7"
|
2024-03-05 19:15:53 +00:00
|
|
|
"github.com/rs/zerolog"
|
2024-05-28 03:07:38 +00:00
|
|
|
"go-humas-be/app/database/entity"
|
2024-03-05 19:15:53 +00:00
|
|
|
"go-humas-be/app/module/ppid_data_files/mapper"
|
|
|
|
|
"go-humas-be/app/module/ppid_data_files/repository"
|
|
|
|
|
"go-humas-be/app/module/ppid_data_files/request"
|
|
|
|
|
"go-humas-be/app/module/ppid_data_files/response"
|
2024-05-29 04:46:00 +00:00
|
|
|
config "go-humas-be/config/config"
|
2024-03-05 19:15:53 +00:00
|
|
|
"go-humas-be/utils/paginator"
|
2024-04-28 18:39:43 +00:00
|
|
|
"io"
|
|
|
|
|
"log"
|
2024-04-29 17:34:05 +00:00
|
|
|
"math/rand"
|
2024-04-28 18:39:43 +00:00
|
|
|
"mime"
|
2024-05-29 04:46:00 +00:00
|
|
|
"path"
|
2024-04-29 17:34:05 +00:00
|
|
|
"path/filepath"
|
2024-04-28 18:39:43 +00:00
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
2024-03-05 19:15:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// PpidDataFilesService
|
|
|
|
|
type ppidDataFilesService struct {
|
2024-04-28 18:39:43 +00:00
|
|
|
Repo repository.PpidDataFilesRepository
|
|
|
|
|
Log zerolog.Logger
|
2024-05-29 04:46:00 +00:00
|
|
|
MinioStorage *config.MinioStorage
|
|
|
|
|
Cfg *config.Config
|
2024-03-05 19:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PpidDataFilesService define interface of IPpidDataFilesService
|
|
|
|
|
type PpidDataFilesService interface {
|
|
|
|
|
All(req request.PpidDataFilesQueryRequest) (ppidDataFiles []*response.PpidDataFilesResponse, paging paginator.Pagination, err error)
|
|
|
|
|
Show(id uint) (ppidDataFiles *response.PpidDataFilesResponse, err error)
|
2024-04-28 18:39:43 +00:00
|
|
|
Save(c *fiber.Ctx) error
|
2024-03-05 19:15:53 +00:00
|
|
|
Update(id uint, req request.PpidDataFilesUpdateRequest) (err error)
|
2024-05-28 03:07:38 +00:00
|
|
|
UpdatePosition(req []request.PpidDataFilesUpdatePositionRequest) (err error)
|
2024-03-05 19:15:53 +00:00
|
|
|
Delete(id uint) error
|
2024-04-29 17:34:05 +00:00
|
|
|
Viewer(c *fiber.Ctx, filename string) error
|
2024-03-05 19:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewPpidDataFilesService init PpidDataFilesService
|
2024-05-29 04:46:00 +00:00
|
|
|
func NewPpidDataFilesService(repo repository.PpidDataFilesRepository, log zerolog.Logger, minioStorage *config.MinioStorage, cfg *config.Config) PpidDataFilesService {
|
2024-03-05 19:15:53 +00:00
|
|
|
|
|
|
|
|
return &ppidDataFilesService{
|
2024-04-28 18:39:43 +00:00
|
|
|
Repo: repo,
|
|
|
|
|
Log: log,
|
|
|
|
|
MinioStorage: minioStorage,
|
2024-05-29 04:46:00 +00:00
|
|
|
Cfg: cfg,
|
2024-03-05 19:15:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// All implement interface of PpidDataFilesService
|
|
|
|
|
func (_i *ppidDataFilesService) All(req request.PpidDataFilesQueryRequest) (ppidDataFiless []*response.PpidDataFilesResponse, paging paginator.Pagination, err error) {
|
|
|
|
|
results, paging, err := _i.Repo.GetAll(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, result := range results {
|
|
|
|
|
ppidDataFiless = append(ppidDataFiless, mapper.PpidDataFilesResponseMapper(result))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (_i *ppidDataFilesService) Show(id uint) (ppidDataFiles *response.PpidDataFilesResponse, err error) {
|
|
|
|
|
result, err := _i.Repo.FindOne(id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return mapper.PpidDataFilesResponseMapper(result), nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-28 18:39:43 +00:00
|
|
|
func (_i *ppidDataFilesService) Save(c *fiber.Ctx) (err error) {
|
|
|
|
|
|
2024-04-29 17:34:05 +00:00
|
|
|
id, err := strconv.ParseUint(c.Params("ppidDataId"), 10, 0)
|
2024-04-28 18:39:43 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-29 04:46:00 +00:00
|
|
|
host := _i.Cfg.App.Host
|
|
|
|
|
port := _i.Cfg.App.ExternalPort
|
2024-04-28 18:39:43 +00:00
|
|
|
|
|
|
|
|
form, err := c.MultipartForm()
|
|
|
|
|
|
2024-05-29 04:46:00 +00:00
|
|
|
_i.Log.Info().Str("timestamp", time.Now().
|
|
|
|
|
Format(time.RFC3339)).Str("Service", "Save").
|
|
|
|
|
Interface("form", form).Msg("")
|
|
|
|
|
|
|
|
|
|
typesString := form.Value["types"]
|
|
|
|
|
types := strings.Split(typesString[0], ",")
|
|
|
|
|
positionsString := form.Value["positions"]
|
|
|
|
|
positions := strings.Split(positionsString[0], ",")
|
2024-05-29 07:37:41 +00:00
|
|
|
var urls []string
|
2024-05-29 04:46:00 +00:00
|
|
|
urlsString := form.Value["urls"]
|
2024-05-29 07:37:41 +00:00
|
|
|
if urlsString != nil {
|
|
|
|
|
urls = strings.Split(urlsString[0], ",")
|
|
|
|
|
}
|
2024-05-29 04:46:00 +00:00
|
|
|
files := form.File["files"]
|
2024-03-05 19:15:53 +00:00
|
|
|
|
2024-05-29 04:46:00 +00:00
|
|
|
fileCounter := 0
|
|
|
|
|
urlCounter := 0
|
2024-04-28 18:39:43 +00:00
|
|
|
|
2024-05-29 04:46:00 +00:00
|
|
|
_i.Log.Info().Str("timestamp", time.Now().
|
|
|
|
|
Format(time.RFC3339)).Str("Service", "Save").
|
2024-05-29 07:37:41 +00:00
|
|
|
Interface("form", files).Msg("")
|
2024-04-28 18:39:43 +00:00
|
|
|
|
2024-05-29 04:46:00 +00:00
|
|
|
for index, fileType := range types {
|
|
|
|
|
position := &positions[index]
|
|
|
|
|
positionInt64, err := strconv.ParseInt(*position, 10, 0)
|
2024-04-28 18:39:43 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2024-05-29 04:46:00 +00:00
|
|
|
positionInt := int(positionInt64)
|
2024-04-29 17:34:05 +00:00
|
|
|
statusId := 1
|
2024-04-28 18:39:43 +00:00
|
|
|
req := request.PpidDataFilesCreateRequest{
|
2024-04-29 17:34:05 +00:00
|
|
|
PpidDataId: int(id),
|
2024-05-29 04:46:00 +00:00
|
|
|
Type: &fileType,
|
|
|
|
|
Position: &positionInt,
|
2024-04-29 17:34:05 +00:00
|
|
|
StatusId: &statusId,
|
2024-04-28 18:39:43 +00:00
|
|
|
}
|
|
|
|
|
|
2024-05-29 04:46:00 +00:00
|
|
|
_i.Log.Info().Str("timestamp", time.Now().
|
|
|
|
|
Format(time.RFC3339)).Str("Service", "Save").
|
|
|
|
|
Interface("request", req).Msg("")
|
|
|
|
|
|
|
|
|
|
if fileType == "url" {
|
|
|
|
|
url := urls[urlCounter]
|
|
|
|
|
filename := path.Base(url)
|
|
|
|
|
filenameWithoutExt := filepath.Clean(filename[:len(filename)-len(filepath.Ext(filename))])
|
|
|
|
|
extension := filepath.Ext(filename)[1:]
|
|
|
|
|
title := strings.ReplaceAll(filenameWithoutExt, "-", " ")
|
|
|
|
|
|
|
|
|
|
req.Title = &title
|
|
|
|
|
req.FileType = &extension
|
|
|
|
|
req.FileName = &filename
|
|
|
|
|
req.FileUrl = &url
|
|
|
|
|
|
|
|
|
|
urlCounter += 1
|
|
|
|
|
} else {
|
|
|
|
|
bucketName := _i.MinioStorage.Cfg.ObjectStorage.MinioStorage.BucketName
|
|
|
|
|
|
|
|
|
|
// Create minio connection.
|
|
|
|
|
minioClient, err := _i.MinioStorage.ConnectMinio()
|
|
|
|
|
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
|
|
|
|
|
file := files[fileCounter]
|
|
|
|
|
|
|
|
|
|
_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()
|
|
|
|
|
|
|
|
|
|
filename := filepath.Base(file.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 := "ppid/upload/" + newFilename
|
|
|
|
|
|
|
|
|
|
size := strconv.FormatInt(file.Size, 10)
|
|
|
|
|
fileUrl := "/ppid-data-files/viewer/"
|
|
|
|
|
fileUrl += host + port + newFilename
|
|
|
|
|
|
|
|
|
|
req.Title = &newFilenameWithoutExt
|
|
|
|
|
req.FileType = &extension
|
|
|
|
|
req.FileName = &newFilename
|
|
|
|
|
req.FilePath = &objectName
|
|
|
|
|
req.FileUrl = &fileUrl
|
|
|
|
|
req.Size = &size
|
|
|
|
|
|
|
|
|
|
// Upload file to MinIO
|
|
|
|
|
_, err = minioClient.PutObject(context.Background(), bucketName, objectName, src, file.Size, minio.PutObjectOptions{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fileCounter += 1
|
2024-04-28 18:39:43 +00:00
|
|
|
}
|
|
|
|
|
|
2024-05-29 04:46:00 +00:00
|
|
|
err = _i.Repo.Create(req.ToEntity())
|
2024-04-28 18:39:43 +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-05 19:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (_i *ppidDataFilesService) Update(id uint, req request.PpidDataFilesUpdateRequest) (err error) {
|
|
|
|
|
_i.Log.Info().Interface("data", req).Msg("")
|
|
|
|
|
return _i.Repo.Update(id, req.ToEntity())
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-28 03:07:38 +00:00
|
|
|
func (_i *ppidDataFilesService) UpdatePosition(req []request.PpidDataFilesUpdatePositionRequest) (err error) {
|
|
|
|
|
_i.Log.Info().Interface("data", req).Msg("")
|
|
|
|
|
|
|
|
|
|
var entityReq []*entity.PpidDataFiles
|
|
|
|
|
for _, reqItem := range req {
|
|
|
|
|
entityReq = append(entityReq, reqItem.ToEntity())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _i.Repo.UpdateAll(entityReq)
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-05 19:15:53 +00:00
|
|
|
func (_i *ppidDataFilesService) Delete(id uint) error {
|
2024-07-02 02:02:56 +00:00
|
|
|
result, err := _i.Repo.FindOne(id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isActive := false
|
|
|
|
|
result.IsActive = &isActive
|
|
|
|
|
return _i.Repo.Update(id, result)
|
2024-03-05 19:15:53 +00:00
|
|
|
}
|
2024-04-28 18:39:43 +00:00
|
|
|
|
2024-04-29 17:34:05 +00:00
|
|
|
func (_i *ppidDataFilesService) Viewer(c *fiber.Ctx, filename string) (err error) {
|
|
|
|
|
result, err := _i.Repo.FindByFilename(filename)
|
2024-04-28 18:39:43 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-03 13:19:54 +00:00
|
|
|
if result.FilePath == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-28 18:39:43 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
bucketName := _i.MinioStorage.Cfg.ObjectStorage.MinioStorage.BucketName
|
|
|
|
|
objectName := result.FilePath
|
|
|
|
|
|
|
|
|
|
_i.Log.Info().Str("timestamp", time.Now().
|
|
|
|
|
Format(time.RFC3339)).Str("Service:Resource", "Article:Uploads").
|
|
|
|
|
Interface("data", objectName).Msg("")
|
|
|
|
|
|
|
|
|
|
// Create minio connection.
|
|
|
|
|
minioClient, err := _i.MinioStorage.ConnectMinio()
|
|
|
|
|
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]
|
|
|
|
|
}
|