Skip to content

Commit

Permalink
modify work with template
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkCherepovskyi committed Oct 2, 2023
1 parent 5f28e2f commit 9803292
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 81 deletions.
4 changes: 2 additions & 2 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ networks:
ipfs_pr_key: ""

sbt:
external_url: "https://dlt-academy.com/certificates"
external_url: "https://distributed.education/certificates"

google:
secret_path: "./client.json"
Expand All @@ -35,7 +35,7 @@ tables:

qr_code:
qr_path: "./qr/"
template: "message:\n%s\n\naddress:\n%s\n\nsignature:\n%s\n\ncertificate page:\nhttps://dlt-academy.com/certificates"
template: "message:\n%s\n\naddress:\n%s\n\nsignature:\n%s\n\ncertificate page:\nhttps://distributed.education/certificates"

templates:
list:
Expand Down
73 changes: 36 additions & 37 deletions internal/service/api/handlers/create_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@ import (
"gitlab.com/distributed_lab/ape/problems"
"gitlab.com/tokend/course-certificates/ccp/internal/data"
"gitlab.com/tokend/course-certificates/ccp/internal/service/api/requests"
"gitlab.com/tokend/course-certificates/ccp/internal/service/core/pdf"
"gitlab.com/tokend/course-certificates/ccp/resources"
"net/http"
"strings"
)

