From c92913971ebfb85b1fcbc6e6ef52f3b86cae19fc Mon Sep 17 00:00:00 2001 From: Gnana Siva Sai V Date: Wed, 24 Jan 2024 22:17:36 +0530 Subject: [PATCH] updated the hashed filename easy to read later --- storage.go | 36 ++++++++++++++++++++---------------- storage_test.go | 7 ++++++- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/storage.go b/storage.go index 13092f4..0b4ae44 100644 --- a/storage.go +++ b/storage.go @@ -1,17 +1,16 @@ package main import ( - "bytes" - "crypto/md5" "crypto/sha1" "encoding/hex" + "fmt" "io" "log" "os" "strings" ) -func CASPathTransformFunc(key string) string { +func CASPathTransformFunc(key string) PathKey { hash := sha1.Sum([]byte(key)) hashStr := hex.EncodeToString(hash[:]) @@ -25,10 +24,22 @@ func CASPathTransformFunc(key string) string { paths[i] = hashStr[from:to] } - return strings.Join(paths, "/") + return PathKey{ + Path: strings.Join(paths, "/"), + Original: hashStr, + } +} + +type PathKey struct { + Path string + Original string } -type PathTransformFunc func(string) string +func (p PathKey) FileName() string { + return fmt.Sprintf("%s/%s", p.Path, p.Original) +} + +type PathTransformFunc func(string) PathKey type StoreOpts struct { PathTransformFunc PathTransformFunc @@ -49,25 +60,18 @@ func NewStore(opts StoreOpts) *Store { } func (s *Store) writeStream(key string, r io.Reader) error { - pathName := s.PathTransformFunc(key) - if err := os.MkdirAll(pathName, os.ModePerm); err != nil { + pathKey := s.PathTransformFunc(key) + if err := os.MkdirAll(pathKey.Path, os.ModePerm); err != nil { return err } - buf := new(bytes.Buffer) - io.Copy(buf, r) - log.Printf("read (%d) bytes from memory: %s", buf.Len(), pathName) - - filenameBytes := md5.Sum(buf.Bytes()) - filename := hex.EncodeToString(filenameBytes[:]) - pathAndFilename := pathName + "/" + filename - + pathAndFilename := pathKey.FileName() f, err := os.Create(pathAndFilename) if err != nil { return err } - n, err := io.Copy(f, buf) + n, err := io.Copy(f, r) if err != nil { return err } diff --git a/storage_test.go b/storage_test.go index 9f6f63a..8d840bd 100644 --- a/storage_test.go +++ b/storage_test.go @@ -8,8 +8,13 @@ import ( func TestPathTransformFunc(t *testing.T) { key := "encodedpathname" pathname := CASPathTransformFunc(key) + expectedOriginalKey := "2d09e004d0c86cffa599f704573b8050a4e9e109" expectedPathName := "2d09e/004d0/c86cf/fa599/f7045/73b80/50a4e/9e109" - if pathname != expectedPathName { + if pathname.Path != expectedPathName { + t.Errorf("expected %s, got %s", expectedPathName, pathname) + } + + if pathname.Original != expectedOriginalKey { t.Errorf("expected %s, got %s", expectedPathName, pathname) } }