Skip to content

Commit

Permalink
updated the hashed filename easy to read later
Browse files Browse the repository at this point in the history
  • Loading branch information
gnana997 committed Jan 24, 2024
1 parent 2605ae0 commit c929139
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
36 changes: 20 additions & 16 deletions storage.go
Original file line number Diff line number Diff line change
@@ -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[:])

Expand All @@ -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
Expand All @@ -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
}
Expand Down
7 changes: 6 additions & 1 deletion storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down

0 comments on commit c929139

Please sign in to comment.