-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcorporation-pdf.go
215 lines (184 loc) · 5.55 KB
/
corporation-pdf.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package controllers
import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/opensourceways/app-cla-server/config"
"github.com/opensourceways/app-cla-server/dbmodels"
"github.com/opensourceways/app-cla-server/models"
"github.com/opensourceways/app-cla-server/pdf"
"github.com/opensourceways/app-cla-server/util"
)
type CorporationPDFController struct {
baseController
}
func (this *CorporationPDFController) Prepare() {
if strings.HasSuffix(this.routerPattern(), "/") {
// admin reviews pdf
this.apiPrepare(PermissionCorpAdmin)
} else {
this.apiPrepare(PermissionOwnerOfOrg)
}
}
func (this *CorporationPDFController) downloadCorpPDF(linkID, corpEmail string) *failedApiResult {
dir := util.GenFilePath(config.AppConfig.PDFOutDir, "tmp")
s := strings.ReplaceAll(util.EmailSuffix(corpEmail), ".", "_")
name := fmt.Sprintf("%s_%s_*.pdf", linkID, s)
f, err := ioutil.TempFile(dir, name)
if err != nil {
return newFailedApiResult(500, errSystemError, err)
}
path := f.Name()
f.Close()
defer func() {
os.Remove(path)
}()
merr := models.DownloadCorporationSigningPDF(linkID, corpEmail, path)
if merr != nil {
if merr.IsErrorOf(models.ErrNoLinkOrUnuploaed) {
return newFailedApiResult(400, errUnuploaded, merr)
}
return parseModelError(merr)
}
this.downloadFile(path)
return nil
}
// @Title Upload
// @Description upload pdf of corporation signing
// @Param :org_cla_id path string true "org cla id"
// @Param :email path string true "email of corp"
// @Success 204 {int} map
// @router /:link_id/:email [patch]
func (this *CorporationPDFController) Upload() {
action := "upload corp's signing pdf"
linkID := this.GetString(":link_id")
corpEmail := this.GetString(":email")
pl, fr := this.tokenPayloadBasedOnCodePlatform()
if fr != nil {
this.sendFailedResultAsResp(fr, action)
return
}
if fr := pl.isOwnerOfLink(linkID); fr != nil {
this.sendFailedResultAsResp(fr, action)
return
}
// lock to avoid conflict with deleting corp signing
unlock, fr := lockOnRepo(pl.orgInfo(linkID))
if fr != nil {
this.sendFailedResultAsResp(fr, action)
return
}
defer unlock()
b, merr := models.IsCorpSigned(linkID, corpEmail)
if merr != nil {
this.sendModelErrorAsResp(merr, action)
return
}
if !b {
this.sendFailedResponse(400, errUnsigned, fmt.Errorf("not signed"), action)
return
}
data, fr := this.readInputFile("pdf", config.AppConfig.MaxSizeOfCorpCLAPDF)
if fr != nil {
this.sendFailedResultAsResp(fr, action)
return
}
if err := models.UploadCorporationSigningPDF(linkID, corpEmail, data); err != nil {
this.sendModelErrorAsResp(err, action)
return
}
this.sendSuccessResp("upload pdf of signature page successfully")
}
// @Title Download
// @Description download pdf of corporation signing
// @Param :org_cla_id path string true "org cla id"
// @Param :email path string true "email of corp"
// @Success 200 {int} map
// @router /:link_id/:email [get]
func (this *CorporationPDFController) Download() {
action := "download corp's signing pdf"
linkID := this.GetString(":link_id")
corpEmail := this.GetString(":email")
pl, fr := this.tokenPayloadBasedOnCodePlatform()
if fr != nil {
this.sendFailedResultAsResp(fr, action)
return
}
if fr := pl.isOwnerOfLink(linkID); fr != nil {
this.sendFailedResultAsResp(fr, action)
return
}
if fr := this.downloadCorpPDF(linkID, corpEmail); fr != nil {
this.sendFailedResultAsResp(fr, action)
}
}
// @Title Review
// @Description corp administrator review pdf of corporation signing
// @Success 200 {int} map
// @router / [get]
func (this *CorporationPDFController) Review() {
action := "download corp's signing pdf"
pl, fr := this.tokenPayloadBasedOnCorpManager()
if fr != nil {
this.sendFailedResultAsResp(fr, action)
return
}
if fr := this.downloadCorpPDF(pl.LinkID, pl.Email); fr != nil {
this.sendFailedResultAsResp(fr, action)
}
}
// @Title Preview
// @Description preview the unsinged pdf of corp
// @Param :org_cla_id path string true "org cla id"
// @Success 200 {int} map
// @router /preview/:linkID/:language [get]
func (this *CorporationPDFController) Preview() {
action := "preview blank pdf"
linkID := this.GetString(":link_id")
claLang := this.GetString(":language")
pl, fr := this.tokenPayloadBasedOnCodePlatform()
if fr != nil {
this.sendFailedResultAsResp(fr, action)
return
}
if fr := pl.isOwnerOfLink(linkID); fr != nil {
this.sendFailedResultAsResp(fr, action)
return
}
orgInfo, merr := models.GetOrgOfLink(linkID)
if merr != nil {
this.sendModelErrorAsResp(merr, action)
return
}
claInfo, merr := models.GetCLAInfoToSign(linkID, claLang, dbmodels.ApplyToCorporation)
if merr != nil {
this.sendModelErrorAsResp(merr, action)
return
}
if claInfo == nil {
this.sendFailedResponse(400, errUnsupportedCLALang, fmt.Errorf("unsupport language"), action)
return
}
claFile := genCLAFilePath(linkID, dbmodels.ApplyToCorporation, claLang)
orgSignatureFile := genOrgSignatureFilePath(linkID, claLang)
value := map[string]string{}
for _, item := range claInfo.Fields {
value[item.ID] = ""
}
signing := models.CorporationSigning{
CorporationSigningBasicInfo: dbmodels.CorporationSigningBasicInfo{
AdminEmail: "test@preview_blank_pdf.com",
Date: util.Date(),
},
Info: dbmodels.TypeSigningInfo(value),
}
outFile, err := pdf.GetPDFGenerator().GenPDFForCorporationSigning(
linkID, orgSignatureFile, claFile, orgInfo, &signing, claInfo.Fields)
if err != nil {
this.sendFailedResponse(400, errSystemError, err, action)
return
}
defer func() { os.Remove(outFile) }()
this.downloadFile(outFile)
}