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"
"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)
}
return buf.Bytes()
}

View file

@ -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))

View file

@ -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)

View file

@ -16,12 +16,14 @@ 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,
@ -29,6 +31,7 @@ func NewVolumeServer(r *http.ServeMux, ip string, port int, publicIp string, fol
dataCenter: dataCenter,
rack: rack,
whiteList: whiteList,
FixJpgOrientation: fixJpgOrientation,
}
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
}
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 == "" {