/
attachment.go
executable file
·149 lines (127 loc) · 3.85 KB
/
attachment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package services
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
weddinginvitation "github.com/gitkuldp/wedding-invitation-api"
"github.com/gitkuldp/wedding-invitation-api/internal/api"
"github.com/gitkuldp/wedding-invitation-api/internal/api/helper"
"github.com/gitkuldp/wedding-invitation-api/internal/models"
"github.com/gitkuldp/wedding-invitation-api/internal/utils"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"gorm.io/gorm"
)
type attachmentService struct {
service
}
// DetailAttachment implements AttachmentService
func (as *attachmentService) DetailAttachment(id uuid.UUID) (*models.Attachment, error) {
var attach models.Attachment
if err := as.db.First(&attach).Where("id = ?", id).Error; err != nil {
return nil, as.translate(err)
}
return &attach, nil
}
// ListAttachment implements AttachmentService
func (as *attachmentService) ListAttachment(ctx echo.Context, params models.QueryParams) ([]*models.Attachment, *models.Pagination, error) {
pagination := utils.ParsePagination(ctx)
db := ApplyQueryParams(as.db, params)
var attachments []*models.Attachment
err := db.Find(&attachments).Error
if err != nil {
return nil, nil, err
}
meta, err := Paginate(db, pagination, len(attachments), models.Attachment{})
if err != nil {
return nil, nil, err
}
return attachments, meta, nil
}
type AttachmentService interface {
CreateAttachment(ctx echo.Context, attachment models.AttachmentView) ([]*models.Attachment, error)
ListAttachment(ctx echo.Context, params models.QueryParams) ([]*models.Attachment, *models.Pagination, error)
DetailAttachment(id uuid.UUID) (*models.Attachment, error)
}
func NewAttachmentService(db *gorm.DB) AttachmentService {
return &attachmentService{
service{
db: db,
},
}
}
func (as *attachmentService) CreateAttachment(ctx echo.Context, attachment models.AttachmentView) ([]*models.Attachment, error) {
// Get the uploaded files
form, err := ctx.MultipartForm()
if err != nil {
return nil, err
}
myMap := form.Value
if values, ok := myMap["table_name"]; ok {
if len(values) > 0 {
attachment.TableName = api.TableName(values[0])
}
}
if values, ok := myMap["table_id"]; ok {
if len(values) > 0 {
attachment.TableID = values[0]
}
}
tableName, isValid := helper.MapTableName(string(attachment.TableName))
if !isValid {
return nil, weddinginvitation.NewWeddingInvitationError("Table not Found")
}
var attachments []*models.Attachment
files := form.File["images"]
// Create the folder path
folderPath := fmt.Sprintf("./uploads/%s", string(attachment.TableName))
if _, err := os.Stat(folderPath); os.IsNotExist(err) {
os.MkdirAll(folderPath, 0700)
}
// Process each uploaded file
for _, file := range files {
src, err := file.Open()
if err != nil {
return nil, err
}
defer src.Close()
// Read the file content
byteContent, err := ioutil.ReadAll(src)
if err != nil {
return nil, err
}
attachmentID := uuid.NewString()
parsedUUID, err := uuid.Parse(attachmentID)
if err != nil {
return nil, as.translate(err)
}
fileName := fmt.Sprintf(attachmentID + "-" + utils.SanitizeString(file.Filename))
fileURL := fmt.Sprintf(string(attachment.TableName) + "/" + fileName)
filePath := filepath.Join(folderPath, fileName)
// Write the file to the server
err = ioutil.WriteFile(filePath, byteContent, 0644)
if err != nil {
return nil, err
}
// Create the attachment object
attach := &models.Attachment{
ID: parsedUUID,
TableName: tableName,
TableID: attachment.TableID,
FileName: utils.SanitizeString(file.Filename),
FileURL: fileURL,
FileType: file.Header.Get("Content-Type"),
FileSize: int(file.Size),
IsActive: true,
CreatedAt: time.Now(),
}
attachments = append(attachments, attach)
}
err = as.Create(attachments)
if err != nil {
return nil, as.translate(err)
}
return attachments, nil
}