package main
import (
"bytes"
"context"
"fmt"
"io"
"log"
"strings"
"github.com/fastly/compute-sdk-go/fsthttp"
"github.com/rwcarlsen/goexif/exif"
"github.com/rwcarlsen/goexif/tiff"
)
const BackendName = "origin_0"
type Printer struct {
resp fsthttp.Response
}
func (p Printer) Walk(name exif.FieldName, tag *tiff.Tag) error {
s := tag.String()
if len(s) > 0 && s[0] == '"' {
s = s[1:]
}
if len(s) > 0 && s[len(s)-1] == '"' {
s = s[:len(s)-1]
}
p.resp.Header.Add("Edge-Exif-"+string(name), s)
return nil
}
func main() {
fsthttp.ServeFunc(func(ctx context.Context, w fsthttp.ResponseWriter, r *fsthttp.Request) {
resp, err := r.Send(ctx, BackendName)
if err != nil {
w.WriteHeader(fsthttp.StatusBadGateway)
fmt.Fprintln(w, err.Error())
return
}
if strings.HasPrefix(resp.Header.Get("Content-Type"), "image/") {
bodyBytes, _ := io.ReadAll(resp.Body)
resp.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
metaData, err := exif.Decode(resp.Body)
if err != nil {
log.Fatal(err)
}
var p Printer
p.resp = *resp
metaData.Walk(p)
resp.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
}
w.Header().Reset(resp.Header)
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
})
}