Example #1
0
func orient(r io.Reader, i image.Image) (image.Image, error) {
	t := log.Start()
	defer log.End(t)

	e, err := exif.Decode(r)
	if err != nil {
		log.Printf("fail to decode exif: %v", err)
		return nil, err
	}
	tag, err := e.Get(exif.Orientation)
	// Orientationタグが存在しない場合、処理を完了する
	if err != nil {
		log.Println("oritentation tag doesn't exist")
		return nil, err
	}
	o, err := tag.Int(0)
	if err != nil {
		log.Println("oritentation tag is't int")
		return nil, err
	}

	rect := i.Bounds()
	// orientation=5~8 なら画像サイズの縦横を入れ替える
	if o >= 5 && o <= 8 {
		rect = RotateRect(rect)
	}
	d := image.NewRGBA64(rect)
	a := affines[o]
	a.TransformCenter(d, i, interp.Bilinear)

	return d, nil
}
Example #2
0
func Start() error {
	t := log.Start()
	defer log.End(t)

	o, err := option.New(os.Args[1:])
	if err != nil {
		return err
	}
	handler, err := NewHandler(o)
	if err != nil {
		return err
	}
	s := http.Server{
		Handler:        &handler,
		ReadTimeout:    10 * time.Second,
		MaxHeaderBytes: 1 << 20,
	}
	l, err := net.Listen("tcp", addr)
	if err != nil {
		return err
	}
	if err := s.Serve(netutil.LimitListener(l, o.MaxConn)); err != nil {
		log.Fatalf("fail: err=%v", err)
	}
	log.Println("listening: addr=%s", addr)
	return nil
}
Example #3
0
// Preprocess はリサイズ処理の前処理を行います。
// 画像をデコードし、jpegのEXIFの回転情報をピクセルに反映して返します。
func (self *Processor) Preprocess(path string) (image.Image, error) {
	t := log.Start()
	defer log.End(t)

	// ファイルをデコードする
	f, err := os.Open(path)
	if err != nil {
		return nil, err
	}
	defer f.Close()
	i, format, err := image.Decode(bufio.NewReader(f))
	if err != nil {
		log.Println("fail to decode")
		return nil, err
	}

	// jpeg以外ならピクセルをそのまま返す
	if format != storage.FormatJpeg {
		return i, nil
	}

	// jpegならEXIFの回転情報をピクセルに反映して返す
	f, err = os.Open(path)
	if err != nil {
		return nil, err
	}
	defer f.Close()
	io, err := orient(bufio.NewReader(f), i)
	// 回転に失敗した場合、元のピクセルを返す
	if err != nil {
		log.Println("cancel to apply orientation")
		return i, nil
	}
	log.Debugf("%s -> %s", reflect.TypeOf(i), reflect.TypeOf(io))
	return io, nil
}
Example #4
0
func Init() error {
	tempDir = path.Join(os.TempDir(), "")
	log.Println(tempDir)

	var err error
	expires, err = time.ParseDuration("1h")
	if err != nil {
		return err
	}
	if err := os.RemoveAll(tempDir); err != nil {
		return err
	}
	if err := os.MkdirAll(tempDir, 0777); err != nil {
		return err
	}

	client = new(http.Client)

	return nil
}
Example #5
0
func TestStartAndEnd(t *testing.T) {
	os.Setenv(log.EnvLogFilename, file)
	filename, err := log.Init()
	if err != nil {
		t.Fatalf("fail to init: error=%v", err)
	}
	if err := os.Unsetenv(log.EnvLogFilename); err != nil {
		t.Fatalf("fail to unset env: error=%v", err)
	}

	defer func() {
		if err := recover(); err != nil {
			fmt.Println(err)
		}
	}()

	timer := log.Start()

	log.Debug("debug")
	log.Print("print")
	log.Info("info")
	log.Warn("warn")
	log.Warning("warning")
	log.Error("error")

	log.Debugf("debug%s", "f")
	log.Printf("print%s", "f")
	log.Infof("info%s", "f")
	log.Warnf("warn%s", "f")
	log.Warningf("warning%s", "f")
	log.Errorf("error%s", "f")

	log.Debugln("debugln")
	log.Println("println")
	log.Infoln("infoln")
	log.Warnln("warnln")
	log.Warningln("warningln")
	log.Errorln("errorln")

	log.End(timer)

	expected := []map[string]string{
		map[string]string{"level": "info", "msg": "start"},

		map[string]string{"level": "info", "msg": "print"},
		map[string]string{"level": "info", "msg": "info"},
		map[string]string{"level": "warning", "msg": "warn"},
		map[string]string{"level": "warning", "msg": "warning"},
		map[string]string{"level": "error", "msg": "error"},

		map[string]string{"level": "info", "msg": "printf"},
		map[string]string{"level": "info", "msg": "infof"},
		map[string]string{"level": "warning", "msg": "warnf"},
		map[string]string{"level": "warning", "msg": "warningf"},
		map[string]string{"level": "error", "msg": "errorf"},

		map[string]string{"level": "info", "msg": "println"},
		map[string]string{"level": "info", "msg": "infoln"},
		map[string]string{"level": "warning", "msg": "warnln"},
		map[string]string{"level": "warning", "msg": "warningln"},
		map[string]string{"level": "error", "msg": "errorln"},

		map[string]string{"level": "info", "msg": "end"},
	}

	bufs, err := ioutil.ReadFile(filename)
	if err != nil {
		t.Fatalf("fail to read file: error=%v", err)
	}
	lines := strings.Split(string(bufs), "\n")

	l := len(lines)
	for i, line := range lines {
		// fmt.Println(i, line)

		if i == l-1 {
			break
		}
		var obj map[string]interface{}
		if err := json.Unmarshal([]byte(line), &obj); err != nil {
			t.Fatalf("fail to decode json")
		}

		e := expected[i]
		// fmt.Println(i, e)
		if obj["level"] != e["level"] {
			t.Fatalf("wrong level: expected %s, but actual %s", e["level"], obj["level"])
		}
		if obj["msg"] != e["msg"] {
			t.Fatalf("wrong msg: expected %s, but actual %s", e["msg"], obj["msg"])
		}
	}

	if err := log.Close(); err != nil {
		t.Fatalf("fail to close log: error=%v", err)
	}
}