Skip to content

Commit

Permalink
improved error messages and checking channel capacity before sending …
Browse files Browse the repository at this point in the history
…a message
  • Loading branch information
sahinfalcon committed Dec 23, 2024
1 parent 55cc7fd commit 0935416
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions src/internal/file_operations_extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ func extractCompressFile(src, dest string) error {
processNewState: p,
}

if len(channel) < 5 {
channel <- message
}

x := &xtractr.XFile{
FilePath: src,
Expand All @@ -48,16 +50,21 @@ func extractCompressFile(src, dest string) error {
p.state = failure
p.doneTime = time.Now()
message.processNewState = p
if len(channel) < 5 {
channel <- message
}
outPutLog(fmt.Sprintf("Error extracting %s: %v", src, err))
return err
}

p.state = successful
p.done = 1
p.doneTime = time.Now()
message.processNewState = p
if len(channel) < 5 {
channel <- message

}

return nil
}

Expand All @@ -66,13 +73,14 @@ func unzip(src, dest string) error {
id := shortuuid.New()
r, err := zip.OpenReader(src)
if err != nil {
return err
return fmt.Errorf("failed to open zip: %w", err)
}
defer func() {
if err := r.Close(); err != nil {
panic(err)
outPutLog(fmt.Sprintf("Error closing zip reader: %v", err))
}
}()

totalFiles := len(r.File)
// progressbar
prog := progress.New(generateGradientColor())
Expand All @@ -98,11 +106,11 @@ func unzip(src, dest string) error {

rc, err := f.Open()
if err != nil {
return err
return fmt.Errorf("failed to open file in zip: %w", err)
}
defer func() {
if err := rc.Close(); err != nil {
panic(err)
outPutLog(fmt.Sprintf("Error closing file reader: %v", err))
}
}()

Expand All @@ -119,7 +127,7 @@ func unzip(src, dest string) error {
os.MkdirAll(filepath.Dir(path), f.Mode())
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return fmt.Errorf("error open file: %s", err)
return fmt.Errorf("failed to create directory: %w", err)
}
defer func() {
if err := f.Close(); err != nil {
Expand All @@ -130,15 +138,15 @@ func unzip(src, dest string) error {
_, err = io.Copy(f, rc)

if err != nil {
return fmt.Errorf("error copy file: %s", err)
return fmt.Errorf("failed to create file: %w", err)
}
}
return nil
}

for _, f := range r.File {
p.name = icon.ExtractFile + icon.Space + f.Name
if len(channel) < 3 {
if len(channel) < 5 {
message.processNewState = p
channel <- message
}
Expand All @@ -147,12 +155,14 @@ func unzip(src, dest string) error {
p.state = failure
message.processNewState = p
channel <- message
return err
outPutLog(fmt.Sprintf("Error extracting %s: %v", f.Name, err))
p.done++
continue
}
p.done++
if len(channel) < 3 {
if len(channel) < 5 {
message.processNewState = p
channel <- message
channel <- message
}
}

Expand All @@ -164,7 +174,9 @@ func unzip(src, dest string) error {
p.state = failure
}
message.processNewState = p
if len(channel) < 5 {
channel <- message
}

return nil
}

0 comments on commit 0935416

Please sign in to comment.