From 79604c40ed686b38f092ddc7d5f1c5b773837e61 Mon Sep 17 00:00:00 2001 From: Xe Iaso Date: Sat, 11 Nov 2023 21:55:03 -0500 Subject: cmd/xedn: use sha256 hash for future file ids Signed-off-by: Xe Iaso --- cmd/xedn/imgoptimize.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/cmd/xedn/imgoptimize.go b/cmd/xedn/imgoptimize.go index ad2f367..a28a492 100644 --- a/cmd/xedn/imgoptimize.go +++ b/cmd/xedn/imgoptimize.go @@ -2,6 +2,7 @@ package main import ( "bytes" + "crypto/sha256" "encoding/json" "errors" "flag" @@ -263,6 +264,8 @@ func (iu *ImageUploader) CreateImage(w http.ResponseWriter, r *http.Request) { ctx := r.Context() id := uuid.New().String() + defer r.Body.Close() + os.MkdirAll(filepath.Join(*dir, "uploud"), 0700) fout, err := os.Create(filepath.Join(*dir, "uploud", id+".png")) @@ -272,7 +275,9 @@ func (iu *ImageUploader) CreateImage(w http.ResponseWriter, r *http.Request) { return } - if n, err := io.Copy(fout, r.Body); err != nil { + h := sha256.New() + + if n, err := io.Copy(io.MultiWriter(h, fout), r.Body); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) slog.Error("cannot copy image to buffer", "err", err) return @@ -340,6 +345,8 @@ func (iu *ImageUploader) CreateImage(w http.ResponseWriter, r *http.Request) { return } + id = fmt.Sprintf("%x", h.Sum(nil)) + s3c := mkS3Client() for _, finfo := range files { -- cgit v1.2.3