Beispiel #1
0
// Base64 ...
func Base64(w http.ResponseWriter, r *http.Request) {

	render := marmoset.Render(w, true)

	var body = new(struct {
		Base64    string `json:"base64"`
		Trim      string `json:"trim"`
		Languages string `json:"languages"`
		Whitelist string `json:"whitelist"`
	})

	err := json.NewDecoder(r.Body).Decode(body)
	if err != nil {
		render.JSON(http.StatusBadRequest, err)
		return
	}

	tempfile, err := ioutil.TempFile("", config.AppName()+"-")
	if err != nil {
		render.JSON(http.StatusInternalServerError, err)
		return
	}
	defer func() {
		tempfile.Close()
		os.Remove(tempfile.Name())
	}()

	if len(body.Base64) == 0 {
		render.JSON(http.StatusBadRequest, fmt.Errorf("base64 string required"))
		return
	}
	body.Base64 = regexp.MustCompile("data:image\\/png;base64,").ReplaceAllString(body.Base64, "")
	b, err := base64.StdEncoding.DecodeString(body.Base64)
	if err != nil {
		render.JSON(http.StatusBadRequest, err)
		return
	}
	tempfile.Write(b)

	// TODO: refactor gosseract
	body.Languages = "eng"

	result := gosseract.Must(gosseract.Params{
		Src:       tempfile.Name(),
		Languages: body.Languages,
		Whitelist: body.Whitelist,
	})
	render.JSON(http.StatusOK, map[string]interface{}{
		"result":  strings.Trim(result, body.Trim),
		"version": config.Version(),
	})
}
Beispiel #2
0
// Index ...
func Index(w http.ResponseWriter, r *http.Request) {
	tmpl, err := template.New("").Parse(view)
	if err != nil {
		m.Render(w, true).JSON(http.StatusInternalServerError, m.P{
			"message": err.Error(),
		})
		return
	}
	tmpl.Execute(w, map[string]interface{}{
		"AppName": config.AppName(),
	})
	/* TODO: marmoset.View doesn't work in Heroku instance :(
	marmoset.Render(w).HTML("index", map[string]interface{}{
		"AppName": config.AppName(),
	})
	*/
}
Beispiel #3
0
// FileUpload ...
func FileUpload(w http.ResponseWriter, r *http.Request) {

	render := marmoset.Render(w, true)

	whitelist := r.FormValue("whitelist")
	trim := r.FormValue("trim")
	// Get uploaded file
	r.ParseMultipartForm(32 << 20)
	// upload, h, err := r.FormFile("file")
	upload, _, err := r.FormFile("file")
	if err != nil {
		render.JSON(http.StatusBadRequest, err)
		return
	}
	defer upload.Close()

	// Create physical file
	tempfile, err := ioutil.TempFile("", config.AppName()+"-")
	if err != nil {
		render.JSON(http.StatusBadRequest, err)
		return
	}
	defer func() {
		tempfile.Close()
		os.Remove(tempfile.Name())
	}()

	// Make uploaded physical
	if _, err := io.Copy(tempfile, upload); err != nil {
		render.JSON(http.StatusInternalServerError, err)
		return
	}

	result := gosseract.Must(gosseract.Params{
		Src:       tempfile.Name(),
		Languages: "eng",
		Whitelist: whitelist,
	})

	render.JSON(http.StatusOK, map[string]interface{}{
		"result":  strings.Trim(result, trim),
		"version": config.Version(),
	})
}
Beispiel #4
0
// Status ...
func Status(w http.ResponseWriter, r *http.Request) {
	marmoset.Render(w, true).JSON(http.StatusOK, map[string]interface{}{
		"message": "Hello!",
		"version": config.Version(),
	})
}
Beispiel #5
0
// Index ...
func Index(w http.ResponseWriter, r *http.Request) {
	marmoset.Render(w).HTML("index", map[string]interface{}{
		"AppName": config.AppName(),
	})
}
Beispiel #6
0
func uploadHandler(w http.ResponseWriter, r *http.Request) {

	render := marmoset.Render(w, true)

	file, _, err := r.FormFile("file")
	if err != nil {
		render.JSON(http.StatusBadRequest, err)
		return
	}
	defer file.Close()

	buff := make([]byte, 512) // why 512 bytes ? see http://golang.org/pkg/net/http/#DetectContentType
	_, err = file.Read(buff)
	if err != nil {
		render.JSON(http.StatusBadRequest, err)
		return
	}

	filetype := http.DetectContentType(buff)
	if filetype != "image/jpeg" {
		render.JSON(http.StatusBadRequest, map[string]string{"error": filetype + " detected, not a image/jpeg"})
		return
	} else {
		// reset for io.Copy
		_, err = file.Seek(0, 0)
		if err != nil {
			render.JSON(http.StatusInternalServerError, err)
			return
		}

	}

	tmpfile, err := os.Create("tmp/" + strconv.FormatInt(time.Now().UTC().Unix(), 10) + ".jpg")
	if err != nil {
		render.JSON(http.StatusInternalServerError, err)
		return
	}

	if _, err := io.Copy(tmpfile, file); err != nil {
		render.JSON(http.StatusInternalServerError, err)
		return
	}
	tmpfile.Close()

	pwd, _ := os.Getwd()
	log.Println("docker", "run", "--rm", "-v", pwd+"/tmp:/data:ro", "openalpr/openalpr", "-j", "-c", "sg", path.Base(tmpfile.Name()))
	cmd := exec.Command("docker", "run", "--rm", "-v", pwd+"/tmp:/data:ro", "openalpr/openalpr", "-j", "-c", "sg", path.Base(tmpfile.Name()))
	var stdout bytes.Buffer
	var stderr bytes.Buffer
	cmd.Stdout = &stdout
	cmd.Stderr = &stderr
	err = cmd.Run()
	if err != nil {
		http.Error(w, err.Error()+"\n"+stderr.String(), http.StatusInternalServerError)
		return
	}

	w.WriteHeader(http.StatusOK)
	w.Header().Set("Content-Type", "application/json")
	w.Write(stdout.Bytes())
}