func CreateTemplate(w http.ResponseWriter, r *http.Request) {
template, backgroundImg, req, err := requests.NewGenerateTemplate(r)
template, _, req, err := requests.NewGenerateTemplate(r)
if err != nil {
Log(r).WithError(err).Error("failed to generate template")
ape.RenderErr(w, problems.BadRequest(err)...)
return
}
defaultData := pdf.DefaultData
client, err := MasterQ(r).ClientQ().FilterByName(req.Data.Relationships.User).Get()
if err != nil {
Log(r).WithError(err).Error("failed to get client")
Expand All @@ -31,36 +30,36 @@ func CreateTemplate(w http.ResponseWriter, r *http.Request) {
ape.RenderErr(w, problems.NotFound())
return
}

if template.Width == 0 || template.High == 0 {
tp := pdf.DefaultTemplateTall
_, _, imgBytes, err := tp.Prepare(defaultData, pdf.NewPDFConfig(Config(r)), MasterQ(r), backgroundImg, client.ID, StaticConfiger(r).Location)
if err != nil {
Log(r).WithError(err).Error("failed to prepare pdf")
ape.RenderErr(w, problems.InternalError())
return
}
ape.Render(w, newTemplateImageResp(imgBytes))
return
}

file := pdf.NewPDF(template.High, template.Width)

file.SetName(template.Name.X, template.Name.Y, template.Name.FontSize, template.Name.Font)
file.SetDate(template.Date.X, template.Date.Y, template.Date.FontSize, template.Date.Font)
file.SetCourse(template.Course.X, template.Course.Y, template.Course.FontSize, template.Course.Font)
file.SetCredits(template.Credits.X, template.Credits.Y, template.Credits.FontSize, template.Credits.Font)
file.SetExam(template.Exam.X, template.Exam.Y, template.Exam.FontSize, template.Exam.Font)
file.SetLevel(template.Level.X, template.Level.Y, template.Level.FontSize, template.Level.Font)
file.SetSerialNumber(template.SerialNumber.X, template.SerialNumber.Y, template.SerialNumber.FontSize, template.SerialNumber.Font)
file.SetPoints(template.Points.X, template.Points.Y, template.Points.FontSize, template.Points.Font)
file.SetQR(template.QR.X, template.QR.Y, template.QR.FontSize, template.QR.High, template.Width)
_, _, imgBytes, err := template.Prepare(defaultData, pdf.NewPDFConfig(Config(r)), MasterQ(r), backgroundImg, client.ID, StaticConfiger(r).Location)
if err != nil {
Log(r).WithError(err).Error("failed to prepare pdf")
ape.RenderErr(w, problems.InternalError())
return
}
//
//if template.Width == 0 || template.Height == 0 {
// tp := pdf.DefaultTemplateTall
// _, _, imgBytes, err := tp.Prepare(defaultData, pdf.NewPDFConfig(Config(r)), MasterQ(r), backgroundImg, client.ID, StaticConfiger(r).Location)
// if err != nil {
// Log(r).WithError(err).Error("failed to prepare pdf")
// ape.RenderErr(w, problems.InternalError())
// return
// }
// ape.Render(w, newTemplateImageResp(imgBytes))
// return
//}
//
//file := pdf.NewPDF(template.Height, template.Width)
//
//file.SetName(template.Name.X, template.Name.Y, template.Name.FontSize, template.Name.Font)
//file.SetDate(template.Date.X, template.Date.Y, template.Date.FontSize, template.Date.Font)
//file.SetCourse(template.Course.X, template.Course.Y, template.Course.FontSize, template.Course.Font)
//file.SetCredits(template.Credits.X, template.Credits.Y, template.Credits.FontSize, template.Credits.Font)
//file.SetExam(template.Exam.X, template.Exam.Y, template.Exam.FontSize, template.Exam.Font)
//file.SetLevel(template.Level.X, template.Level.Y, template.Level.FontSize, template.Level.Font)
//file.SetSerialNumber(template.SerialNumber.X, template.SerialNumber.Y, template.SerialNumber.FontSize, template.SerialNumber.Font)
//file.SetPoints(template.Points.X, template.Points.Y, template.Points.FontSize, template.Points.Font)
//file.SetQR(template.QR.X, template.QR.Y, template.QR.FontSize, template.QR.Height, template.Width)
//_, _, imgBytes, err := template.Prepare(defaultData, pdf.NewPDFConfig(Config(r)), MasterQ(r), backgroundImg, client.ID, StaticConfiger(r).Location)
//if err != nil {
// Log(r).WithError(err).Error("failed to prepare pdf")
// ape.RenderErr(w, problems.InternalError())
// return
//}
if req.Data.Attributes.IsCompleted {
templateBytes, err := json.Marshal(template)
if err != nil {
Expand All @@ -71,17 +70,17 @@ func CreateTemplate(w http.ResponseWriter, r *http.Request) {

err = MasterQ(r).TemplateQ().Insert(&data.Template{
Template: templateBytes,
//ImgBytes: backgroundImg,
Name: req.Data.Attributes.TemplateName,
UserID: client.ID,
ImgBytes: []byte(strings.Replace(req.Data.Attributes.BackgroundImg, "data:image/png;base64,", "", 1)),
Name: req.Data.Attributes.TemplateName,
UserID: client.ID,
})
if err != nil {
Log(r).WithError(err).Error("failed to insert template")
ape.RenderErr(w, problems.InternalError())
return
}
}
ape.Render(w, newTemplateImageResp(imgBytes))
w.WriteHeader(http.StatusNoContent)
return
}

Expand Down
2 changes: 1 addition & 1 deletion internal/service/api/handlers/get_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func GetUsers(w http.ResponseWriter, r *http.Request) {
}

Log(r).Error("failed to parse table: Errors:", errs)
ape.RenderErr(w, problems.BadRequest(err)...)
ape.RenderErr(w, problems.Unauthorized())
return
}

Expand Down
4 changes: 2 additions & 2 deletions internal/service/api/requests/create_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"gitlab.com/tokend/course-certificates/ccp/internal/service/core/pdf"
"gitlab.com/tokend/course-certificates/ccp/resources"
"image"
"image/jpeg"
"image/png"
"net/http"
"strings"
)
Expand Down Expand Up @@ -52,7 +52,7 @@ func base64toJpg(data string) ([]byte, error) {
}

buf := new(bytes.Buffer)
if err = jpeg.Encode(buf, m, &jpeg.Options{Quality: 75}); err != nil {
if err = png.Encode(buf, m); err != nil {
return nil, err
}

Expand Down
18 changes: 14 additions & 4 deletions internal/service/core/pdf/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,14 @@ func (c *Container) Generate() error {
files = append(files, google.FilesBytes{File: file, Name: name, ID: user.ID, Type: "image/svg+xml"})

pdf := PDF{}
certificate := pdf.SetTemplateData(DefaultTemplateTall)
certificateTemplate, err := pdf.InitTemplate(c.masterQ, user.CourseTitle, c.owner.ID)
if err != nil {
return errors.Wrap(err, "failed to get template")
}

certificate := pdf.SetTemplateData(*certificateTemplate)

pdfData := NewData(user.Participant, user.CourseTitle, "45 hours / 1.5 ECTS Credit", user.Points, user.SerialNumber, user.Date, img, user.Note, "", "")
pdfData := NewData(user.Participant, user.CourseTitle, certificateTemplate.Credits.Text, user.Points, user.SerialNumber, user.Date, img, user.Note, "", "")
fileBytes, name, certificateImg, err := certificate.Prepare(pdfData, NewPDFConfig(c.config), c.masterQ, nil, c.owner.ID, c.config.StaticConfig().Location)
if err != nil {
return errors.Wrap(err, "failed to create pdf")
Expand Down Expand Up @@ -105,9 +110,14 @@ func (c *Container) Update() error {
files = append(files, google.FilesBytes{File: file, Name: name, ID: user.ID, Type: "image/svg+xml"})

pdf := PDF{}
certificate := pdf.SetTemplateData(DefaultTemplateTall)
certificateTemplate, err := pdf.InitTemplate(c.masterQ, user.CourseTitle, c.owner.ID)
if err != nil {
return errors.Wrap(err, "failed to get template")
}

certificate := pdf.SetTemplateData(*certificateTemplate)

pdfData := NewData(user.Participant, user.CourseTitle, "45 hours / 1.5 ECTS Credit", user.Points, user.SerialNumber, user.Date, img, user.Note, "", "")
pdfData := NewData(user.Participant, user.CourseTitle, certificateTemplate.Credits.Text, user.Points, user.SerialNumber, user.Date, img, user.Note, "", "")
fileBytes, name, certificateImg, err := certificate.Prepare(pdfData, NewPDFConfig(c.config), c.masterQ, nil, c.owner.ID, c.config.StaticConfig().Location)
if err != nil {
return errors.Wrap(err, "failed to create pdf")
Expand Down
25 changes: 13 additions & 12 deletions internal/service/core/pdf/default_data.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package pdf

var DefaultTemplateNormal = PDF{
High: 595,
Width: 842,
Height: 595,
Width: 842,
Name: Field{
X: 200,
Y: 217,
Expand Down Expand Up @@ -40,10 +40,10 @@ var DefaultTemplateNormal = PDF{
Font: "regular",
},
QR: Field{
X: 658,
Y: 106,
High: 114,
Width: 114,
X: 658,
Y: 106,
Height: 114,
Width: 114,
},
Exam: Field{
X: 300,
Expand All @@ -60,8 +60,8 @@ var DefaultTemplateNormal = PDF{
}

var DefaultTemplateTall = PDF{
High: 1190,
Width: 1684,
Height: 1190,
Width: 1684,
Name: Field{
Y: 434,
FontSize: 56,
Expand All @@ -77,6 +77,7 @@ var DefaultTemplateTall = PDF{
Y: 112,
FontSize: 24,
Font: "regular",
Text: "45 hours / 1.5 ECTS Credit",
},
Points: Field{
X: 140,
Expand All @@ -99,10 +100,10 @@ var DefaultTemplateTall = PDF{
Font: "regular",
},
QR: Field{
X: 1316,
Y: 212,
High: 228,
Width: 228,
X: 1316,
Y: 212,
Height: 228,
Width: 228,
},
Exam: Field{
Y: 600,
Expand Down
Loading

0 comments on commit 9803292

Please sign in to comment.