@@ -10,11 +10,11 @@ import (
10
10
"os"
11
11
"path/filepath"
12
12
"regexp"
13
+ "runtime"
13
14
"sort"
14
15
"strconv"
15
16
"strings"
16
17
"time"
17
- "runtime"
18
18
19
19
"github.com/barasher/go-exiftool"
20
20
"github.com/charmbracelet/bubbles/progress"
@@ -46,14 +46,14 @@ func getWellKnownDirectories() []directory {
46
46
{location : userdirs .PublicShare , name : " PublicShare" },
47
47
}
48
48
49
- if runtime .GOOS == "darwin" {
50
- wellKnownDirectories [1 ].location = HomeDir + "/Downloads/"
51
- wellKnownDirectories [2 ].location = HomeDir + "/Documents/"
52
- wellKnownDirectories [3 ].location = HomeDir + "/Pictures/"
53
- wellKnownDirectories [4 ].location = HomeDir + "/Movies/"
54
- wellKnownDirectories [5 ].location = HomeDir + "/Music/"
55
- wellKnownDirectories [7 ].location = HomeDir + "/Public/"
56
- }
49
+ if runtime .GOOS == "darwin" {
50
+ wellKnownDirectories [1 ].location = HomeDir + "/Downloads/"
51
+ wellKnownDirectories [2 ].location = HomeDir + "/Documents/"
52
+ wellKnownDirectories [3 ].location = HomeDir + "/Pictures/"
53
+ wellKnownDirectories [4 ].location = HomeDir + "/Movies/"
54
+ wellKnownDirectories [5 ].location = HomeDir + "/Music/"
55
+ wellKnownDirectories [7 ].location = HomeDir + "/Public/"
56
+ }
57
57
58
58
for _ , dir := range wellKnownDirectories {
59
59
if _ , err := os .Stat (dir .location ); ! os .IsNotExist (err ) {
@@ -546,16 +546,16 @@ func unzip(src, dest string) error {
546
546
panic (err )
547
547
}
548
548
}()
549
- totalFile := len (r .File )
549
+ totalFiles := len (r .File )
550
550
// progessbar
551
551
prog := progress .New (progress .WithScaledGradient (theme .ProcessBarGradient [0 ], theme .ProcessBarGradient [1 ]))
552
552
prog .PercentageStyle = textStyle
553
553
// channel message
554
554
p := process {
555
- name : "test " ,
555
+ name : "unzip file " ,
556
556
progress : prog ,
557
557
state : inOperation ,
558
- total : totalFile ,
558
+ total : totalFiles ,
559
559
done : 0 ,
560
560
}
561
561
if _ , err := os .Stat (filepath .Join (dest , filepath .Base (src ))); os .IsExist (err ) {
@@ -639,8 +639,127 @@ func unzip(src, dest string) error {
639
639
}
640
640
}
641
641
642
- p .total = totalFile
642
+ p .total = totalFiles
643
+ p .state = successful
644
+ channel <- channelMessage {
645
+ messageId : id ,
646
+ processNewState : p ,
647
+ }
648
+
649
+ return nil
650
+ }
651
+
652
+ func zipSource (source , target string ) error {
653
+ id := shortuuid .New ()
654
+ prog := progress .New (progress .WithScaledGradient (theme .ProcessBarGradient [0 ], theme .ProcessBarGradient [1 ]))
655
+ prog .PercentageStyle = textStyle
656
+
657
+ totalFiles , err := countFiles (source )
658
+
659
+ if err != nil {
660
+ outPutLog ("Zip file count files error: " , err )
661
+ }
662
+
663
+ p := process {
664
+ name : "zip files" ,
665
+ progress : prog ,
666
+ state : inOperation ,
667
+ total : totalFiles ,
668
+ done : 0 ,
669
+ }
670
+
671
+ _ , err = os .Stat (target )
672
+ if os .IsExist (err ) {
673
+ p .name = " File already exist"
674
+ channel <- channelMessage {
675
+ messageId : id ,
676
+ processNewState : p ,
677
+ }
678
+ return nil
679
+ }
680
+
681
+ // 1. Create a ZIP file and zip.Writer
682
+ f , err := os .Create (target )
683
+ if err != nil {
684
+ return err
685
+ }
686
+ defer f .Close ()
687
+
688
+ writer := zip .NewWriter (f )
689
+ defer writer .Close ()
690
+
691
+ // 2. Go through all the files of the source
692
+ err = filepath .Walk (source , func (path string , info os.FileInfo , err error ) error {
693
+ p .name = filepath .Base (path )
694
+ if len (channel ) < 5 {
695
+ channel <- channelMessage {
696
+ messageId : id ,
697
+ processNewState : p ,
698
+ }
699
+ }
700
+
701
+ if err != nil {
702
+ return err
703
+ }
704
+
705
+ // 3. Create a local file header
706
+ header , err := zip .FileInfoHeader (info )
707
+ if err != nil {
708
+ return err
709
+ }
710
+
711
+ // set compression
712
+ header .Method = zip .Deflate
713
+
714
+ // 4. Set relative path of a file as the header name
715
+ header .Name , err = filepath .Rel (filepath .Dir (source ), path )
716
+ if err != nil {
717
+ return err
718
+ }
719
+ if info .IsDir () {
720
+ header .Name += "/"
721
+ }
722
+
723
+ // 5. Create writer for the file header and save content of the file
724
+ headerWriter , err := writer .CreateHeader (header )
725
+ if err != nil {
726
+ return err
727
+ }
728
+
729
+ if info .IsDir () {
730
+ return nil
731
+ }
732
+
733
+ f , err := os .Open (path )
734
+ if err != nil {
735
+ return err
736
+ }
737
+ defer f .Close ()
738
+
739
+ _ , err = io .Copy (headerWriter , f )
740
+ if err != nil {
741
+ return err
742
+ }
743
+ p .done ++
744
+ if len (channel ) < 5 {
745
+ channel <- channelMessage {
746
+ messageId : id ,
747
+ processNewState : p ,
748
+ }
749
+ }
750
+ return nil
751
+ })
752
+
753
+ if err != nil {
754
+ outPutLog ("Error while zip file:" , err )
755
+ p .state = failure
756
+ channel <- channelMessage {
757
+ messageId : id ,
758
+ processNewState : p ,
759
+ }
760
+ }
643
761
p .state = successful
762
+ p .done = totalFiles
644
763
channel <- channelMessage {
645
764
messageId : id ,
646
765
processNewState : p ,
0 commit comments