Browse Source

1. refactor, move image resizing to its own package

2. make code compile
pull/2/head
Chris Lu 11 years ago
parent
commit
f7d6909b6f
  1. 14
      go/filer/client_operations.go
  2. 39
      go/images/images.go
  3. 12
      go/weed/mount_std.go
  4. 1
      go/weed/weed_server/filer_server.go
  5. 20
      go/weed/weed_server/filer_server_handlers_api.go
  6. 29
      go/weed/weed_server/volume_server_handlers.go

14
go/filer/client_operations.go

@ -11,9 +11,9 @@ import (
)
type ApiRequest struct {
Command string //"listFiles", "listDirectories"
DirectoryId DirectoryId
FileName string
Command string //"listFiles", "listDirectories"
Directory string
FileName string
}
type ListFilesResult struct {
@ -21,9 +21,9 @@ type ListFilesResult struct {
Error string `json:"error,omitempty"`
}
func ListFiles(server string, directoryId DirectoryId, fileName string) (*ListFilesResult, error) {
func ListFiles(server string, directory string, fileName string) (*ListFilesResult, error) {
var ret ListFilesResult
if err := call(server, ApiRequest{Command: "listFiles", DirectoryId: directoryId, FileName: fileName}, &ret); err == nil {
if err := call(server, ApiRequest{Command: "listFiles", Directory: directory, FileName: fileName}, &ret); err == nil {
if ret.Error != "" {
return nil, errors.New(ret.Error)
}
@ -38,9 +38,9 @@ type ListDirectoriesResult struct {
Error string `json:"error,omitempty"`
}
func ListDirectories(server string, directoryId DirectoryId) (*ListDirectoriesResult, error) {
func ListDirectories(server string, directory string) (*ListDirectoriesResult, error) {
var ret ListDirectoriesResult
if err := call(server, ApiRequest{Command: "listDirectories", DirectoryId: directoryId}, &ret); err == nil {
if err := call(server, ApiRequest{Command: "listDirectories", Directory: directory}, &ret); err == nil {
if ret.Error != "" {
return nil, errors.New(ret.Error)
}

39
go/images/images.go

@ -0,0 +1,39 @@
package images
import (
"bytes"
"github.com/disintegration/imaging"
"image"
"image/gif"
"image/jpeg"
"image/png"
)
func FixJpgOrientation() {
}
func Resized(ext string, data []byte, width, height int) (resized []byte) {
if width == 0 && height == 0 {
return data
}
if srcImage, _, err := image.Decode(bytes.NewReader(data)); err == nil {
bounds := srcImage.Bounds()
var dstImage *image.NRGBA
if width == height && bounds.Dx() != bounds.Dy() {
dstImage = imaging.Thumbnail(srcImage, width, height, imaging.Lanczos)
} else {
dstImage = imaging.Resize(srcImage, width, height, imaging.Lanczos)
}
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()
}
return data
}

12
go/weed/mount_std.go

@ -59,16 +59,16 @@ func (File) ReadAll(intr fs.Intr) ([]byte, fuse.Error) {
}
type Dir struct {
DirectoryId filer.DirectoryId
Name string
Path string
Id uint64
}
func (dir Dir) Attr() fuse.Attr {
return fuse.Attr{Inode: 1, Mode: os.ModeDir | 0555}
return fuse.Attr{Inode: dir.Id, Mode: os.ModeDir | 0555}
}
func (dir Dir) Lookup(name string, intr fs.Intr) (fs.Node, fuse.Error) {
files_result, e := filer.ListFiles(*mountOptions.filer, dir.DirectoryId, name)
files_result, e := filer.ListFiles(*mountOptions.filer, dir.Path, name)
if e != nil {
return nil, fuse.ENOENT
}
@ -86,13 +86,13 @@ func (WFS) Root() (fs.Node, fuse.Error) {
func (dir *Dir) ReadDir(intr fs.Intr) ([]fuse.Dirent, fuse.Error) {
ret := make([]fuse.Dirent, 0)
if dirs, e := filer.ListDirectories(*mountOptions.filer, dir.DirectoryId); e == nil {
if dirs, e := filer.ListDirectories(*mountOptions.filer, dir.Path); e == nil {
for _, d := range dirs.Directories {
dirId := uint64(d.Id)
ret = append(ret, fuse.Dirent{Inode: dirId, Name: d.Name, Type: fuse.DT_Dir})
}
}
if files, e := filer.ListFiles(*mountOptions.filer, dir.DirectoryId, ""); e == nil {
if files, e := filer.ListFiles(*mountOptions.filer, dir.Path, ""); e == nil {
for _, f := range files.Files {
if fileId, e := storage.ParseFileId(string(f.Id)); e == nil {
fileInode := uint64(fileId.VolumeId)<<48 + fileId.Key

1
go/weed/weed_server/filer_server.go

@ -26,7 +26,6 @@ func NewFilerServer(r *http.ServeMux, port int, master string, dir string, colle
return
}
r.HandleFunc("/__api__", fs.filerApiHandler)
r.HandleFunc("/", fs.filerHandler)
return fs, nil

20
go/weed/weed_server/filer_server_handlers_api.go

@ -1,20 +0,0 @@
package weed_server
import (
"net/http"
)
func (fs *FilerServer) filerApiHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
fs.GetOrHeadHandler(w, r, true)
case "HEAD":
fs.GetOrHeadHandler(w, r, false)
case "DELETE":
fs.DeleteHandler(w, r)
case "PUT":
fs.PostHandler(w, r)
case "POST":
fs.PostHandler(w, r)
}
}

29
go/weed/weed_server/volume_server_handlers.go

@ -1,17 +1,12 @@
package weed_server
import (
"bytes"
"code.google.com/p/weed-fs/go/glog"
"code.google.com/p/weed-fs/go/images"
"code.google.com/p/weed-fs/go/operation"
"code.google.com/p/weed-fs/go/stats"
"code.google.com/p/weed-fs/go/storage"
"code.google.com/p/weed-fs/go/topology"
"github.com/disintegration/imaging"
"image"
"image/gif"
"image/jpeg"
"image/png"
"mime"
"net/http"
"strconv"
@ -132,27 +127,7 @@ func (vs *VolumeServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request,
if r.FormValue("height") != "" {
height, _ = strconv.Atoi(r.FormValue("height"))
}
if width != 0 || height != 0 {
if srcImage, _, err := image.Decode(bytes.NewReader(n.Data)); err == nil {
bounds := srcImage.Bounds()
var dstImage *image.NRGBA
if width == height && bounds.Dx() != bounds.Dy() {
dstImage = imaging.Thumbnail(srcImage, width, height, imaging.Lanczos)
} else {
dstImage = imaging.Resize(srcImage, width, height, imaging.Lanczos)
}
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)
}
n.Data = buf.Bytes()
}
}
n.Data = images.Resized(ext, n.Data, width, height)
}
w.Header().Set("Content-Length", strconv.Itoa(len(n.Data)))
if isGetMethod {

Loading…
Cancel
Save