Skip to content

Commit 3245d0a

Browse files
committed
Updated Go implementation for Go release version 1.
1 parent c2d67ff commit 3245d0a

File tree

3 files changed

+43
-41
lines changed

3 files changed

+43
-41
lines changed

server/gae-go/app.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
application: jquery-file-upload
22
version: 2
33
runtime: go
4-
api_version: 3
4+
api_version: go1
55

66
handlers:
77
- url: /(favicon\.ico|robots\.txt)

server/gae-go/app/main.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* jQuery File Upload Plugin GAE Go Example 1.2.1
2+
* jQuery File Upload Plugin GAE Go Example 2.0
33
* https://github.com/blueimp/jQuery-File-Upload
44
*
55
* Copyright 2011, Sebastian Tschan
@@ -18,19 +18,19 @@ import (
1818
"appengine/taskqueue"
1919
"bytes"
2020
"encoding/base64"
21+
"encoding/json"
2122
"fmt"
22-
"http"
2323
"image"
2424
"image/png"
2525
"io"
26-
"json"
2726
"log"
2827
"mime/multipart"
29-
"os"
28+
"net/http"
29+
"net/url"
3030
"regexp"
3131
"resize"
3232
"strings"
33-
"url"
33+
"time"
3434
)
3535

3636
import _ "image/gif"
@@ -103,7 +103,7 @@ func (fi *FileInfo) CreateUrls(r *http.Request, c appengine.Context) {
103103
}
104104
}
105105

