-
Notifications
You must be signed in to change notification settings - Fork 0
/
UploadService.go
139 lines (118 loc) · 3.42 KB
/
UploadService.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
package services
import (
"context"
"fmt"
"io"
"mime/multipart"
"os"
"strings"
"time"
"github.com/rzknugraha/zorro-mark/helpers"
"github.com/rzknugraha/zorro-mark/infrastructures"
"github.com/rzknugraha/zorro-mark/models"
"github.com/rzknugraha/zorro-mark/repositories"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
// IUploadService is
type IUploadService interface {
StoreFile(ctx context.Context, file multipart.File, oldName string, IDUser int) (Response *helpers.JSONResponse, err error)
}
// UploadService is
type UploadService struct {
DocumentRepository repositories.IDocumentsRepository
DocumentUserRepository repositories.IDocumentUserRepository
DB infrastructures.ISQLConnection
}
// InitUploadService init
func InitUploadService() *UploadService {
documentRepositories := new(repositories.DocumentsRepository)
documentRepositories.DB = &infrastructures.SQLConnection{}
documentUserRepositories := new(repositories.DocumentUserRepository)
documentUserRepositories.DB = &infrastructures.SQLConnection{}
UploadService := new(UploadService)
UploadService.DocumentRepository = documentRepositories
UploadService.DocumentUserRepository = documentUserRepositories
return UploadService
}
// StoreFile is
func (s *UploadService) StoreFile(ctx context.Context, file multipart.File, oldName string, IDUser int) (Response *helpers.JSONResponse, err error) {
trimSpace := strings.ReplaceAll(oldName, " ", "")
path := viper.GetString("storage.path")
fileResp := models.UploadResp{
FileName: fmt.Sprintf("%d-%s", time.Now().UnixNano(), trimSpace),
}
fullPath := fmt.Sprintf("/%s/%s", path, fileResp.FileName)
dst, err := os.Create("." + fullPath)
if err != nil {
return
}
defer dst.Close()
// Copy the uploaded file to the filesystem
// at the specified destination
_, err = io.Copy(dst, file)
if err != nil {
return
}
tx, err := s.DocumentRepository.Tx()
if err != nil {
logrus.WithFields(logrus.Fields{
"code": 5500,
"error": err,
"data": fileResp,
}).Error("[Service StoreFile] error create tx")
return
}
fmt.Println("siniini")
defer tx.RollbackUnlessCommitted()
TimeNow := time.Now()
dataDoc := models.Documents{
CreatedBy: IDUser,
FileName: oldName,
Path: fullPath,
Status: 1,
CreatedAt: TimeNow.Format("2006-01-02 15:04:05"),
}
count, err := s.DocumentRepository.CountNameByUserID(ctx, dataDoc)
if err != nil {
logrus.WithFields(logrus.Fields{
"code": 5500,
"error": err,
"data": fileResp,
}).Error("[Service StoreFile] error count filename doc")
return
}
if count > 0 {
dataDoc.FileName = fmt.Sprintf("%s (%v)", dataDoc.FileName, count)
}
idDoc, err := s.DocumentRepository.StoreDocuments(ctx, tx, dataDoc)
if err != nil || idDoc == 0 {
logrus.WithFields(logrus.Fields{
"code": 5500,
"error": err,
"data": fileResp,
}).Error("[Service StoreFile] error store doc")
return
}
docUser := models.DocumentUser{
DocumentID: int(idDoc),
UserID: IDUser,
Status: 1,
CreatedAt: TimeNow.Format("2006-01-02 15:04:05"),
}
idStore, err := s.DocumentUserRepository.StoreDocumentUser(ctx, tx, docUser)
if err != nil || idStore == 0 {
logrus.WithFields(logrus.Fields{
"code": 5500,
"error": err,
"data": fileResp,
}).Error("[Service StoreFile] error store doc user")
return
}
tx.Commit()
return &helpers.JSONResponse{
Code: 2200,
Message: "Success",
Data: fileResp,
}, nil
}