diff options
| author | Xe Iaso <me@xeiaso.net> | 2023-11-11 21:55:03 -0500 |
|---|---|---|
| committer | Xe Iaso <me@xeiaso.net> | 2023-11-11 21:55:03 -0500 |
| commit | 79604c40ed686b38f092ddc7d5f1c5b773837e61 (patch) | |
| tree | da54e9e4b032977352b2d49bcc8ae64377ddf4c3 | |
| parent | 8cc665a5cd3d2bef75beff372316f49fdd72fccc (diff) | |
| download | x-79604c40ed686b38f092ddc7d5f1c5b773837e61.tar.xz x-79604c40ed686b38f092ddc7d5f1c5b773837e61.zip | |
cmd/xedn: use sha256 hash for future file ids
Signed-off-by: Xe Iaso <me@xeiaso.net>
| -rw-r--r-- | cmd/xedn/imgoptimize.go | 9 |
1 files changed, 8 insertions, 1 deletions
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 { |
