Skip to content

Commit

Permalink
Cleanup functions
Browse files Browse the repository at this point in the history
  • Loading branch information
arvryna committed Feb 6, 2022
1 parent 5ea1c66 commit e95af04
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 35 deletions.
6 changes: 3 additions & 3 deletions internals/chunk/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ Chunks: The file that we want to download will be split into multiple pieces rep
segments: [[0,n1],[n1+1,n1+chunkSize]....[,n]]
*/
type Chunks struct {
type ChunkList struct {
Size int
TotalSize int
Segments []Range
Count int
}

// ComputeChunks: Compute chunks for a given parts(thread count).
func (c *Chunks) ComputeChunks() {
func (c *ChunkList) ComputeChunks() {
c.Size = int(float64(c.TotalSize) / float64(c.Count))
pos := -1
for i := 0; i < c.Count; i++ {
Expand Down Expand Up @@ -59,7 +59,7 @@ func (c *Chunks) ComputeChunks() {
}

// Merge all segments into a single file.
func (c *Chunks) Merge(outputName string, sessionID string) error {
func (c *ChunkList) Merge(outputName string, sessionID string) error {
f, err := os.OpenFile(outputName, os.O_CREATE|os.O_WRONLY|os.O_APPEND, os.ModePerm)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion internals/chunk/chunk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package chunk
import "testing"

func TestComputeChunk(t *testing.T) {
chunks := Chunks{TotalSize: 100, Count: 10}
chunks := ChunkList{TotalSize: 100, Count: 10}
chunks.ComputeChunks()
if len(chunks.Segments) != 10 {
t.Error("Segment computation failed", len(chunks.Segments))
Expand Down
4 changes: 2 additions & 2 deletions internals/network/concurrent_dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ type Dispatcher struct {

// Concurrently download the resource with specified concurrency value.
// it returns download status as bool
func (d *Dispatcher) InitiateConcurrentDispatch() (*chunk.Chunks, bool) {
func (d *Dispatcher) InitiateConcurrentDispatch() (*chunk.ChunkList, bool) {
isDownloadComplete := true

chunks := chunk.Chunks{Count: d.ThreadCount, TotalSize: int(d.Meta.ContentLength)}
chunks := chunk.ChunkList{Count: d.ThreadCount, TotalSize: int(d.Meta.ContentLength)}
chunks.ComputeChunks()

var wg sync.WaitGroup
Expand Down
13 changes: 6 additions & 7 deletions internals/network/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ import (
"time"
)

// URL validation.
func IsValidURL(str string) bool {
u, err := url.Parse(str)
return err == nil && u.Scheme != "" && u.Host != ""
}

// Build request object with custom header.
func BuildRequest(method string, url string) (*http.Request, error) {
r, err := http.NewRequest(method, url, nil)
Expand All @@ -30,8 +24,13 @@ func HTTPClient() *http.Client {
Timeout: 60 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
// We use ABSURDLY large keys, and should probably not.
TLSHandshakeTimeout: 600 * time.Second,
}
return &http.Client{Transport: t}
}

// URL validation.
func IsValidURL(str string) bool {
u, err := url.Parse(str)
return err == nil && u.Scheme != "" && u.Host != ""
}
4 changes: 2 additions & 2 deletions internals/network/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type FileMeta struct {
ServerName string
Age int
ContentType string
AcceptRanges string
AcceptRanges string
}

// Fetch the meta details of a given URL.
Expand All @@ -32,7 +32,7 @@ func (m *FileMeta) Fetch(url string) error {
defer resp.Body.Close()

if resp.StatusCode != 200 {
return fmt.Errorf("received un-expected status code: %v resp: %v", resp.StatusCode, resp)
return fmt.Errorf("Rreceived un-expected status code: %v resp: %v", resp.StatusCode, resp)
}

m.FileURL = url
Expand Down
42 changes: 22 additions & 20 deletions internals/util/file_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,6 @@ func CreateDir(folderName string, dirPath string) {
}
}

func genChecksumSha256(path string) string {
f, err := os.Open(path)
if err != nil {
fmt.Println(err)
}
defer f.Close()

hasher := sha256.New()
if _, err := io.Copy(hasher, f); err != nil {
fmt.Println(err)
}
return hex.EncodeToString(hasher.Sum(nil))
}

func FileIntegrityCheck(hashFunc string, path string, expected string) bool {
if strings.ToLower(hashFunc) == SHA256Algorithm {
return (expected == genChecksumSha256(path))
Expand All @@ -77,18 +63,14 @@ func FileIntegrityCheck(hashFunc string, path string, expected string) bool {
}

func GenHash(s string, threadCount int) string {
hash := fnv.New32a() // why not New64 ?
hash := fnv.New32a()
hash.Write([]byte(s))
return fmt.Sprintf("%v-%v", hash.Sum32(), threadCount)
}

func MemoryFormatStrings() []string {
return []string{"b", "kb", "mb", "gb", "tb", "pb"}
}

func GetFormattedSize(size float64) string {
i := 0
mem := MemoryFormatStrings()
mem := memoryFormatStrings()
for {
if size < MemUnit {
return fmt.Sprintf("%.2f", size) + " " + mem[i]
Expand All @@ -97,3 +79,23 @@ func GetFormattedSize(size float64) string {
i++
}
}

// **** Private Functions ****

func genChecksumSha256(path string) string {
f, err := os.Open(path)
if err != nil {
fmt.Println(err)
}
defer f.Close()

hasher := sha256.New()
if _, err := io.Copy(hasher, f); err != nil {
fmt.Println(err)
}
return hex.EncodeToString(hasher.Sum(nil))
}

func memoryFormatStrings() []string {
return []string{"b", "kb", "mb", "gb", "tb", "pb"}
}

0 comments on commit e95af04

Please sign in to comment.