106-
func (fi *FileInfo) CreateThumbnail(r io.Reader, c appengine.Context) (data []byte, err os.Error) {
106+
func (fi *FileInfo) CreateThumbnail(r io.Reader, c appengine.Context) (data []byte, err error) {
107107
defer func() {
108108
if rec := recover(); rec != nil {
109109
log.Println(rec)
@@ -139,7 +139,7 @@ func (fi *FileInfo) CreateThumbnail(r io.Reader, c appengine.Context) (data []by
139139
return
140140
}
141141

142-
func check(err os.Error) {
142+
func check(err error) {
143143
if err != nil {
144144
panic(err)
145145
}
@@ -153,7 +153,8 @@ func delayedDelete(c appengine.Context, fi *FileInfo) {
153153
if key := string(fi.Key); key != "" {
154154
task := &taskqueue.Task{
155155
Path: "/" + escape(key) + "/-",
156-
Method: "DELETE", Delay: EXPIRATION_TIME * 1000000,
156+
Method: "DELETE",
157+
Delay: time.Duration(EXPIRATION_TIME) * time.Second,
157158
}
158159
taskqueue.Add(c, task, "")
159160
}
@@ -170,11 +171,11 @@ func handleUpload(r *http.Request, p *multipart.Part) (fi *FileInfo) {
170171
defer func() {
171172
if rec := recover(); rec != nil {
172173
log.Println(rec)
173-
fi.Error = rec.(os.Error).String()
174+
fi.Error = rec.(error).Error()
174175
}
175176
}()
176177
var b bytes.Buffer
177-
lr := &io.LimitedReader{p, MAX_FILE_SIZE + 1}
178+
lr := &io.LimitedReader{R: p, N: MAX_FILE_SIZE + 1}
178179
context := appengine.NewContext(r)
179180
w, err := blobstore.Create(context, fi.Type)
180181
defer func() {
@@ -204,7 +205,7 @@ func handleUpload(r *http.Request, p *multipart.Part) (fi *FileInfo) {
204205

205206
func getFormValue(p *multipart.Part) string {
206207
var b bytes.Buffer
207-
io.Copyn(&b, p, int64(1<<20)) // Copy max: 1 MiB
208+
io.CopyN(&b, p, int64(1<<20)) // Copy max: 1 MiB
208209
return b.String()
209210
}
210211

server/gae-go/resize/resize.go

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package resize
66

77
import (
88
"image"
9-
"image/ycbcr"
9+
"image/color"
1010
)
1111

1212
// Resize returns a scaled copy of the image slice r of m.
@@ -16,12 +16,12 @@ func Resize(m image.Image, r image.Rectangle, w, h int) image.Image {
1616
return nil
1717
}
1818
if w == 0 || h == 0 || r.Dx() <= 0 || r.Dy() <= 0 {
19-
return image.NewRGBA64(w, h)
19+
return image.NewRGBA64(image.Rect(0, 0, w, h))
2020
}
2121
switch m := m.(type) {
2222
case *image.RGBA:
2323
return resizeRGBA(m, r, w, h)
24-
case *ycbcr.YCbCr:
24+
case *image.YCbCr:
2525
if m, ok := resizeYCbCr(m, r, w, h); ok {
2626
return m
2727
}
@@ -66,14 +66,14 @@ func Resize(m image.Image, r image.Rectangle, w, h int) image.Image {
6666
b64 := uint64(b32)
6767
a64 := uint64(a32)
6868
// Spread the source pixel over 1 or more destination rows.
69-
py := uint64(y-r.Min.Y) * hh
69+
py := uint64(y) * hh
7070
for remy := hh; remy > 0; {
7171
qy := dy - (py % dy)
7272
if qy > remy {
7373
qy = remy
7474
}
7575
// Spread the source pixel over 1 or more destination columns.
76-
px := uint64(x-r.Min.X) * ww
76+
px := uint64(x) * ww
7777
index := 4 * ((py/dy)*ww + (px / dx))
7878
for remx := ww; remx > 0; {
7979
qx := dx - (px % dx)
@@ -98,28 +98,29 @@ func Resize(m image.Image, r image.Rectangle, w, h int) image.Image {
9898

9999
// average convert the sums to averages and returns the result.
100100
func average(sum []uint64, w, h int, n uint64) image.Image {
101-
ret := image.NewRGBA(w, h)
101+
ret := image.NewRGBA(image.Rect(0, 0, w, h))
102102
for y := 0; y < h; y++ {
103103
for x := 0; x < w; x++ {
104-
i := y*ret.Stride + x*4
105-
j := 4 * (y*w + x)
106-
ret.Pix[i+0] = uint8(sum[j+0] / n)
107-
ret.Pix[i+1] = uint8(sum[j+1] / n)
108-
ret.Pix[i+2] = uint8(sum[j+2] / n)
109-
ret.Pix[i+3] = uint8(sum[j+3] / n)
104+
index := 4 * (y*w + x)
105+
ret.SetRGBA(x, y, color.RGBA{
106+
uint8(sum[index+0] / n),
107+
uint8(sum[index+1] / n),
108+
uint8(sum[index+2] / n),
109+
uint8(sum[index+3] / n),
110+
})
110111
}
111112
}
112113
return ret
113114
}
114115

115116
// resizeYCbCr returns a scaled copy of the YCbCr image slice r of m.
116117
// The returned image has width w and height h.
117-
func resizeYCbCr(m *ycbcr.YCbCr, r image.Rectangle, w, h int) (image.Image, bool) {
118+
func resizeYCbCr(m *image.YCbCr, r image.Rectangle, w, h int) (image.Image, bool) {
118119
var verticalRes int
119120
switch m.SubsampleRatio {
120-
case ycbcr.SubsampleRatio420:
121+
case image.YCbCrSubsampleRatio420:
121122
verticalRes = 2
122-
case ycbcr.SubsampleRatio422:
123+
case image.YCbCrSubsampleRatio422:
123124
verticalRes = 1
124125
default:
125126
return nil, false
@@ -134,19 +135,19 @@ func resizeYCbCr(m *ycbcr.YCbCr, r image.Rectangle, w, h int) (image.Image, bool
134135
Cr := m.Cr[y/verticalRes*m.CStride:]
135136
for x := r.Min.X; x < r.Max.X; x++ {
136137
// Get the source pixel.
137-
r8, g8, b8 := ycbcr.YCbCrToRGB(Y[x], Cb[x/2], Cr[x/2])
138+
r8, g8, b8 := color.YCbCrToRGB(Y[x], Cb[x/2], Cr[x/2])
138139
r64 := uint64(r8)
139140
g64 := uint64(g8)
140141
b64 := uint64(b8)
141142
// Spread the source pixel over 1 or more destination rows.
142-
py := uint64(y-r.Min.Y) * hh
143+
py := uint64(y) * hh
143144
for remy := hh; remy > 0; {
144145
qy := dy - (py % dy)
145146
if qy > remy {
146147
qy = remy
147148
}
148149
// Spread the source pixel over 1 or more destination columns.
149-
px := uint64(x-r.Min.X) * ww
150+
px := uint64(x) * ww
150151
index := 4 * ((py/dy)*ww + (px / dx))
151152
for remx := ww; remx > 0; {
152153
qx := dx - (px % dx)
@@ -178,23 +179,23 @@ func resizeRGBA(m *image.RGBA, r image.Rectangle, w, h int) image.Image {
178179
// See comment in Resize.
179180
n, sum := dx*dy, make([]uint64, 4*w*h)
180181
for y := r.Min.Y; y < r.Max.Y; y++ {
181-
pix := m.Pix[(y-m.Rect.Min.Y)*m.Stride:]
182+
pixOffset := m.PixOffset(r.Min.X, y)
182183
for x := r.Min.X; x < r.Max.X; x++ {
183184
// Get the source pixel.
184-
p := pix[(x-m.Rect.Min.X)*4:]
185-
r64 := uint64(p[0])
186-
g64 := uint64(p[1])
187-
b64 := uint64(p[2])
188-
a64 := uint64(p[3])
185+
r64 := uint64(m.Pix[pixOffset+0])
186+
g64 := uint64(m.Pix[pixOffset+1])
187+
b64 := uint64(m.Pix[pixOffset+2])
188+
a64 := uint64(m.Pix[pixOffset+3])
189+
pixOffset += 4
189190
// Spread the source pixel over 1 or more destination rows.
190-
py := uint64(y-r.Min.Y) * hh
191+
py := uint64(y) * hh
191192
for remy := hh; remy > 0; {
192193
qy := dy - (py % dy)
193194
if qy > remy {
194195
qy = remy
195196
}
196197
// Spread the source pixel over 1 or more destination columns.
197-
px := uint64(x-r.Min.X) * ww
198+
px := uint64(x) * ww
198199
index := 4 * ((py/dy)*ww + (px / dx))
199200
for remx := ww; remx > 0; {
200201
qx := dx - (px % dx)
@@ -225,10 +226,10 @@ func Resample(m image.Image, r image.Rectangle, w, h int) image.Image {
225226
return nil
226227
}
227228
if w == 0 || h == 0 || r.Dx() <= 0 || r.Dy() <= 0 {
228-
return image.NewRGBA64(w, h)
229+
return image.NewRGBA64(image.Rect(0, 0, w, h))
229230
}
230231
curw, curh := r.Dx(), r.Dy()
231-
img := image.NewRGBA(w, h)
232+
img := image.NewRGBA(image.Rect(0, 0, w, h))
232233
for y := 0; y < h; y++ {
233234
for x := 0; x < w; x++ {
234235
// Get a source pixel.
@@ -239,7 +240,7 @@ func Resample(m image.Image, r image.Rectangle, w, h int) image.Image {
239240
g := uint8(g32 >> 8)
240241
b := uint8(b32 >> 8)
241242
a := uint8(a32 >> 8)
242-
img.SetRGBA(x, y, image.RGBAColor{r, g, b, a})
243+
img.SetRGBA(x, y, color.RGBA{r, g, b, a})
243244
}
244245
}
245246
return img

0 commit comments

Comments
 (0)