コード例 #1
0
func processIncoming(conn *printserver.ServerConn, msg *stomp.Message) error {
	var job tickets.Ticket
	if err := proto.Unmarshal(msg.Body, &job); err != nil {
		log.Printf("Received malformed job: %s", err)
		return err
	}

	jobId := "t-" + strconv.FormatUint(uint64(job.GetSubmitId()), 10)
	job.Team.Name = proto.String(strings.Replace(job.Team.GetName(), "#", "\\#", -1))

	var buf bytes.Buffer
	if err := documentTemplate.Execute(&buf, &templateData{
		Ticket: &job,
	}); err != nil {
		log.Println(err)
		return err
	}

	cBlob, err := tickets.NewBlob(buf.Bytes())
	if err != nil {
		return err
	}

	result := tickets.BinaryJob{
		JobId:   &jobId,
		Printer: job.Printer,
		Data:    cBlob,
	}

	return conn.Send(&result)
}
コード例 #2
0
ファイル: tex_processor.go プロジェクト: petemoore/printing3
func (s *server) processIncoming(conn *printserver.ServerConn, msg *stomp.Message) error {
	var job tickets.BinaryJob
	if err := proto.Unmarshal(msg.Body, &job); err != nil {
		log4go.Error("Received malformed job: %s", err)
		return err
	}

	jobDir := filepath.Join(s.Workdir, job.GetJobId())
	os.MkdirAll(jobDir, os.ModePerm) // err?

	sourceName := fmt.Sprintf("%s.tex", job.GetJobId())

	buf, err := job.GetData().Bytes()
	if err != nil {
		return err
	}
	if err = ioutil.WriteFile(filepath.Join(jobDir, sourceName), buf, os.ModePerm); err != nil {
		return err
	}

	cmd := exec.Command("latex", "-interaction=batchmode", sourceName)
	cmd.Dir, cmd.Stdin, cmd.Stdout, cmd.Stderr = jobDir, os.Stdin, os.Stdout, os.Stderr
	if err = cmd.Run(); err != nil {
		log.Printf("Latex error: %s\n", err)
		return nil
	}

	cmd = exec.Command("latex", "-interaction=batchmode", sourceName)
	cmd.Dir, cmd.Stdin, cmd.Stdout, cmd.Stderr = jobDir, os.Stdin, os.Stdout, os.Stderr
	if err = cmd.Run(); err != nil {
		log.Printf("Latex error: %s\n", err)
		return nil
	}

	dviName := fmt.Sprintf("%s.dvi", job.GetJobId())
	cmd = exec.Command("dvips", "-t", "a4", dviName)
	cmd.Dir, cmd.Stdin, cmd.Stdout, cmd.Stderr = jobDir, os.Stdin, os.Stdout, os.Stderr
	if err = cmd.Run(); err != nil {
		log.Printf("Dvips error: %s\n", err)
		return nil
	}

	psName := fmt.Sprintf("%s.ps", job.GetJobId())
	content, err := ioutil.ReadFile(filepath.Join(jobDir, psName))
	if err != nil {
		log.Printf("Where's my file? %s\n", err)
		return nil
	}

	cBlob, err := tickets.NewBlob(content)
	if err != nil {
		return err
	}

	result := tickets.BinaryJob{
		JobId:   job.JobId,
		Printer: job.Printer,
		Data:    cBlob,
	}

	content, err = proto.Marshal(&result)
	if err != nil {
		return err
	}

	return conn.Send(&result)
}
コード例 #3
0
func (s *server) processIncoming(conn *printserver.ServerConn, msg *stomp.Message) error {
	var job tickets.PrintJob
	if err := proto.Unmarshal(msg.Body, &job); err != nil {
		log4go.Error("Received malformed job: %s", err)
		return err
	}

	jobId := "s-" + strconv.FormatUint(uint64(job.GetJobId()), 10)
	job.Team.Name = proto.String(texEscape(job.Team.GetName()))

	jobDir := filepath.Join(s.Workdir, jobId)
	os.MkdirAll(jobDir, os.ModePerm) // err?

	sourceLang := filepath.Ext(job.GetFilename())
	if sourceLang != "" {
		sourceLang = s.languages[sourceLang[1:]]
	}
	if sourceLang == "" {
		sourceLang = "txt"
	}

	sourceName := fmt.Sprintf("%s-source.%s", jobId, sourceLang)
	outputName := fmt.Sprintf("%s-hl.tex", jobId)
	styleName := fmt.Sprintf("%s-style.sty", jobId)

	buf, err := job.GetData().Bytes()
	if err != nil {
		return err
	}

	if err = ioutil.WriteFile(filepath.Join(jobDir, sourceName), buf, os.ModePerm); err != nil {
		return err
	}

	sourceCharset := job.GetCharset()
	if sourceCharset == "" {
		sourceCharset = "cp1251"
	}

	/*args := []string{"--out-format=latex",
		"--syntax=" + sourceLang,
		"--style=print",
		"--input=" + sourceName,
		"--output=" + outputName,
		"--fragment",
		"--replace-quotes",
		"--replace-tabs=4",
		"--wrap",
		"--wrap-no-numbers",
		"--encoding=" + sourceCharset,
		"--style-outfile=" + styleName}
	if sourceLang == "txt" {
		args = append(args, "--line-numbers")
	}
	*/
	args := []string{"-l", "text", "-f", "latex", "-O", "linenos=1,tabsize=4", "-o", outputName, sourceName}
	cmd := exec.Command("pygmentize", args...)
	cmd.Dir, cmd.Stdin, cmd.Stdout, cmd.Stderr = jobDir, os.Stdin, os.Stdout, os.Stderr
	if err = cmd.Run(); err != nil {
		return err
	}

	cmd = exec.Command("pygmentize", "-f", "latex", "-S", "bw")
	out, err := cmd.Output()
	if err != nil {
		return err
	}

	if err = ioutil.WriteFile(filepath.Join(jobDir, styleName), out, os.ModePerm); err != nil {
		return err
	}

	job.Filename = proto.String(texEscape(job.GetFilename()))
	data := templateData{
		PrintJob: &job,
	}

	var contentSource io.Reader
	if contentFile, err := os.Open(filepath.Join(jobDir, outputName)); err == nil {
		contentSource = contentFile
		defer contentFile.Close()
	} else {
		return err
	}

	contents, err := ioutil.ReadAll(contentSource)
	if err != nil {
		return err
	}

	data.IncludeText = string(contents)
	contents, err = ioutil.ReadFile(filepath.Join(jobDir, styleName))
	if err == nil && len(contents) != 0 {
		data.StyleText = string(contents)
	}

	var output bytes.Buffer

	if err = documentTemplate.Execute(&output, &data); err != nil {
		return err
	}

	cBlob, err := tickets.NewBlob(output.Bytes())
	if err != nil {
		return err
	}

	result := tickets.BinaryJob{
		JobId:   &jobId,
		Printer: job.Printer,
		Data:    cBlob,
	}

	return conn.Send(&result)
}