-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring file/folder structure. some changes were made.
- Loading branch information
1 parent
95ad4e1
commit bf35930
Showing
8 changed files
with
120 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
build: | ||
@go build -o bin/fs | ||
@go build -o bin/fs cmd/main.go | ||
|
||
run: build | ||
@./bin/fs | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package fileserver | ||
|
||
// Message is a struct that contains the payload of the message. | ||
type Message struct { | ||
Payload any | ||
} | ||
|
||
// MessageStoreFile is a struct that contains the key and the size of the file. | ||
type MessageStoreFile struct { | ||
Key string | ||
Size int64 | ||
} | ||
|
||
// MessageGetFile is a struct that contains the key of the file. | ||
type MessageGetFile struct { | ||
Key string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package store | ||
|
||
import "strings" | ||
|
||
// PathKey is a struct that contains the pathname and the original key. | ||
type PathKey struct { | ||
PathName string | ||
FileName string | ||
} | ||
|
||
// FirstPathName returns the first path name of the PathKey. | ||
func (p PathKey) FirstPathName() string { | ||
paths := strings.Split(p.PathName, "/") | ||
if len(paths) == 0 { | ||
return "" | ||
} | ||
return paths[0] | ||
} | ||
|
||
// FullPath returns the filename of the PathKey. | ||
func (p PathKey) FullPath() string { | ||
return p.PathName + "/" + p.FileName | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package main | ||
package store | ||
|
||
import ( | ||
"bytes" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package store | ||
|
||
import ( | ||
"crypto/sha1" //nolint:gosec | ||
"encoding/hex" | ||
"strings" | ||
) | ||
|
||
// PathTransformFunc is a function that transforms a path. | ||
type PathTransformFunc func(string) PathKey | ||
|
||
// CASPathTransformFunc is a function that transforms a key into a CAS path. | ||
// CAS stands for Content Addressable Storage. | ||
func CASPathTransformFunc(key string) PathKey { | ||
hash := sha1.Sum([]byte(key)) //nolint:gosec | ||
hashStr := hex.EncodeToString(hash[:]) | ||
|
||
blockSize := 5 | ||
sliceLen := len(hashStr) / blockSize | ||
paths := make([]string, sliceLen) | ||
|
||
for i := 0; i < sliceLen; i++ { | ||
from, to := i*blockSize, (i*blockSize)+blockSize | ||
paths[i] = hashStr[from:to] | ||
} | ||
|
||
return PathKey{ | ||
PathName: strings.Join(paths, "/"), | ||
FileName: hashStr, | ||
} | ||
} | ||
|
||
// DefaultPathTransformFunc is the default path transform function. | ||
var DefaultPathTransformFunc = func(key string) PathKey { | ||
return PathKey{ | ||
PathName: key, | ||
FileName: key, | ||
} | ||
} |