-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract.go
98 lines (84 loc) · 2.38 KB
/
extract.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import "github.com/mholt/archiver"
import "io"
import "path"
import "path/filepath"
import "github.com/elgs/gostrgen"
import "github.com/jagadeesh-kotra/gorar"
import "os"
import "fmt"
type Unpack struct {
TempDir string
TargetDir string
InitialPath string
}
func (u *Unpack) Unpack(archivePath string) {
randomFolder, _ := gostrgen.RandGen(15, gostrgen.Lower | gostrgen.Upper, "", "")
randomPath := path.Join(u.TempDir, randomFolder)
if path.Ext(archivePath) == ".rar" {
fmt.Println(randomPath)
gorar.RarExtractor(archivePath, randomPath)
} else if path.Ext(archivePath)[0:2] != ".r" {
err := archiver.Unarchive(archivePath, randomPath)
if (err != nil) {
panic(err)
}
}
filepath.Walk(randomPath, u.Process)
os.RemoveAll(randomPath)
}
func (u *Unpack) DesiredFile(filePath string) bool {
fmt.Println(filePath)
switch path.Ext(filePath) {
case ".mp3", ".mp4", ".srt", ".mkv", ".avi":
return true
}
return false
}
func (u *Unpack) Do() (suc bool) {
suc = false
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered in f", r)
}
}()
u.Process(u.InitialPath, nil, nil)
os.Remove(u.InitialPath)
suc = true
return
}
func copy_file(src, dst string) (int64, error) {
sourceFileStat, err := os.Stat(src)
if err != nil {
return 0, err
}
if !sourceFileStat.Mode().IsRegular() {
return 0, fmt.Errorf("%s is not a regular file", src)
}
source, err := os.Open(src)
if err != nil {
return 0, err
}
defer source.Close()
destination, err := os.Create(dst)
if err != nil {
return 0, err
}
defer destination.Close()
nBytes, err := io.Copy(destination, source)
return nBytes, err
}
func (u *Unpack) Process(filePath string, info os.FileInfo, err error) error {
_, err2 := archiver.ByExtension(filePath)
if (err2 == nil) {
u.Unpack(filePath)
} else if (u.DesiredFile(filePath)) {
os.MkdirAll(u.TargetDir, 0777)
//os.Rename(filePath, path.Join(u.TargetDir, path.Base(filePath)))
copy_file(filePath, path.Join(u.TargetDir, path.Base(filePath)))
os.Remove(filePath)
} else {
fmt.Println("3rd case")
}
return nil
}