Skip to content

Commit

Permalink
fix: Removing upload buffer no longer needed now
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Nov 13, 2021
1 parent 703dbfe commit 603a583
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 25 deletions.
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,6 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/streadway/amqp v1.0.0 h1:kuuDrUJFZL1QYL9hUNuCxNObNzB0bV/ZG5jV3RWAQgo=
github.com/streadway/amqp v1.0.0/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
Expand Down
20 changes: 10 additions & 10 deletions pkg/exas/exas.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"os"
"os/exec"
Expand All @@ -14,6 +13,8 @@ import (

"github.com/ViBiOh/exas/pkg/geocode"
"github.com/ViBiOh/httputils/v4/pkg/flags"
"github.com/ViBiOh/httputils/v4/pkg/httperror"
"github.com/ViBiOh/httputils/v4/pkg/httpjson"
"github.com/ViBiOh/httputils/v4/pkg/logger"
)

Expand Down Expand Up @@ -77,7 +78,7 @@ func cleanFile(name string) {
}
}

func (a App) getExif(input string, output io.Writer) error {
func (a App) answerExif(input string, w http.ResponseWriter) {
cmd := exec.Command("./exiftool", "-json", input)

buffer := bufferPool.Get().(*bytes.Buffer)
Expand All @@ -88,12 +89,14 @@ func (a App) getExif(input string, output io.Writer) error {
cmd.Stderr = buffer

if err := cmd.Run(); err != nil {
return fmt.Errorf("unable to extract exif `%s`: %s", buffer.String(), err)
httperror.InternalServerError(w, fmt.Errorf("unable to extract exif `%s`: %s", buffer.String(), err))
return
}

var exifs []map[string]interface{}
if err := json.NewDecoder(buffer).Decode(&exifs); err != nil {
return fmt.Errorf("unable to decode exiftool output: %s", err)
httperror.InternalServerError(w, fmt.Errorf("unable to decode exiftool output: %s", err))
return
}

var exifData map[string]interface{}
Expand All @@ -103,13 +106,10 @@ func (a App) getExif(input string, output io.Writer) error {

if a.geocodeApp.Enabled() {
if err := a.geocodeApp.AppendGeocoding(exifData); err != nil {
return fmt.Errorf("unable to append geocoding: %s", err)
httperror.InternalServerError(w, fmt.Errorf("unable to append geocoding: %s", err))
return
}
}

if err := json.NewEncoder(output).Encode(exifData); err != nil {
return fmt.Errorf("unable to marshal exif data: %s", err)
}

return nil
httpjson.Write(w, http.StatusOK, exifData)
}
5 changes: 1 addition & 4 deletions pkg/exas/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,5 @@ func (a App) handleGet(w http.ResponseWriter, r *http.Request) {
return
}

if err := a.getExif(inputFilename, w); err != nil {
httperror.InternalServerError(w, err)
return
}
a.answerExif(inputFilename, w)
}
11 changes: 2 additions & 9 deletions pkg/exas/post.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package exas

import (
"bytes"
"fmt"
"io"
"net/http"
Expand All @@ -28,10 +27,7 @@ func (a App) handlePost(w http.ResponseWriter, r *http.Request) {
return
}

if err := a.getExif(inputFilename, w); err != nil {
httperror.InternalServerError(w, err)
return
}
a.answerExif(inputFilename, w)
}

func loadFile(writer io.WriteCloser, r *http.Request) (err error) {
Expand All @@ -53,9 +49,6 @@ func loadFile(writer io.WriteCloser, r *http.Request) (err error) {
}
}()

buffer := bufferPool.Get().(*bytes.Buffer)
defer bufferPool.Put(buffer)

_, err = io.CopyBuffer(writer, r.Body, buffer.Bytes())
_, err = io.Copy(writer, r.Body)
return
}

0 comments on commit 603a583

Please sign in to comment.