From a39455f281ce7869fbdfa7308d59045bbdd3d6c9 Mon Sep 17 00:00:00 2001 From: zengchen1024 Date: Sun, 10 Jan 2021 20:59:00 +0800 Subject: [PATCH] update api of org signature --- controllers/corporation-pdf.go | 2 +- controllers/corporation-signing.go | 5 +- controllers/org-signature.go | 126 ++++------------------------- dbmodels/db.go | 7 -- models/org-signature.go | 17 ---- mongodb/org-signature.go | 95 ---------------------- pdf/interface.go | 2 +- pdf/pdf-generator.go | 41 ++-------- util/util.go | 4 - worker/worker.go | 6 +- 10 files changed, 32 insertions(+), 273 deletions(-) delete mode 100644 models/org-signature.go delete mode 100644 mongodb/org-signature.go diff --git a/controllers/corporation-pdf.go b/controllers/corporation-pdf.go index 02c79eb..19ae14a 100644 --- a/controllers/corporation-pdf.go +++ b/controllers/corporation-pdf.go @@ -203,6 +203,6 @@ func (this *CorporationPDFController) Preview() { Info: dbmodels.TypeSigningInfo(value), } - pdf.GetPDFGenerator().GenPDFForCorporationSigning(orgCLA, &signing, cla) + pdf.GetPDFGenerator().GenPDFForCorporationSigning("", orgCLA, &signing, cla) // TODO: not finished } diff --git a/controllers/corporation-signing.go b/controllers/corporation-signing.go index 78f0af8..513cb87 100644 --- a/controllers/corporation-signing.go +++ b/controllers/corporation-signing.go @@ -75,7 +75,9 @@ func (this *CorporationSigningController) Post() { this.sendSuccessResp("sign successfully") - worker.GetEmailWorker().GenCLAPDFForCorporationAndSendIt(orgCLA, &info.CorporationSigning, cla) + worker.GetEmailWorker().GenCLAPDFForCorporationAndSendIt( + genOrgSignatureFilePath(orgCLAID, cla.Language), + orgCLA, &info.CorporationSigning, cla) } // @Title ResendCorpSigningEmail @@ -126,6 +128,7 @@ func (this *CorporationSigningController) ResendCorpSigningEmail() { this.sendSuccessResp("resend email successfully") worker.GetEmailWorker().GenCLAPDFForCorporationAndSendIt( + genOrgSignatureFilePath(linkID, cla.Language), orgCLA, (*models.CorporationSigning)(signingInfo), cla, ) diff --git a/controllers/org-signature.go b/controllers/org-signature.go index d2b80bc..dcfa48c 100644 --- a/controllers/org-signature.go +++ b/controllers/org-signature.go @@ -2,10 +2,7 @@ package controllers import ( "fmt" - "io/ioutil" - "github.com/opensourceways/app-cla-server/conf" - "github.com/opensourceways/app-cla-server/models" "github.com/opensourceways/app-cla-server/pdf" "github.com/opensourceways/app-cla-server/util" ) @@ -15,124 +12,35 @@ type OrgSignatureController struct { } func (this *OrgSignatureController) Prepare() { - apiPrepare(&this.Controller, []string{PermissionOwnerOfOrg}) -} - -// @Title Post -// @Description upload org signature -// @Param org_cla_id path string true "org cla id" -// @router /:org_cla_id [post] -func (this *OrgSignatureController) Post() { - var statusCode = 0 - var errCode = "" - var reason error - var body interface{} - - defer func() { - sendResponse(&this.Controller, statusCode, errCode, reason, body, "upload org signature") - }() - - orgCLAID, err := fetchStringParameter(&this.Controller, ":org_cla_id") - if err != nil { - reason = err - errCode = util.ErrInvalidParameter - statusCode = 400 - return - } - - var orgCLA *models.OrgCLA - orgCLA, statusCode, errCode, reason = canAccessOrgCLA(&this.Controller, orgCLAID) - if reason != nil { - return - } - if isNotCorpCLA(orgCLA) { - reason = fmt.Errorf("no need upload org signature for individual signing") - errCode = util.ErrInvalidParameter - statusCode = 400 - return - } - - f, _, err := this.GetFile("signature_page") - if err != nil { - reason = err - errCode = util.ErrInvalidParameter - statusCode = 400 - return - } - - defer f.Close() - - data, err := ioutil.ReadAll(f) - if err != nil { - reason = err - return - } - - if len(data) > (200 << 10) { - reason = fmt.Errorf("big pdf file") - errCode = util.ErrInvalidParameter - statusCode = 400 - return - } - - err = models.UploadOrgSignature(orgCLAID, data) - if err != nil { - reason = err - return - } - - body = "upload pdf of signature page successfully" -} - -func (this *OrgSignatureController) downloadPDF(fileName string, pdf *[]byte) *failedApiResult { - dir := util.GenFilePath(conf.AppConfig.PDFOutDir, "tmp") - name := fmt.Sprintf("%s_*.pdf", fileName) - - f, err := ioutil.TempFile(dir, name) - if err != nil { - return newFailedApiResult(500, util.ErrSystemError, err) - } - - _, err = f.Write(*pdf) - if err != nil { - return newFailedApiResult(500, util.ErrSystemError, err) - } - - downloadFile(&this.Controller, f.Name()) - return nil + this.apiPrepare(PermissionOwnerOfOrg) } // @Title Get // @Description download org signature // @Param org_cla_id path string true "org cla id" -// @router /:org_cla_id [get] +// @router /:link_id/:language [get] func (this *OrgSignatureController) Get() { - rs := func(statusCode int, errCode string, reason error) { - sendResponse(&this.Controller, statusCode, errCode, reason, nil, "download org signature") - } + action := "download org signature" + linkID := this.GetString(":link_id") + claLang := this.GetString(":language") - orgCLAID, err := fetchStringParameter(&this.Controller, ":org_cla_id") - if err != nil { - rs(400, util.ErrInvalidParameter, err) + pl, fr := this.tokenPayloadBasedOnCodePlatform() + if fr != nil { + this.sendFailedResultAsResp(fr, action) return } - - _, statusCode, errCode, reason := canAccessOrgCLA(&this.Controller, orgCLAID) - if reason != nil { - rs(statusCode, errCode, reason) + if fr := pl.isOwnerOfLink(linkID); fr != nil { + this.sendFailedResultAsResp(fr, action) return } - pdf, err := models.DownloadOrgSignature(orgCLAID) - if err != nil { - rs(0, "", err) + path := genOrgSignatureFilePath(linkID, claLang) + if util.IsFileNotExist(path) { + this.sendFailedResponse(400, errFileNotExists, fmt.Errorf(errFileNotExists), action) return } - if fr := this.downloadPDF(orgCLAID, &pdf); fr != nil { - rs(fr.statusCode, fr.errCode, fr.reason) - } - + this.downloadFile(path) } // @Title BlankSignature @@ -140,13 +48,11 @@ func (this *OrgSignatureController) Get() { // @Param language path string true "The language which the signature applies to" // @router /blank/:language [get] func (this *OrgSignatureController) BlankSignature() { - sendResp := this.newFuncForSendingFailedResp("download blank signature") - lang := this.GetString(":language") - path := pdf.GetPDFGenerator().GetBlankSignaturePath(lang) + path := pdf.GetPDFGenerator().GetBlankSignaturePath(lang) if util.IsFileNotExist(path) { - sendResp(newFailedApiResult(400, errFileNotExists, fmt.Errorf(errFileNotExists))) + this.sendFailedResponse(400, errFileNotExists, fmt.Errorf(errFileNotExists), "download blank signature") return } diff --git a/dbmodels/db.go b/dbmodels/db.go index 0e084ff..5289960 100644 --- a/dbmodels/db.go +++ b/dbmodels/db.go @@ -19,7 +19,6 @@ type IDB interface { IIndividualSigning ICLA IVerificationCode - IPDF } type ICorporationSigning interface { @@ -91,12 +90,6 @@ type IVerificationCode interface { GetVerificationCode(opt *VerificationCode) IDBError } -type IPDF interface { - UploadOrgSignature(orgCLAID string, pdf []byte) error - DownloadOrgSignature(orgCLAID string) ([]byte, error) - DownloadOrgSignatureByMd5(orgCLAID, md5sum string) ([]byte, error) -} - type ILink interface { GetLinkID(orgRepo *OrgRepo) (string, IDBError) CreateLink(info *LinkCreateOption) (string, IDBError) diff --git a/models/org-signature.go b/models/org-signature.go deleted file mode 100644 index f9a2457..0000000 --- a/models/org-signature.go +++ /dev/null @@ -1,17 +0,0 @@ -package models - -import ( - "github.com/opensourceways/app-cla-server/dbmodels" -) - -func UploadOrgSignature(orgCLAID string, pdf []byte) error { - return dbmodels.GetDB().UploadOrgSignature(orgCLAID, pdf) -} - -func DownloadOrgSignature(orgCLAID string) ([]byte, error) { - return dbmodels.GetDB().DownloadOrgSignature(orgCLAID) -} - -func DownloadOrgSignatureByMd5(orgCLAID, md5sum string) ([]byte, error) { - return dbmodels.GetDB().DownloadOrgSignatureByMd5(orgCLAID, md5sum) -} diff --git a/mongodb/org-signature.go b/mongodb/org-signature.go deleted file mode 100644 index 673d3f7..0000000 --- a/mongodb/org-signature.go +++ /dev/null @@ -1,95 +0,0 @@ -package mongodb - -import ( - "context" - "fmt" - - "github.com/opensourceways/app-cla-server/dbmodels" - "github.com/opensourceways/app-cla-server/util" - "go.mongodb.org/mongo-driver/bson" -) - -func (this *client) UploadOrgSignature(orgCLAID string, pdf []byte) error { - oid, err := toObjectID(orgCLAID) - if err != nil { - return err - } - - f := func(ctx context.Context) error { - return this.updateDoc( - ctx, this.orgCLACollection, filterOfDocID(oid), - bson.M{ - fieldOrgSignature: pdf, - fieldOrgSignatureTag: util.Md5sumOfBytes(&pdf), - }, - ) - } - - return withContext(f) -} - -func (this *client) DownloadOrgSignature(orgCLAID string) ([]byte, error) { - oid, err := toObjectID(orgCLAID) - if err != nil { - return nil, err - } - - var v OrgCLA - - f := func(ctx context.Context) error { - return this.getDoc( - ctx, this.orgCLACollection, filterOfDocID(oid), - bson.M{fieldOrgSignature: 1}, - &v, - ) - } - - if withContext(f); err != nil { - return nil, err - } - - return v.OrgSignature, nil -} - -func (this *client) DownloadOrgSignatureByMd5(orgCLAID, md5sum string) ([]byte, error) { - oid, err := toObjectID(orgCLAID) - if err != nil { - return nil, err - } - - var v []OrgCLA - - f := func(ctx context.Context) error { - pipeline := bson.A{ - bson.M{"$match": filterOfDocID(oid)}, - bson.M{"$project": bson.M{ - fieldOrgSignature: bson.M{"$cond": bson.M{ - "if": bson.M{"$eq": bson.A{md5sum, "$" + fieldOrgSignatureTag}}, - "then": "$$REMOVE", - "else": "$" + fieldOrgSignature, - }}, - }}, - } - - col := this.collection(this.orgCLACollection) - cursor, err := col.Aggregate(ctx, pipeline) - if err != nil { - return err - } - - return cursor.All(ctx, &v) - } - - if withContext(f); err != nil { - return nil, err - } - - if len(v) > 0 { - return v[0].OrgSignature, nil - } - - return nil, dbmodels.DBError{ - Err: fmt.Errorf("can't find org's cla"), - ErrCode: util.ErrNoDBRecord, - } -} diff --git a/pdf/interface.go b/pdf/interface.go index 955d065..b24d231 100644 --- a/pdf/interface.go +++ b/pdf/interface.go @@ -11,7 +11,7 @@ type IPDFGenerator interface { LangSupported() map[string]bool GetBlankSignaturePath(string) string - GenPDFForCorporationSigning(orgCLA *models.OrgCLA, signing *models.CorporationSigning, cla *models.CLA) (string, error) + GenPDFForCorporationSigning(string, *models.OrgCLA, *models.CorporationSigning, *models.CLA) (string, error) } var generator *pdfGenerator diff --git a/pdf/pdf-generator.go b/pdf/pdf-generator.go index 240094d..6f50665 100644 --- a/pdf/pdf-generator.go +++ b/pdf/pdf-generator.go @@ -2,7 +2,6 @@ package pdf import ( "fmt" - "io/ioutil" "os" "os/exec" "sort" @@ -28,7 +27,7 @@ func (this *pdfGenerator) GetBlankSignaturePath(claLang string) string { return util.GenFilePath(this.pdfOrgSigDir, strings.ToLower(claLang)+"_blank_signature.pdf") } -func (this *pdfGenerator) GenPDFForCorporationSigning(orgCLA *models.OrgCLA, signing *models.CorporationSigning, cla *models.CLA) (string, error) { +func (this *pdfGenerator) GenPDFForCorporationSigning(orgSignatureFile string, orgCLA *models.OrgCLA, signing *models.CorporationSigning, cla *models.CLA) (string, error) { tempPdf, err := genCorporPDFMissingSig(this.corp, orgCLA, signing, cla, this.pdfOutDir) if err != nil { return "", err @@ -46,9 +45,8 @@ func (this *pdfGenerator) GenPDFForCorporationSigning(orgCLA *models.OrgCLA, sig } defer unlock() - orgSigPdfFile := util.OrgSignaturePDFFILE(this.pdfOrgSigDir, orgCLA.ID) file := util.CorporCLAPDFFile(this.pdfOutDir, orgCLA.ID, signing.AdminEmail, "") - if err := mergeCorporPDFSignaturePage(orgCLA.ID, this.pythonBin, tempPdf, orgSigPdfFile, file); err != nil { + if err := mergeCorporPDFSignaturePage(this.pythonBin, tempPdf, orgSignatureFile, file); err != nil { return "", err } @@ -82,39 +80,14 @@ func genCorporPDFMissingSig(c *corpSigningPDF, orgCLA *models.OrgCLA, signing *m return path, nil } -func mergeCorporPDFSignaturePage(orgCLAID, pythonBin, pdfFile, sigFile, outfile string) error { - md5sum := "" - var err error - if !util.IsFileNotExist(sigFile) { - if md5sum, err = util.Md5sumOfFile(sigFile); err != nil { - return fmt.Errorf("calculate md5sum failed: %s", err.Error()) - } - } - - // fetch signature, it will be returned when md5sum is not matched. - signature, err := models.DownloadOrgSignatureByMd5(orgCLAID, md5sum) - if err != nil { - return fmt.Errorf("download org's signature failed: %s", err.Error()) - } - - if signature == nil { - if md5sum == "" { - return fmt.Errorf("the org's signature has not been uploaded") - } - } else { - // write signature - if err := ioutil.WriteFile(sigFile, signature, 0644); err != nil { - return fmt.Errorf("write org's signature failed: %s", err.Error()) - } +func mergeCorporPDFSignaturePage(pythonBin, pdfFile, sigFile, outfile string) error { + if util.IsFileNotExist(sigFile) { + return fmt.Errorf("org signature file(%s) is not exist", sigFile) } // merge file - cmd := exec.Command( - pythonBin, "./util/merge-signature.py", - pdfFile, sigFile, outfile, - ) - _, err = cmd.Output() - if err != nil { + cmd := exec.Command(pythonBin, "./util/merge-signature.py", pdfFile, sigFile, outfile) + if _, err := cmd.Output(); err != nil { return fmt.Errorf("merge signature page of pdf failed: %s", err.Error()) } diff --git a/util/util.go b/util/util.go index 52e4f2e..4fc250a 100644 --- a/util/util.go +++ b/util/util.go @@ -31,10 +31,6 @@ func CorporCLAPDFFile(out, claOrgID, email, other string) string { return filepath.Join(out, f) } -func OrgSignaturePDFFILE(out, claOrgID string) string { - return filepath.Join(out, fmt.Sprintf("%s.pdf", claOrgID)) -} - func GenFilePath(dir, fileName string) string { return filepath.Join(dir, fileName) } diff --git a/worker/worker.go b/worker/worker.go index c264bf7..f72ee37 100644 --- a/worker/worker.go +++ b/worker/worker.go @@ -18,7 +18,7 @@ import ( var worker IEmailWorker type IEmailWorker interface { - GenCLAPDFForCorporationAndSendIt(orgCLA *models.OrgCLA, signing *models.CorporationSigning, cla *models.CLA) + GenCLAPDFForCorporationAndSendIt(string, *models.OrgCLA, *models.CorporationSigning, *models.CLA) SendSimpleMessage(orgEmail string, msg *email.EmailMessage) } @@ -43,7 +43,7 @@ func (this *emailWorker) Shutdown() { this.wg.Wait() } -func (this *emailWorker) GenCLAPDFForCorporationAndSendIt(orgCLA *models.OrgCLA, signing *models.CorporationSigning, cla *models.CLA) { +func (this *emailWorker) GenCLAPDFForCorporationAndSendIt(orgSignatureFile string, orgCLA *models.OrgCLA, signing *models.CorporationSigning, cla *models.CLA) { f := func() { defer func() { this.wg.Done() @@ -83,7 +83,7 @@ func (this *emailWorker) GenCLAPDFForCorporationAndSendIt(orgCLA *models.OrgCLA, } if file == "" || util.IsFileNotExist(file) { - file, err = this.pdfGenerator.GenPDFForCorporationSigning(orgCLA, signing, cla) + file, err = this.pdfGenerator.GenPDFForCorporationSigning(orgSignatureFile, orgCLA, signing, cla) if err != nil { next(fmt.Errorf( "Failed to generate pdf for corp signing(%s:%s:%s/%s): %s",