From dcd12576c6ff122e83a744358b94365a7d8c74bd Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Thu, 15 May 2014 01:08:00 -0700 Subject: [PATCH] Add option to auto fix jpg orientation --- go/images/orientation.go | 16 ++-------------- go/weed/server.go | 2 ++ go/weed/volume.go | 2 ++ go/weed/weed_server/volume_server.go | 15 +++++++++------ go/weed/weed_server/volume_server_handlers.go | 4 ++++ 5 files changed, 19 insertions(+), 20 deletions(-) diff --git a/go/images/orientation.go b/go/images/orientation.go index 7c70f9ed7..a7c126fc1 100644 --- a/go/images/orientation.go +++ b/go/images/orientation.go @@ -5,17 +5,12 @@ import ( "github.com/rwcarlsen/goexif/exif" "image" "image/draw" - "image/gif" "image/jpeg" - "image/png" "log" ) //many code is copied from http://camlistore.org/pkg/images/images.go -func FixJpgOrientation(ext string, data []byte) (oriented []byte) { - if ext != ".jpg" { - return data - } +func FixJpgOrientation(data []byte) (oriented []byte) { ex, err := exif.Decode(bytes.NewReader(data)) if err != nil { return data @@ -53,14 +48,7 @@ func FixJpgOrientation(ext string, data []byte) (oriented []byte) { if srcImage, _, err := image.Decode(bytes.NewReader(data)); err == nil { dstImage := flip(rotate(srcImage, angle), flipMode) var buf bytes.Buffer - switch ext { - case ".png": - png.Encode(&buf, dstImage) - case ".jpg": - jpeg.Encode(&buf, dstImage, nil) - case ".gif": - gif.Encode(&buf, dstImage, nil) - } + jpeg.Encode(&buf, dstImage, nil) return buf.Bytes() } diff --git a/go/weed/server.go b/go/weed/server.go index 8d8504285..eea9f9037 100644 --- a/go/weed/server.go +++ b/go/weed/server.go @@ -63,6 +63,7 @@ var ( volumeDataFolders = cmdServer.Flag.String("dir", os.TempDir(), "directories to store data files. dir[,dir]...") volumeMaxDataVolumeCounts = cmdServer.Flag.String("volume.max", "7", "maximum numbers of volumes, count[,count]...") volumePulse = cmdServer.Flag.Int("pulseSeconds", 5, "number of seconds between heartbeats") + volumeFixJpgOrientation = cmdServer.Flag.Bool("volume.fix.jpg.orientation", false, "Adjust jpg orientation when uploading.") isStartingFiler = cmdServer.Flag.Bool("filer", false, "whether to start filer") serverWhiteList []string @@ -206,6 +207,7 @@ func runServer(cmd *Command, args []string) bool { r := http.NewServeMux() volumeServer := weed_server.NewVolumeServer(r, *serverIp, *volumePort, *serverPublicIp, folders, maxCounts, *serverIp+":"+strconv.Itoa(*masterPort), *volumePulse, *serverDataCenter, *serverRack, serverWhiteList, + *volumeFixJpgOrientation, ) glog.V(0).Infoln("Start Weed volume server", util.VERSION, "at", *serverIp+":"+strconv.Itoa(*volumePort)) diff --git a/go/weed/volume.go b/go/weed/volume.go index c4177a98a..142002ec1 100644 --- a/go/weed/volume.go +++ b/go/weed/volume.go @@ -37,6 +37,7 @@ var ( dataCenter = cmdVolume.Flag.String("dataCenter", "", "current volume server's data center name") rack = cmdVolume.Flag.String("rack", "", "current volume server's rack name") volumeWhiteListOption = cmdVolume.Flag.String("whiteList", "", "comma separated Ip addresses having write permission. No limit if empty.") + fixJpgOrientation = cmdVolume.Flag.Bool("fix.jpg.orientation", false, "Adjust jpg orientation when uploading.") volumeWhiteList []string ) @@ -80,6 +81,7 @@ func runVolume(cmd *Command, args []string) bool { volumeServer := weed_server.NewVolumeServer(r, *ip, *vport, *publicIp, folders, maxCounts, *masterNode, *vpulse, *dataCenter, *rack, volumeWhiteList, + *fixJpgOrientation, ) listeningAddress := *ip + ":" + strconv.Itoa(*vport) diff --git a/go/weed/weed_server/volume_server.go b/go/weed/weed_server/volume_server.go index 4f64eb2c5..b118e8c35 100644 --- a/go/weed/weed_server/volume_server.go +++ b/go/weed/weed_server/volume_server.go @@ -16,19 +16,22 @@ type VolumeServer struct { rack string whiteList []string store *storage.Store + + FixJpgOrientation bool } func NewVolumeServer(r *http.ServeMux, ip string, port int, publicIp string, folders []string, maxCounts []int, masterNode string, pulseSeconds int, dataCenter string, rack string, - whiteList []string) *VolumeServer { + whiteList []string, fixJpgOrientation bool) *VolumeServer { publicUrl := publicIp + ":" + strconv.Itoa(port) vs := &VolumeServer{ - masterNode: masterNode, - pulseSeconds: pulseSeconds, - dataCenter: dataCenter, - rack: rack, - whiteList: whiteList, + masterNode: masterNode, + pulseSeconds: pulseSeconds, + dataCenter: dataCenter, + rack: rack, + whiteList: whiteList, + FixJpgOrientation: fixJpgOrientation, } vs.store = storage.NewStore(port, ip, publicUrl, folders, maxCounts) diff --git a/go/weed/weed_server/volume_server_handlers.go b/go/weed/weed_server/volume_server_handlers.go index 5b99233a2..65a3d14f8 100644 --- a/go/weed/weed_server/volume_server_handlers.go +++ b/go/weed/weed_server/volume_server_handlers.go @@ -156,6 +156,10 @@ func (vs *VolumeServer) PostHandler(w http.ResponseWriter, r *http.Request) { return } + if vs.FixJpgOrientation && strings.HasSuffix(string(needle.Name), ".jpg") { + needle.Data = images.FixJpgOrientation(needle.Data) + } + ret := operation.UploadResult{} size, errorStatus := topology.ReplicatedWrite(vs.masterNode, vs.store, volumeId, needle, r) if errorStatus == "" {