예제 #1
0
파일: auth.go 프로젝트: robxu9/reservoir
func Auth_BasicCheck(username, password string) (string, error) {
	hashed := checksum.String(password, crypto.SHA512)
	return Auth_HashedCheck(username, hashed)
}
예제 #2
0
파일: main.go 프로젝트: santihbc/latex
/*
	Renders a LaTeX string, returns a PNG image path.
*/
func (self *Renderer) Render(latex string) (string, error) {
	var err error

	name := checksum.String(latex, crypto.SHA1)

	// Relative output directory.
	relPath := name[0:4] + PS + name[4:8] + PS + name[8:12] + PS + name[12:16] + PS + name[16:] + ".png"

	// Setting output directory.
	pngPath := OutputDirectory + PS + relPath

	err = os.MkdirAll(path.Dir(pngPath), 0755)

	if err != nil {
		return "", err
	}

	// Does the output file already exists?
	if self.UseCache == true {
		_, err = os.Stat(pngPath)

		if err == nil {
			return relPath, nil
		}
	}

	// Setting working directory.
	workdir := WorkingDirectory + PS + name

	err = os.MkdirAll(workdir, 0755)

	if err != nil {
		return "", err
	}

	// Will clean the directory at the end.
	defer os.RemoveAll(workdir)

	// Writing LaTeX to a file.
	texFile, err := os.Create(workdir + PS + "output.tex")

	if err != nil {
		return "", err
	}

	defer texFile.Close()

	_, err = io.WriteString(texFile, latex)

	if err != nil {
		return "", err
	}

	// Temp files.
	dviPath := workdir + PS + "output.dvi"
	epsPath := workdir + PS + "output.eps"

	// LaTeX toolchain
	batch := []*exec.Cmd{
		exec.Command(
			"latex",
			"-no-shell-escape",
			"-interaction=batchmode",
			fmt.Sprintf("-output-directory=%s", workdir),
			texFile.Name(),
		),
		exec.Command(
			"dvips",
			"-G",
			"-R2",
			"-E",
			dviPath,
			"-o",
			epsPath,
		),
		exec.Command(
			"convert",
			"+adjoin",
			"-density",
			to.String(self.Density),
			"-antialias",
			epsPath,
			pngPath,
		),
	}

	// Executing toolchain.
	os.Setenv("openout_any", "p")

	for _, cmd := range batch {
		err = cmd.Run()

		if err != nil {
			// Trying to catch error
			logPath := workdir + PS + "output.log"

			logFile, err := os.Open(logPath)

			if err == nil {
				buf := bytes.NewBuffer(nil)
				buf.ReadFrom(logFile)

				logFile.Close()

				if buf.Len() > 0 {
					return "", errors.New(string(buf.Bytes()))
				}
			}

			return "", err
		}
	}

	// Had success?
	stat, err := os.Stat(pngPath)

	if err != nil || stat == nil {
		return "", errors.New("Failed to create PNG file.")

	}

	return relPath, err
}