Esempio n. 1
1
// Apply writes the pdf file.
func (r ResumePDF) Apply(req *revel.Request, resp *revel.Response) {
	resp.Out.Header().Set("Content-Disposition", `inline; filename="rafael_chargels_resume.pdf"`)

	pdf := gofpdf.New("P", "pt", "letter", "./fonts/")

	header := func(text string) {
		pdf.Ln(4)
		pdf.SetFont("Arial", "B", 16)
		pdf.CellFormat(0, 30, strings.ToUpper(text), "", 1, "L", false, 0, "")
	}

	writeBullet := func(text string) {
		pdf.SetFont("Arial", "", 11)
		pdf.Ln(4)
		pdf.SetFillColor(0, 0, 0)
		pdf.CellFormat(20, 15, "", "", 0, "L", false, 0, "")

		x := pdf.GetX()
		y := pdf.GetY()
		pdf.Circle(x-5, y+7, 2, "F")
		pdf.MultiCell(0, 15, cleanStr(text), "", "L", false)
	}

	writeEducation := func(education domain.Education) {
		pdf.SetFont("Arial", "B", 11)
		pdf.Ln(4)
		pdf.CellFormat(0, 15, education.Institution+" - "+education.Year, "", 1, "L", false, 0, "")
		pdf.SetFont("Arial", "", 11)
		pdf.CellFormat(0, 15, education.Location, "", 1, "L", false, 0, "")
		pdf.CellFormat(0, 15, education.Degree, "", 1, "L", false, 0, "")
	}

	writeJob := func(job domain.Job) {
		pdf.SetFont("Arial", "B", 11)
		pdf.Ln(4)
		pdf.CellFormat(0, 15, job.Start+" - "+job.End, "", 1, "L", false, 0, "")
		pdf.CellFormat(0, 15, job.Title, "", 1, "L", false, 0, "")
		pdf.CellFormat(0, 15, job.Company, "", 1, "L", false, 0, "")
		pdf.SetFont("Arial", "BI", 11)
		pdf.CellFormat(0, 15, job.Location, "", 1, "L", false, 0, "")
		pdf.SetFont("Arial", "", 11)
		pdf.Ln(8)
		pdf.CellFormat(20, 15, "", "", 0, "L", false, 0, "")
		pdf.MultiCell(0, 15, cleanStr(job.Description), "", "L", false)
		pdf.Ln(8)
		pdf.SetFont("Arial", "I", 11)
		pdf.CellFormat(20, 15, "", "", 0, "L", false, 0, "")
		pdf.MultiCell(0, 15, cleanStr(job.Skills), "", "L", false)
		pdf.Ln(8)
	}

	pdf.SetTitle("Rafael Pacheco Chargel - Resume", true)
	pdf.SetAuthor("Rafael Pacheco Chargel", true)
	pdf.SetMargins(30, 30, 30)
	pdf.SetAutoPageBreak(true, 30)
	pdf.AddPage()
	pdf.SetFont("Helvetica", "B", 11)
	pdf.CellFormat(0, 15, "Rafael Pacheco Chargel", "", 1, "R", false, 0, "")
	pdf.SetFont("Helvetica", "", 11)
	pdf.CellFormat(0, 15, "http://zcarioca.net", "", 1, "R", false, 0, "")
	header("Summary")

	pdf.SetFont("Arial", "", 11)
	pdf.MultiCell(0, 15, cleanStr(r.resume.Summary), "", "L", false)

	header("Experience")
	for _, job := range r.resume.Experience.Jobs {
		writeJob(job)
	}

	header("Other Skills")
	writeBullet("Skills: " + r.resume.AdditionalSkills)
	writeBullet("Languages: " + r.resume.AdditionalLanguages)

	header("Education")
	writeEducation(r.resume.Education)

	resp.WriteHeader(http.StatusOK, "application/pdf")
	pdf.Output(resp.Out)
}
Esempio n. 2
0
func (r *ConvertResult) Apply(req *revel.Request, resp *revel.Response) {
	resp.WriteHeader(http.StatusOK, "text/html; charset=utf-8")

	resp.Out.Write([]byte("<pre><code>\n"))

	logger := func(line string) {
		resp.Out.Write([]byte(line + "\n"))

		if revel.DevMode {
			fmt.Println(line)
		}

		if f, ok := resp.Out.(http.Flusher); ok {
			f.Flush()
		}
	}

	shortUrl, err := models.Convert(r.url, r.koofr, logger)

	if err != nil {
		revel.ERROR.Println(err)
		return
	}

	resp.Out.Write([]byte("</pre></code>\n"))

	resp.Out.Write([]byte("<br /><a href=\"" + shortUrl + "\">" + shortUrl + "</a>"))

	return
}
Esempio n. 3
0
func (r *StaticBinaryResult) Apply(req *revel.Request, resp *revel.Response) {
	// If we have a ReadSeeker, delegate to http.ServeContent
	if rs, ok := r.Reader.(io.ReadSeeker); ok {
		// http.ServeContent doesn't know about response.ContentType, so we set the respective header.
		if resp.ContentType != "" {
			resp.Out.Header().Set("Content-Type", resp.ContentType)
		} else {
			contentType := revel.ContentTypeByFilename(r.Name)
			resp.Out.Header().Set("Content-Type", contentType)
		}
		http.ServeContent(resp.Out, req.Request, r.Name, r.ModTime, rs)
	} else {
		// Else, do a simple io.Copy.
		if r.Length != -1 {
			resp.Out.Header().Set("Content-Length", strconv.FormatInt(r.Length, 10))
		}
		resp.WriteHeader(http.StatusOK, revel.ContentTypeByFilename(r.Name))
		io.Copy(resp.Out, r.Reader)
	}

	// Close the Reader if we can
	if v, ok := r.Reader.(io.Closer); ok {
		v.Close()
	}
}
Esempio n. 4
0
func (r jsonApiRES) Apply(req *revel.Request, resp *revel.Response) {

	resp.WriteHeader(http.StatusOK, "application/vnd.api+json")

	if err := jsonapi.MarshalManyPayload(resp.Out, r); err != nil {
		http.Error(resp.Out, err.Error(), 500)
	}

}
Esempio n. 5
0
func (c CommonResult) Apply(req *revel.Request, resp *revel.Response) {
	if c.Code == 0 {
		c.Code = 200
	}
	if len(c.ContentType) == 0 {
		c.ContentType = ContentTypeHTML
	}
	resp.WriteHeader(c.Code, c.ContentType)
	resp.Out.Write(c.Data)
}
Esempio n. 6
0
func (r JsonErrorResult) Apply(req *revel.Request, resp *revel.Response) {
	var b []byte
	resultJson := JsonError{Error: r.ErrorMessage}

	b, err := json.Marshal(resultJson)

	if err != nil {
		revel.ErrorResult{Error: err}.Apply(req, resp)
		return
	}

	resp.WriteHeader(r.StatusCode, "application/json; charset=utf-8")
	resp.Out.Write(b)
}
Esempio n. 7
0
func (r *RenderTemplateResult) render(req *revel.Request, resp *revel.Response, wr io.Writer) {
	err := r.Template.Execute(wr, r.RenderArgs)
	if err == nil {
		return
	}

	var templateContent []string
	templateName, line, description := parseTemplateError(err)
	var content = ""
	if templateName == "" {
		templateName = r.Template.Name()
		content = r.PathContent[templateName]
	} else {
		content = r.PathContent[templateName]
	}
	if content != "" {
		templateContent = strings.Split(content, "\n")
	}

	compileError := &revel.Error{
		Title:       "Template Execution Error",
		Path:        templateName,
		Description: description,
		Line:        line,
		SourceLines: templateContent,
	}

	// 这里, 错误!!
	// 这里应该导向到本主题的错误页面
	resp.Status = 500
	ErrorResult{r.RenderArgs, compileError, r.IsPreview, r.CurBlogTpl}.Apply(req, resp)
}
Esempio n. 8
0
func (r *StreamConvertResult) Apply(req *revel.Request, resp *revel.Response) {
	resp.Out.Header().Add("Access-Control-Allow-Origin", "*")

	resp.WriteHeader(http.StatusOK, "audio/mp3")

	logger := func(line string) {
		if revel.DevMode {
			fmt.Println(line)
		}
	}

	tmpDir, err := ioutil.TempDir("", "youtube-to-koofr")
	if err != nil {
		revel.ERROR.Println(err)
		return
	}

	defer func() {
		os.RemoveAll(tmpDir)
	}()

	fileName, err := models.YoutubeDl(r.url, tmpDir, logger)
	if err != nil {
		revel.ERROR.Println(err)
		return
	}

	filePath := path.Join(tmpDir, fileName)

	f, err := os.Open(filePath)

	if err != nil {
		revel.ERROR.Println(err)
		return
	}

	_, err = io.Copy(resp.Out, f)

	if err != nil {
		revel.ERROR.Println(err)
		return
	}

	return
}
Esempio n. 9
0
func (r *RenderTemplateResult) Apply(req *revel.Request, resp *revel.Response) {
	// Handle panics when rendering templates.
	defer func() {
		if err := recover(); err != nil {
		}
	}()

	chunked := revel.Config.BoolDefault("results.chunked", false)

	// If it's a HEAD request, throw away the bytes.
	out := io.Writer(resp.Out)
	if req.Method == "HEAD" {
		out = ioutil.Discard
	}

	// In a prod mode, write the status, render, and hope for the best.
	// (In a dev mode, always render to a temporary buffer first to avoid having
	// error pages distorted by HTML already written)
	if chunked && !revel.DevMode {
		resp.WriteHeader(http.StatusOK, "text/html; charset=utf-8")
		r.render(req, resp, out) // 这里!!!
		return
	}

	// Render the template into a temporary buffer, to see if there was an error
	// rendering the template.  If not, then copy it into the response buffer.
	// Otherwise, template render errors may result in unpredictable HTML (and
	// would carry a 200 status code)
	var b bytes.Buffer
	r.render(req, resp, &b)
	if !chunked {
		resp.Out.Header().Set("Content-Length", strconv.Itoa(b.Len()))
	}
	resp.WriteHeader(http.StatusOK, "text/html; charset=utf-8")
	b.WriteTo(out)
}
Esempio n. 10
0
// 错误显示出
func (r ErrorResult) Apply(req *revel.Request, resp *revel.Response) {
	format := req.Format
	status := resp.Status
	if status == 0 {
		status = http.StatusInternalServerError
	}

	contentType := revel.ContentTypeByFilename("xxx." + format)
	if contentType == revel.DefaultFileContentType {
		contentType = "text/plain"
	}

	// Get the error template.
	var err error
	templatePath := fmt.Sprintf("errors/%d.%s", status, format)
	err = nil
	//	tmpl, err := revel.MainTemplateLoader.Template("index.html") // 这里找到错误页面主题

	// This func shows a plaintext error message, in case the template rendering
	// doesn't work.
	showPlaintext := func(err error) {
		revel.PlaintextErrorResult{fmt.Errorf("Server Error:\n%s\n\n"+
			"Additionally, an error occurred when rendering the error page:\n%s",
			r.Error, err)}.Apply(req, resp)
	}

	// 根据是否是preview来得到404模板
	// 是, 则显示系统的错误信息, blog-500.html
	var tmpl *template.Template
	if r.IsPreview {
		tmpl = r.CurBlogTpl.Template.Lookup("blog/404.html")
	} else {
		tmpl = r.CurBlogTpl.Template.Lookup("404.html")
	}
	if tmpl == nil {
		if err == nil {
			err = fmt.Errorf("Couldn't find template %s", templatePath)
		}
		showPlaintext(err)
		return
	}

	// If it's not a revel error, wrap it in one.
	var revelError *revel.Error
	switch e := r.Error.(type) {
	case *revel.Error:
		revelError = e
	case error:
		revelError = &revel.Error{
			Title:       "Server Error",
			Description: e.Error(),
		}
	}

	if revelError == nil {
		panic("no error provided")
	}

	if r.RenderArgs == nil {
		r.RenderArgs = make(map[string]interface{})
	}
	r.RenderArgs["Error"] = revelError
	r.RenderArgs["Router"] = revel.MainRouter

	// 不是preview就不要显示错误了
	if r.IsPreview {
		var b bytes.Buffer
		out := io.Writer(resp.Out)
		//	out = ioutil.Discard
		err = tmpl.Execute(&b, r.RenderArgs)
		resp.WriteHeader(http.StatusOK, "text/html; charset=utf-8")
		b.WriteTo(out)
	}
}
Esempio n. 11
0
func (r OctetResponse) Apply(req *revel.Request, resp *revel.Response) {
	resp.WriteHeader(http.StatusOK, "application/octet-stream")
	resp.Out.Write(r)
}
Esempio n. 12
0
func (r TextResponse) Apply(req *revel.Request, resp *revel.Response) {
	resp.WriteHeader(http.StatusOK, "application/json")
	resp.Out.Write(r)
}
Esempio n. 13
0
func (r RenderHtmlResult) Apply(req *revel.Request, resp *revel.Response) {
	resp.WriteHeader(http.StatusOK, "text/html")
	resp.Out.Write([]byte(r.html))
}
Esempio n. 14
0
func (r Ca) Apply(req *revel.Request, resp *revel.Response) {
	resp.WriteHeader(http.StatusOK, "image/png")
}
Esempio n. 15
0
func (jr JpegResponse) Apply(req *revel.Request, resp *revel.Response) {
	resp.WriteHeader(http.StatusOK, "image/jpeg")
	resp.Out.Write([]byte(jr))
}
Esempio n. 16
0
func (r LoginResult) Apply(req *revel.Request, resp *revel.Response) {
	resp.WriteHeader(r.StatusCode, "text/html")
	resp.Out.Write([]byte(r.Message))
}
Esempio n. 17
0
func (r EmptyResult) Apply(req *revel.Request, resp *revel.Response) {
	resp.WriteHeader(r.StatusCode, "text/html; charset=utf-8")
}