コード例 #1
0
ファイル: main.go プロジェクト: nishkarr/sqrizr
func indexHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method == "GET" {
		io.WriteString(w, indexHtml)
	} else {
		file, fileHeader, err := r.FormFile("image")
		if err != nil {
			io.WriteString(w, err.Error())
			return
		}
		defer file.Close()

		// this is messy, we need to know if the processing completed successfully so we
		// can set the request headers. Request headers must be set before writing to ResponseWriter!

		headers := w.Header()
		headers.Set("Content-Type", "image/png")
		headers.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="sqr_%s"`, convertFileName(fileHeader.Filename)))

		_, _, err = sqrizrlib.Sqrize(file, w) // we rely on the sqrizrlib not writing anything to w on failure!
		if err != nil {
			headers.Set("Content-Type", "text/plain")
			headers.Del("Content-Disposition")
			io.WriteString(w, "Error decoding: "+fileHeader.Filename+": "+err.Error())
		} else {
			//headers := w.Header()
			//headers.Set("Content-Type", "image/jpg")
			//headers.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="sqr_%s"`, fileHeader.Filename))
		}
	}
}
コード例 #2
0
ファイル: main.go プロジェクト: nishkarr/sqrizr
func main() {
	flag.StringVar(&srcImagePath, "src", "", "Source image path")
	flag.Parse()

	if srcImagePath == "" {
		fmt.Println("Missing parameters")
		flag.PrintDefaults()
		return
	}

	var (
		err                      error
		srcImageFile, outputFile *os.File
	)

	srcImageFile, err = os.Open(srcImagePath)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer srcImageFile.Close()

	outputFile, err = os.Create("output.png")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer outputFile.Close()

	format, orientation, err := sqrizrlib.Sqrize(srcImageFile, outputFile)
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(fmt.Sprintf("processed file: %s\nformat: %s\norientation: %s", srcImagePath, format, orientation))
}