2011-07-03 20:33:41 +00:00
|
|
|
/*
|
|
|
|
Copyright 2011 Google Inc.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2011-10-18 18:12:01 +00:00
|
|
|
package server
|
2011-07-03 20:33:41 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"errors"
|
2011-07-03 20:33:41 +00:00
|
|
|
"fmt"
|
|
|
|
"image"
|
|
|
|
"image/jpeg"
|
|
|
|
"image/png"
|
|
|
|
"io"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2011-10-25 20:30:02 +00:00
|
|
|
"strings"
|
2011-07-03 20:33:41 +00:00
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"camlistore.org/pkg/blobref"
|
|
|
|
"camlistore.org/pkg/blobserver"
|
|
|
|
"camlistore.org/pkg/magic"
|
|
|
|
"camlistore.org/pkg/misc/resize"
|
|
|
|
"camlistore.org/pkg/schema"
|
2011-07-03 20:33:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ImageHandler struct {
|
|
|
|
Fetcher blobref.StreamingFetcher
|
|
|
|
Cache blobserver.Storage // optional
|
|
|
|
MaxWidth, MaxHeight int
|
2011-07-09 01:14:18 +00:00
|
|
|
Square bool
|
2011-10-25 20:30:02 +00:00
|
|
|
sc ScaledImage // optional cache for scaled images
|
2011-07-03 20:33:41 +00:00
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func (ih *ImageHandler) storageSeekFetcher() (blobref.SeekFetcher, error) {
|
2011-07-03 20:33:41 +00:00
|
|
|
return blobref.SeekerFromStreamingFetcher(ih.Fetcher) // TODO: pass ih.Cache?
|
|
|
|
}
|
|
|
|
|
2011-07-09 01:14:18 +00:00
|
|
|
type subImager interface {
|
|
|
|
SubImage(image.Rectangle) image.Image
|
|
|
|
}
|
|
|
|
|
|
|
|
func squareImage(i image.Image) image.Image {
|
|
|
|
si, ok := i.(subImager)
|
|
|
|
if !ok {
|
|
|
|
log.Fatalf("image %T isn't a subImager", i)
|
|
|
|
}
|
|
|
|
b := i.Bounds()
|
|
|
|
if b.Dx() > b.Dy() {
|
|
|
|
thin := (b.Dx() - b.Dy()) / 2
|
|
|
|
newB := b
|
|
|
|
newB.Min.X += thin
|
|
|
|
newB.Max.X -= thin
|
|
|
|
return si.SubImage(newB)
|
|
|
|
}
|
|
|
|
thin := (b.Dy() - b.Dx()) / 2
|
|
|
|
newB := b
|
|
|
|
newB.Min.Y += thin
|
|
|
|
newB.Max.Y -= thin
|
|
|
|
return si.SubImage(newB)
|
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func (ih *ImageHandler) cache(tr io.Reader, name string) (*blobref.BlobRef, error) {
|
2011-10-25 20:30:02 +00:00
|
|
|
br, err := schema.WriteFileFromReaderRolling(ih.Cache, name, tr)
|
|
|
|
if err != nil {
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
return br, errors.New("failed to cache " + name + ": " + err.Error())
|
2011-07-03 20:33:41 +00:00
|
|
|
}
|
2011-10-25 20:30:02 +00:00
|
|
|
log.Printf("Imache Cache: saved as %v\n", br)
|
|
|
|
return br, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CacheScaled saves in the image handler's cache the scaled image read
|
|
|
|
// from tr, and puts its blobref in the scaledImage under the key name.
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func (ih *ImageHandler) cacheScaled(tr io.Reader, name string) error {
|
2011-10-25 20:30:02 +00:00
|
|
|
br, err := ih.cache(tr, name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ih.sc.Put(name, br)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func (ih *ImageHandler) cached(br *blobref.BlobRef) (fr *schema.FileReader, err error) {
|
2011-10-25 20:30:02 +00:00
|
|
|
fetchSeeker, err := blobref.SeekerFromStreamingFetcher(ih.Cache)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fr, err = schema.NewFileReader(fetchSeeker, br)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
log.Printf("Image Cache: hit: %v\n", br)
|
|
|
|
return fr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Key format: "scaled:" + bref + ":" + width "x" + height
|
|
|
|
// where bref is the blobref of the unscaled image.
|
|
|
|
func cacheKey(bref string, width int, height int) string {
|
|
|
|
return fmt.Sprintf("scaled:%v:%dx%d", bref, width, height)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScaledCached reads the scaled version of the image in file,
|
|
|
|
// if it is in cache. On success, the image format is returned.
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func (ih *ImageHandler) scaledCached(buf *bytes.Buffer, file *blobref.BlobRef) (format string, err error) {
|
2011-10-25 20:30:02 +00:00
|
|
|
name := cacheKey(file.String(), ih.MaxWidth, ih.MaxHeight)
|
|
|
|
br, err := ih.sc.Get(name)
|
|
|
|
if err != nil {
|
|
|
|
return format, fmt.Errorf("%v: %v", name, err)
|
|
|
|
}
|
|
|
|
fr, err := ih.cached(br)
|
|
|
|
if err != nil {
|
|
|
|
return format, fmt.Errorf("No cache hit for %v: %v", br, err)
|
|
|
|
}
|
|
|
|
_, err = io.Copy(buf, fr)
|
|
|
|
if err != nil {
|
|
|
|
return format, fmt.Errorf("error reading cached thumbnail %v: %v", name, err)
|
|
|
|
}
|
|
|
|
mime := magic.MimeType(buf.Bytes())
|
|
|
|
if mime == "" {
|
|
|
|
return format, fmt.Errorf("error with cached thumbnail %v: unknown mime type", name)
|
|
|
|
}
|
|
|
|
pieces := strings.Split(mime, "/")
|
|
|
|
if len(pieces) < 2 {
|
|
|
|
return format, fmt.Errorf("error with cached thumbnail %v: bogus mime type", name)
|
|
|
|
}
|
|
|
|
if pieces[0] != "image" {
|
|
|
|
return format, fmt.Errorf("error with cached thumbnail %v: not an image", name)
|
2011-07-03 20:33:41 +00:00
|
|
|
}
|
2011-10-25 20:30:02 +00:00
|
|
|
return pieces[1], nil
|
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func (ih *ImageHandler) scaleImage(buf *bytes.Buffer, file *blobref.BlobRef) (format string, err error) {
|
2011-10-25 20:30:02 +00:00
|
|
|
mw, mh := ih.MaxWidth, ih.MaxHeight
|
2011-07-03 20:33:41 +00:00
|
|
|
|
|
|
|
fetchSeeker, err := ih.storageSeekFetcher()
|
|
|
|
if err != nil {
|
2011-10-25 20:30:02 +00:00
|
|
|
return format, err
|
2011-07-03 20:33:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fr, err := schema.NewFileReader(fetchSeeker, file)
|
|
|
|
if err != nil {
|
2011-10-25 20:30:02 +00:00
|
|
|
return format, err
|
2011-07-03 20:33:41 +00:00
|
|
|
}
|
|
|
|
|
2011-10-25 20:30:02 +00:00
|
|
|
_, err = io.Copy(buf, fr)
|
2011-07-08 20:33:55 +00:00
|
|
|
if err != nil {
|
2011-10-25 20:30:02 +00:00
|
|
|
return format, fmt.Errorf("image resize: error reading image %s: %v", file, err)
|
2011-07-08 20:33:55 +00:00
|
|
|
}
|
|
|
|
i, format, err := image.Decode(bytes.NewBuffer(buf.Bytes()))
|
2011-07-03 20:33:41 +00:00
|
|
|
if err != nil {
|
2011-10-25 20:30:02 +00:00
|
|
|
return format, err
|
2011-07-03 20:33:41 +00:00
|
|
|
}
|
|
|
|
b := i.Bounds()
|
2011-07-09 01:14:18 +00:00
|
|
|
|
|
|
|
useBytesUnchanged := true
|
|
|
|
|
|
|
|
isSquare := b.Dx() == b.Dy()
|
|
|
|
if ih.Square && !isSquare {
|
|
|
|
useBytesUnchanged = false
|
|
|
|
i = squareImage(i)
|
|
|
|
b = i.Bounds()
|
|
|
|
}
|
|
|
|
|
2011-07-03 20:33:41 +00:00
|
|
|
// only do downscaling, otherwise just serve the original image
|
|
|
|
if mw < b.Dx() || mh < b.Dy() {
|
2011-07-09 01:14:18 +00:00
|
|
|
useBytesUnchanged = false
|
|
|
|
|
2011-07-03 20:33:41 +00:00
|
|
|
const huge = 2400
|
|
|
|
// If it's gigantic, it's more efficient to downsample first
|
|
|
|
// and then resize; resizing will smooth out the roughness.
|
|
|
|
// (trusting the moustachio guys on that one).
|
|
|
|
if b.Dx() > huge || b.Dy() > huge {
|
|
|
|
w, h := mw*2, mh*2
|
|
|
|
if b.Dx() > b.Dy() {
|
|
|
|
w = b.Dx() * h / b.Dy()
|
|
|
|
} else {
|
|
|
|
h = b.Dy() * w / b.Dx()
|
|
|
|
}
|
|
|
|
i = resize.Resample(i, i.Bounds(), w, h)
|
|
|
|
b = i.Bounds()
|
|
|
|
}
|
|
|
|
// conserve proportions. use the smallest of the two as the decisive one.
|
|
|
|
if mw > mh {
|
|
|
|
mw = b.Dx() * mh / b.Dy()
|
|
|
|
} else {
|
|
|
|
mh = b.Dy() * mw / b.Dx()
|
|
|
|
}
|
2011-07-09 01:14:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !useBytesUnchanged {
|
2011-07-03 20:33:41 +00:00
|
|
|
i = resize.Resize(i, b, mw, mh)
|
|
|
|
// Encode as a new image
|
|
|
|
buf.Reset()
|
|
|
|
switch format {
|
|
|
|
case "jpeg":
|
2011-10-25 20:30:02 +00:00
|
|
|
err = jpeg.Encode(buf, i, nil)
|
2011-07-03 20:33:41 +00:00
|
|
|
default:
|
2011-10-25 20:30:02 +00:00
|
|
|
err = png.Encode(buf, i)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return format, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return format, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ih *ImageHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request, file *blobref.BlobRef) {
|
|
|
|
if req.Method != "GET" && req.Method != "HEAD" {
|
|
|
|
http.Error(rw, "Invalid method", 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mw, mh := ih.MaxWidth, ih.MaxHeight
|
|
|
|
if mw == 0 || mh == 0 || mw > 2000 || mh > 2000 {
|
|
|
|
http.Error(rw, "bogus dimensions", 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
var err error
|
2011-10-25 20:30:02 +00:00
|
|
|
format := ""
|
|
|
|
cacheHit := false
|
|
|
|
if ih.sc != nil {
|
|
|
|
format, err = ih.scaledCached(&buf, file)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("image resize: %v", err)
|
|
|
|
} else {
|
|
|
|
cacheHit = true
|
2011-07-03 20:33:41 +00:00
|
|
|
}
|
2011-10-25 20:30:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !cacheHit {
|
|
|
|
format, err = ih.scaleImage(&buf, file)
|
2011-07-03 20:33:41 +00:00
|
|
|
if err != nil {
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
http.Error(rw, err.Error(), 500)
|
2011-07-03 20:33:41 +00:00
|
|
|
return
|
|
|
|
}
|
2011-10-25 20:30:02 +00:00
|
|
|
if ih.sc != nil {
|
|
|
|
name := cacheKey(file.String(), mw, mh)
|
|
|
|
bufcopy := buf.Bytes()
|
|
|
|
err = ih.cacheScaled(bytes.NewBuffer(bufcopy), name)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("image resize: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2011-07-03 20:33:41 +00:00
|
|
|
}
|
2011-07-09 01:14:18 +00:00
|
|
|
|
|
|
|
rw.Header().Set("Content-Type", imageContentTypeOfFormat(format))
|
2011-07-03 20:33:41 +00:00
|
|
|
size := buf.Len()
|
|
|
|
rw.Header().Set("Content-Length", fmt.Sprintf("%d", size))
|
2011-10-25 20:30:02 +00:00
|
|
|
n, err := io.Copy(rw, &buf)
|
2011-07-03 20:33:41 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error serving thumbnail of file schema %s: %v", file, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if n != int64(size) {
|
|
|
|
log.Printf("error serving thumbnail of file schema %s: sent %d, expected size of %d",
|
|
|
|
file, n, size)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2011-07-09 01:14:18 +00:00
|
|
|
|
|
|
|
func imageContentTypeOfFormat(format string) string {
|
|
|
|
if format == "jpeg" {
|
|
|
|
return "image/jpeg"
|
|
|
|
}
|
|
|
|
return "image/png"
|
2011-07-11 02:33:34 +00:00
|
|
|
}
|