Skip to content
Browse files

Use mogrify-go library

  • Loading branch information...
1 parent 449664e commit 254e3d8312a64dae6995a83bf2236ec0e9bcb1e5 @tobi committed May 3, 2012
Showing with 61 additions and 147 deletions.
  1. +1 −0 .gitignore
  2. +29 −8 main.go
  3. BIN mogrify/assets/image.jpg
  4. +0 −54 mogrify/mogrify.go
  5. +0 −61 mogrify/mogrify_test.go
  6. +20 −13 {phantom → }/phantom.go
  7. +0 −10 phantom/phantom_test.go
  8. +10 −0 phantom_test.go
  9. +1 −1 run
View
1 .gitignore
@@ -1 +1,2 @@
browser
+browser-go
View
37 main.go
@@ -1,8 +1,7 @@
package main
import (
- "browser/mogrify"
- "browser/phantom"
+ "github.com/tobi/mogrify-go"
"bytes"
"flag"
"time"
@@ -12,6 +11,8 @@ import (
"net/http"
)
+var phantom *Phantom = NewWebkitPool(1)
+
func param(r *http.Request, name string) string {
if len(r.Form[name]) > 0 {
return r.Form[name][0]
@@ -37,7 +38,7 @@ func fresh(c *cacheEntry) bool {
elapsed := time.Since(c.stat.ModTime()).Minutes()
log.Printf("Since last mod: %v", elapsed)
- return time.Since(c.stat.ModTime()).Minutes() < 0.5
+ return time.Since(c.stat.ModTime()).Minutes() < 10
}
func httpError(w http.ResponseWriter, msg string) {
@@ -101,15 +102,35 @@ func Server(w http.ResponseWriter, r *http.Request) {
return
}
- var output = new(bytes.Buffer)
+ image := mogrify.NewImage()
+ defer image.Destroy()
+
+ log.Printf("buffer: %d", len(buffer))
+
+ err := image.OpenBlob(buffer)
+
+ if err != nil {
+ httpError(w, err.Error())
+ return
+ }
+
+ resized, err := image.NewTransformation("", size)
+ defer resized.Destroy()
+
+ if err != nil {
+ httpError(w, err.Error())
+ return
+ }
+
+ blob, err := resized.Blob()
- if err := mogrify.Resize(output, bytes.NewBuffer(buffer), size); err != nil {
- httpError(w, "could not resize")
+ if err != nil {
+ httpError(w, err.Error())
return
}
- CacheStore(url + size, output.Bytes())
- servePng(w, output)
+ CacheStore(url + size, blob)
+ servePng(w, bytes.NewBuffer(blob))
return
}
View
BIN mogrify/assets/image.jpg
Deleted file not rendered
View
54 mogrify/mogrify.go
@@ -1,54 +0,0 @@
-package mogrify
-
-import (
- "io"
- "log"
- "os"
- "os/exec"
-)
-
-func init() {
- _, err := exec.LookPath("gm")
- if err != nil {
- log.Print("Could not locate gm (GraphicsMagic) tool in path")
- os.Exit(1)
- }
-}
-
-func Resize(out io.Writer, in io.Reader, size string) error {
- log.Printf("resize:%s", size)
-
- cmd := exec.Command("gm", "convert", "-resize", size, "-colorspace", "RGB", "-quality", "90%%", "+profile", "'!EXIF,!IPTC,*'", "-", "-")
- stdin, err := cmd.StdinPipe()
- if err != nil {
- return err
- }
-
- stdout, err := cmd.StdoutPipe()
- if err != nil {
- return err
- }
-
- err = cmd.Start()
- if err != nil {
- log.Print(err)
- return err
- }
-
- // copy stream ot gm tool, close stream once done and
- // read the output of the tool back to the out stream
- io.Copy(stdin, in)
- stdin.Close()
- io.Copy(out, stdout)
-
- return cmd.Wait()
-}
-
-func ResizeFile(filename string, size string) error {
- log.Printf("resize:%s", size)
- cmd := exec.Command("gm", "convert", "-resize", size, "-colorspace", "RGB", "-quality", "90%%", "+profile", "'!EXIF,!IPTC,*'", filename, filename)
- cmd.Stdout = os.Stdout
- cmd.Stderr = os.Stderr
- return cmd.Run()
-}
-
View
61 mogrify/mogrify_test.go
@@ -1,61 +0,0 @@
-package mogrify
-
-import (
- "bytes"
- "io"
- "log"
- "os"
- "testing"
-)
-
-func copyFile(src, dst string) (int64, error) {
- sf, err := os.Open(src)
- if err != nil {
- log.Fatal(err)
- return 0, err
- }
- defer sf.Close()
- df, err := os.Create(dst)
- if err != nil {
- log.Fatal(err)
- return 0, err
- }
- defer df.Close()
- return io.Copy(df, sf)
-}
-
-func TestResizeFile(t *testing.T) {
-
- targetPath := os.TempDir() + "/image1.jpg"
- _, err := copyFile("../assets/image.jpg", targetPath)
-
- if err != nil {
- log.Printf("Could not copy file to tmp folder: %s", err)
- t.Fail()
- }
-
- ResizeFile(targetPath, "50x50")
-}
-
-func TestResizeStream(t *testing.T) {
- image, err := os.Open("../assets/image.jpg")
- if err != nil {
- t.Fail()
- }
-
- output, err := os.Create( os.TempDir() + "/image2.jpg")
- if err != nil {
- t.Fail()
- }
-
- ResizeStream(output, image, "50x50")
-}
-
-func TestResizeFileDoesntExist(t *testing.T) {
- buf := bytes.NewBuffer(nil)
- in := bytes.NewBuffer(nil)
- err := Resize(buf, in, "50x50")
- if err == nil {
- t.Fail()
- }
-}
View
33 phantom/phantom.go → phantom.go
@@ -1,4 +1,4 @@
-package phantom
+package main
import (
"log"
@@ -18,8 +18,6 @@ type shot struct {
resultChan chan string
}
-var screenshotChan = make(chan *shot, 10)
-
func init() {
dir = os.TempDir()
rand = reseed()
@@ -32,14 +30,24 @@ func init() {
log.Fatalf("Cannot find phantomjs executable in bath")
}
- go webkitWorker()
- go webkitWorker()
- go webkitWorker()
- go webkitWorker()
}
-func webkitWorker() {
- for shot := range screenshotChan {
+type Phantom struct {
+ screenshotChan chan *shot
+}
+
+func NewWebkitPool(pool int) *Phantom {
+ phantom := &Phantom{ make(chan *shot, 10) }
+
+ for i := 0; i < pool; i++ {
+ go phantom.webkitWorker()
+ }
+
+ return phantom
+}
+
+func (p *Phantom) webkitWorker() {
+ for shot := range p.screenshotChan {
result, err := screenshot(shot.url)
if err == nil {
@@ -52,10 +60,9 @@ func webkitWorker() {
}
}
-func Screenshot(url string) string {
- shot := shot{url, make(chan string)}
-
- screenshotChan <- &shot
+func (p *Phantom) Screenshot(url string) string {
+ shot := shot{url, make(chan string)}
+ p.screenshotChan <- &shot
return <-shot.resultChan
}
View
10 phantom/phantom_test.go
@@ -1,10 +0,0 @@
-package phantom
-
-import (
- "testing"
-)
-
-func TestScreenshot(t *testing.T) {
-
- Screenshot("http://www.snowdevil.ca")
-}
View
10 phantom_test.go
@@ -0,0 +1,10 @@
+package main
+
+import (
+ "testing"
+)
+
+func TestScreenshot(t *testing.T) {
+ pool = NewWebkitPool(1)
+ pool.Screenshot("http://www.snowdevil.ca")
+}
View
2 run
@@ -1,3 +1,3 @@
#!/bin/sh -e
go build
-exec ./browser
+exec ./browser-go

0 comments on commit 254e3d8

Please sign in to comment.
Something went wrong with that request. Please try again.