feat: fixing article files upload
This commit is contained in:
parent
9f2bf11ba3
commit
b85349e9d8
|
|
@ -30,6 +30,7 @@ func NewMiddleware(app *fiber.App, cfg *config.Config) *Middleware {
|
||||||
// Register registers all the middleware functions
|
// Register registers all the middleware functions
|
||||||
func (m *Middleware) Register() {
|
func (m *Middleware) Register() {
|
||||||
// Add Extra Middlewares
|
// Add Extra Middlewares
|
||||||
|
|
||||||
m.App.Use(limiter.New(limiter.Config{
|
m.App.Use(limiter.New(limiter.Config{
|
||||||
Next: utils.IsEnabled(m.Cfg.Middleware.Limiter.Enable),
|
Next: utils.IsEnabled(m.Cfg.Middleware.Limiter.Enable),
|
||||||
Max: m.Cfg.Middleware.Limiter.Max,
|
Max: m.Cfg.Middleware.Limiter.Max,
|
||||||
|
|
|
||||||
|
|
@ -75,10 +75,11 @@ func (_i *articleFilesService) Save(c *fiber.Ctx, id uint) (err error) {
|
||||||
bucketName := _i.MinioStorage.Cfg.ObjectStorage.MinioStorage.BucketName
|
bucketName := _i.MinioStorage.Cfg.ObjectStorage.MinioStorage.BucketName
|
||||||
|
|
||||||
form, err := c.MultipartForm()
|
form, err := c.MultipartForm()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
files := form.File["files"]
|
//filess := form.File["files"]
|
||||||
|
|
||||||
// Create minio connection.
|
// Create minio connection.
|
||||||
minioClient, err := _i.MinioStorage.ConnectMinio()
|
minioClient, err := _i.MinioStorage.ConnectMinio()
|
||||||
|
|
@ -90,51 +91,56 @@ func (_i *articleFilesService) Save(c *fiber.Ctx, id uint) (err error) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Iterasi semua file yang diunggah
|
for _, files := range form.File {
|
||||||
for _, file := range files {
|
|
||||||
|
|
||||||
_i.Log.Info().Str("timestamp", time.Now().
|
_i.Log.Info().Str("timestamp", time.Now().
|
||||||
Format(time.RFC3339)).Str("Service:Resource", "Uploader:: loop1").
|
Format(time.RFC3339)).Str("Service:Resource", "Uploader:: top").
|
||||||
Interface("data", file).Msg("")
|
Interface("files", files).Msg("")
|
||||||
|
|
||||||
src, err := file.Open()
|
for _, fileHeader := range files {
|
||||||
if err != nil {
|
_i.Log.Info().Str("timestamp", time.Now().
|
||||||
return err
|
Format(time.RFC3339)).Str("Service:Resource", "Uploader:: loop").
|
||||||
}
|
Interface("data", fileHeader).Msg("")
|
||||||
defer src.Close()
|
|
||||||
|
|
||||||
filename := filepath.Base(file.Filename)
|
src, err := fileHeader.Open()
|
||||||
filenameAlt := filepath.Clean(filename[:len(filename)-len(filepath.Ext(filename))])
|
if err != nil {
|
||||||
filename = strings.ReplaceAll(filename, " ", "")
|
return err
|
||||||
filenameWithoutExt := filepath.Clean(filename[:len(filename)-len(filepath.Ext(filename))])
|
}
|
||||||
extension := filepath.Ext(file.Filename)[1:]
|
defer src.Close()
|
||||||
|
|
||||||
rand.New(rand.NewSource(time.Now().UnixNano()))
|
filename := filepath.Base(fileHeader.Filename)
|
||||||
randUniqueId := rand.Intn(1000000)
|
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(fileHeader.Filename)[1:]
|
||||||
|
|
||||||
newFilenameWithoutExt := filenameWithoutExt + "_" + strconv.Itoa(randUniqueId)
|
rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||||
newFilename := newFilenameWithoutExt + "." + extension
|
randUniqueId := rand.Intn(1000000)
|
||||||
|
|
||||||
objectName := "articles/upload/" + newFilename
|
newFilenameWithoutExt := filenameWithoutExt + "_" + strconv.Itoa(randUniqueId)
|
||||||
fileSize := strconv.FormatInt(file.Size, 10)
|
newFilename := newFilenameWithoutExt + "." + extension
|
||||||
|
|
||||||
req := request.ArticleFilesCreateRequest{
|
objectName := "articles/upload/" + newFilename
|
||||||
ArticleId: id,
|
fileSize := strconv.FormatInt(fileHeader.Size, 10)
|
||||||
FilePath: &objectName,
|
|
||||||
FileName: &newFilename,
|
|
||||||
FileAlt: &filenameAlt,
|
|
||||||
Size: &fileSize,
|
|
||||||
}
|
|
||||||
|
|
||||||
err = _i.Repo.Create(req.ToEntity())
|
req := request.ArticleFilesCreateRequest{
|
||||||
if err != nil {
|
ArticleId: id,
|
||||||
return err
|
FilePath: &objectName,
|
||||||
}
|
FileName: &newFilename,
|
||||||
|
FileAlt: &filenameAlt,
|
||||||
|
Size: &fileSize,
|
||||||
|
}
|
||||||
|
|
||||||
// Upload file ke MinIO
|
err = _i.Repo.Create(req.ToEntity())
|
||||||
_, err = minioClient.PutObject(context.Background(), bucketName, objectName, src, file.Size, minio.PutObjectOptions{})
|
if err != nil {
|
||||||
if err != nil {
|
return err
|
||||||
return err
|
}
|
||||||
|
|
||||||
|
// Upload file ke MinIO
|
||||||
|
_, err = minioClient.PutObject(context.Background(), bucketName, objectName, src, fileHeader.Size, minio.PutObjectOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ type app = struct {
|
||||||
Prefork bool `toml:"prefork"`
|
Prefork bool `toml:"prefork"`
|
||||||
Production bool `toml:"production"`
|
Production bool `toml:"production"`
|
||||||
IdleTimeout time.Duration `toml:"idle-timeout"`
|
IdleTimeout time.Duration `toml:"idle-timeout"`
|
||||||
|
BodyLimit int `toml:"body-limit"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// db struct config
|
// db struct config
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ idle-timeout = 5 # As seconds
|
||||||
print-routes = false
|
print-routes = false
|
||||||
prefork = true
|
prefork = true
|
||||||
production = false
|
production = false
|
||||||
|
body-limit = 104857600 # "100 * 1024 * 1024"
|
||||||
|
|
||||||
[db.postgres]
|
[db.postgres]
|
||||||
dsn = "postgresql://humas_polri:P@ssw0rd.1@103.82.242.92:5432/humas_polri" # <driver>://<username>:<password>@<host>:<port>/<database>
|
dsn = "postgresql://humas_polri:P@ssw0rd.1@103.82.242.92:5432/humas_polri" # <driver>://<username>:<password>@<host>:<port>/<database>
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,7 @@ func NewFiber(cfg *config.Config) *fiber.App {
|
||||||
ErrorHandler: response.ErrorHandler,
|
ErrorHandler: response.ErrorHandler,
|
||||||
IdleTimeout: cfg.App.IdleTimeout * time.Second,
|
IdleTimeout: cfg.App.IdleTimeout * time.Second,
|
||||||
EnablePrintRoutes: cfg.App.PrintRoutes,
|
EnablePrintRoutes: cfg.App.PrintRoutes,
|
||||||
|
BodyLimit: cfg.App.BodyLimit,
|
||||||
DisableStartupMessage: true,
|
DisableStartupMessage: true,
|
||||||
ReadBufferSize: 8192,
|
ReadBufferSize: 8192,
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -792,7 +792,7 @@ const docTemplate = `{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"/article-files/viewer/{id}": {
|
"/article-files/viewer/{filename}": {
|
||||||
"get": {
|
"get": {
|
||||||
"security": [
|
"security": [
|
||||||
{
|
{
|
||||||
|
|
@ -807,8 +807,8 @@ const docTemplate = `{
|
||||||
"parameters": [
|
"parameters": [
|
||||||
{
|
{
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "Article File ID",
|
"description": "Article File Name",
|
||||||
"name": "id",
|
"name": "filename",
|
||||||
"in": "path",
|
"in": "path",
|
||||||
"required": true
|
"required": true
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -781,7 +781,7 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"/article-files/viewer/{id}": {
|
"/article-files/viewer/{filename}": {
|
||||||
"get": {
|
"get": {
|
||||||
"security": [
|
"security": [
|
||||||
{
|
{
|
||||||
|
|
@ -796,8 +796,8 @@
|
||||||
"parameters": [
|
"parameters": [
|
||||||
{
|
{
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "Article File ID",
|
"description": "Article File Name",
|
||||||
"name": "id",
|
"name": "filename",
|
||||||
"in": "path",
|
"in": "path",
|
||||||
"required": true
|
"required": true
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1245,13 +1245,13 @@ paths:
|
||||||
summary: Update ArticleFiles
|
summary: Update ArticleFiles
|
||||||
tags:
|
tags:
|
||||||
- Article Files
|
- Article Files
|
||||||
/article-files/viewer/{id}:
|
/article-files/viewer/{filename}:
|
||||||
get:
|
get:
|
||||||
description: API for create ArticleFiles
|
description: API for create ArticleFiles
|
||||||
parameters:
|
parameters:
|
||||||
- description: Article File ID
|
- description: Article File Name
|
||||||
in: path
|
in: path
|
||||||
name: id
|
name: filename
|
||||||
required: true
|
required: true
|
||||||
type: string
|
type: string
|
||||||
responses:
|
responses:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue