mirror of
https://github.com/syumai/workers.git
synced 2025-03-10 17:29:11 +00:00
fix implementation of R2 example and removed debug logging
This commit is contained in:
parent
78dc0e3ad8
commit
480f570fe3
@ -14,6 +14,7 @@ var (
|
||||
uint8ArrayClass = global.Get("Uint8Array")
|
||||
errorClass = global.Get("Error")
|
||||
readableStreamClass = global.Get("ReadableStream")
|
||||
stringClass = global.Get("String")
|
||||
)
|
||||
|
||||
func newObject() js.Value {
|
||||
@ -29,8 +30,6 @@ func newPromise(fn js.Func) js.Value {
|
||||
}
|
||||
|
||||
func awaitPromise(promiseVal js.Value) (js.Value, error) {
|
||||
fmt.Println("await promise")
|
||||
fmt.Println(promiseVal.Call("toString").String())
|
||||
resultCh := make(chan js.Value)
|
||||
errCh := make(chan error)
|
||||
var then, catch js.Func
|
||||
@ -49,17 +48,15 @@ func awaitPromise(promiseVal js.Value) (js.Value, error) {
|
||||
promiseVal.Call("then", then).Call("catch", catch)
|
||||
select {
|
||||
case result := <-resultCh:
|
||||
fmt.Println("got result of promise")
|
||||
return result, nil
|
||||
case err := <-errCh:
|
||||
fmt.Println("got error of promise")
|
||||
return js.Value{}, err
|
||||
}
|
||||
}
|
||||
|
||||
// dateToTime converts JavaScript side's Data object into time.Time.
|
||||
func dateToTime(v js.Value) (time.Time, error) {
|
||||
milliStr := v.Call("getTime").Call("toString").String()
|
||||
milliStr := stringClass.Invoke(v.Call("getTime")).String()
|
||||
milli, err := strconv.ParseInt(milliStr, 10, 64)
|
||||
if err != nil {
|
||||
return time.Time{}, fmt.Errorf("failed to convert Date to time.Time: %w", err)
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/syumai/workers"
|
||||
)
|
||||
@ -21,19 +22,22 @@ func handleErr(w http.ResponseWriter, msg string, err error) {
|
||||
// This example is based on implementation in syumai/workers-playground
|
||||
// * https://github.com/syumai/workers-playground/blob/e32881648ccc055e3690a0d9c750a834261c333e/r2-image-viewer/src/index.ts#L30
|
||||
func handler(w http.ResponseWriter, req *http.Request) {
|
||||
fmt.Println("new R2Bucket")
|
||||
bucket, err := NewR2Bucket(bucketName)
|
||||
if err != nil {
|
||||
handleErr(w, "failed to get R2Bucket\n", err)
|
||||
return
|
||||
}
|
||||
imgPath := req.URL.Path
|
||||
fmt.Println("bucket.get")
|
||||
imgPath := strings.TrimPrefix(req.URL.Path, "/")
|
||||
imgObj, err := bucket.Get(imgPath)
|
||||
if err != nil {
|
||||
handleErr(w, "failed to get R2Object\n", err)
|
||||
return
|
||||
}
|
||||
if imgObj == nil {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write([]byte(fmt.Sprintf("image not found: %s", imgPath)))
|
||||
return
|
||||
}
|
||||
w.Header().Set("Cache-Control", "public, max-age=14400")
|
||||
w.Header().Set("ETag", fmt.Sprintf("W/%s", imgObj.HTTPETag))
|
||||
contentType := "application/octet-stream"
|
||||
@ -41,7 +45,6 @@ func handler(w http.ResponseWriter, req *http.Request) {
|
||||
contentType = *imgObj.HTTPMetadata.ContentType
|
||||
}
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
fmt.Println("return result")
|
||||
io.Copy(w, imgObj.Body)
|
||||
}
|
||||
|
||||
|
@ -41,7 +41,6 @@ func (o *R2Object) BodyUsed() (bool, error) {
|
||||
// toR2Object converts JavaScript side's R2Object to *R2Object.
|
||||
// * https://github.com/cloudflare/workers-types/blob/3012f263fb1239825e5f0061b267c8650d01b717/index.d.ts#L1094
|
||||
func toR2Object(v js.Value) (*R2Object, error) {
|
||||
fmt.Println("toR2Object")
|
||||
uploaded, err := dateToTime(v.Get("uploaded"))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error converting uploaded: %w", err)
|
||||
|
Loading…
x
Reference in New Issue
Block a user