Add option to auto fix jpg orientation

This commit is contained in:
Chris Lu 2014-05-15 01:08:00 -07:00
parent 025589adf8
commit dcd12576c6
5 changed files with 19 additions and 20 deletions

View file

@ -5,17 +5,12 @@ import (
"github.com/rwcarlsen/goexif/exif" "github.com/rwcarlsen/goexif/exif"
"image" "image"
"image/draw" "image/draw"
"image/gif"
"image/jpeg" "image/jpeg"
"image/png"
"log" "log"
) )
//many code is copied from http://camlistore.org/pkg/images/images.go //many code is copied from http://camlistore.org/pkg/images/images.go
func FixJpgOrientation(ext string, data []byte) (oriented []byte) { func FixJpgOrientation(data []byte) (oriented []byte) {
if ext != ".jpg" {
return data
}
ex, err := exif.Decode(bytes.NewReader(data)) ex, err := exif.Decode(bytes.NewReader(data))
if err != nil { if err != nil {
return data return data
@ -53,14 +48,7 @@ func FixJpgOrientation(ext string, data []byte) (oriented []byte) {
if srcImage, _, err := image.Decode(bytes.NewReader(data)); err == nil { if srcImage, _, err := image.Decode(bytes.NewReader(data)); err == nil {
dstImage := flip(rotate(srcImage, angle), flipMode) dstImage := flip(rotate(srcImage, angle), flipMode)
var buf bytes.Buffer var buf bytes.Buffer
switch ext { jpeg.Encode(&buf, dstImage, nil)
case ".png":
png.Encode(&buf, dstImage)
case ".jpg":
jpeg.Encode(&buf, dstImage, nil)
case ".gif":
gif.Encode(&buf, dstImage, nil)
}
return buf.Bytes() return buf.Bytes()
} }

View file

@ -63,6 +63,7 @@ var (
volumeDataFolders = cmdServer.Flag.String("dir", os.TempDir(), "directories to store data files. dir[,dir]...") 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]...") volumeMaxDataVolumeCounts = cmdServer.Flag.String("volume.max", "7", "maximum numbers of volumes, count[,count]...")
volumePulse = cmdServer.Flag.Int("pulseSeconds", 5, "number of seconds between heartbeats") 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") isStartingFiler = cmdServer.Flag.Bool("filer", false, "whether to start filer")
serverWhiteList []string serverWhiteList []string
@ -206,6 +207,7 @@ func runServer(cmd *Command, args []string) bool {
r := http.NewServeMux() r := http.NewServeMux()
volumeServer := weed_server.NewVolumeServer(r, *serverIp, *volumePort, *serverPublicIp, folders, maxCounts, volumeServer := weed_server.NewVolumeServer(r, *serverIp, *volumePort, *serverPublicIp, folders, maxCounts,
*serverIp+":"+strconv.Itoa(*masterPort), *volumePulse, *serverDataCenter, *serverRack, serverWhiteList, *serverIp+":"+strconv.Itoa(*masterPort), *volumePulse, *serverDataCenter, *serverRack, serverWhiteList,
*volumeFixJpgOrientation,
) )
glog.V(0).Infoln("Start Weed volume server", util.VERSION, "at", *serverIp+":"+strconv.Itoa(*volumePort)) glog.V(0).Infoln("Start Weed volume server", util.VERSION, "at", *serverIp+":"+strconv.Itoa(*volumePort))

View file

@ -37,6 +37,7 @@ var (
dataCenter = cmdVolume.Flag.String("dataCenter", "", "current volume server's data center name") dataCenter = cmdVolume.Flag.String("dataCenter", "", "current volume server's data center name")
rack = cmdVolume.Flag.String("rack", "", "current volume server's rack 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.") 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 volumeWhiteList []string
) )
@ -80,6 +81,7 @@ func runVolume(cmd *Command, args []string) bool {
volumeServer := weed_server.NewVolumeServer(r, *ip, *vport, *publicIp, folders, maxCounts, volumeServer := weed_server.NewVolumeServer(r, *ip, *vport, *publicIp, folders, maxCounts,
*masterNode, *vpulse, *dataCenter, *rack, volumeWhiteList, *masterNode, *vpulse, *dataCenter, *rack, volumeWhiteList,
*fixJpgOrientation,
) )
listeningAddress := *ip + ":" + strconv.Itoa(*vport) listeningAddress := *ip + ":" + strconv.Itoa(*vport)

View file

@ -16,19 +16,22 @@ type VolumeServer struct {
rack string rack string
whiteList []string whiteList []string
store *storage.Store store *storage.Store
FixJpgOrientation bool
} }
func NewVolumeServer(r *http.ServeMux, ip string, port int, publicIp string, folders []string, maxCounts []int, func NewVolumeServer(r *http.ServeMux, ip string, port int, publicIp string, folders []string, maxCounts []int,
masterNode string, pulseSeconds int, masterNode string, pulseSeconds int,
dataCenter string, rack string, dataCenter string, rack string,
whiteList []string) *VolumeServer { whiteList []string, fixJpgOrientation bool) *VolumeServer {
publicUrl := publicIp + ":" + strconv.Itoa(port) publicUrl := publicIp + ":" + strconv.Itoa(port)
vs := &VolumeServer{ vs := &VolumeServer{
masterNode: masterNode, masterNode: masterNode,
pulseSeconds: pulseSeconds, pulseSeconds: pulseSeconds,
dataCenter: dataCenter, dataCenter: dataCenter,
rack: rack, rack: rack,
whiteList: whiteList, whiteList: whiteList,
FixJpgOrientation: fixJpgOrientation,
} }
vs.store = storage.NewStore(port, ip, publicUrl, folders, maxCounts) vs.store = storage.NewStore(port, ip, publicUrl, folders, maxCounts)

View file

@ -156,6 +156,10 @@ func (vs *VolumeServer) PostHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
if vs.FixJpgOrientation && strings.HasSuffix(string(needle.Name), ".jpg") {
needle.Data = images.FixJpgOrientation(needle.Data)
}
ret := operation.UploadResult{} ret := operation.UploadResult{}
size, errorStatus := topology.ReplicatedWrite(vs.masterNode, vs.store, volumeId, needle, r) size, errorStatus := topology.ReplicatedWrite(vs.masterNode, vs.store, volumeId, needle, r)
if errorStatus == "" { if errorStatus == "" {