Skip to content

Commit

Permalink
feat: filesize
Browse files Browse the repository at this point in the history
  • Loading branch information
Sciroccogti committed May 5, 2020
1 parent cce8f06 commit 7f7fbef
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 23 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ tmpbin:
treshday: 30 # files not modified for more than this long will be moved into tmpbin, 30 days as default
deleteday: 30 # files in tmpbin for more than this long will be deleted, 30 days as default
```
4. Set your rules in `rules.yml`: (Currently support 10 rules atmost)
4. Set your rules in `rules.yml`: (Currently support 10 rules utmost)
```yml
rule1:
enable: true
Expand All @@ -25,6 +25,9 @@ rule1:
- ".*?.doc"
- ".*?.docx"
- ".*?.pdf"
threshday: 7
maxsize: # MB
minsize: # MB
rule2:
...
```
Expand Down
14 changes: 13 additions & 1 deletion rules.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,16 @@ rule1:
- ".*?.doc"
- ".*?.docx"
- ".*?.pdf"
threshday: 30
threshday: 7
maxsize:
minsize:
rule2:
enable: true
name: films
regex:
- ".*?.mp4"
- ".*?.avi"
- ".*?.flv"
threshday: 7
maxsize:
minsize: 300
22 changes: 10 additions & 12 deletions utils/classify.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,7 @@ func doRule(rule *Rule, path string, isVerbose bool) {
}

// Moving files to rule dir
for count, file := range dir {
if count > 10 {
break
}

for _, file := range dir {
// jump rule dir
if file.Name() == rule.Name || file.IsDir() {
continue
Expand All @@ -91,19 +87,21 @@ func doRule(rule *Rule, path string, isVerbose bool) {
re := regexp.MustCompile(pattern)
match := re.MatchString(file.Name())
if match {
modTime, strerr := GetFileModTime(path + rule.Name + file.Name())
modTime, strerr := GetFileModTime(path + file.Name())
if strerr == "" {
if isVerbose {
fmt.Printf("%c[0;34m%s%c[0m %c[0;32m%s%c[0m\n", 0x1B, file.Name(), 0x1B, 0x1B, modTime, 0x1B)
fmt.Printf("%c[0;34m%s%c[0m %c[0;32m%s%c[0m %d\n", 0x1B, file.Name(), 0x1B, 0x1B, modTime, 0x1B, file.Size())
}
// If file reaches deleteday
if time.Now().Unix()-modTime.Unix() >= int64(rule.Thresh*86400) {
if isVerbose {
fmt.Printf("%c[0;34m%s%c[0m matches %c[0;33m%s%c[0m\n", 0x1B, file.Name(), 0x1B, 0x1B, rule.Name, 0x1B)
if (rule.Maxsize <= 0 || file.Size() < (int64)(rule.Maxsize)*1024*1024) && file.Size() > (int64)(rule.Minsize)*1024*1024 {
if isVerbose {
fmt.Printf("%c[0;34m%s%c[0m matches %c[0;33m%s%c[0m\n", 0x1B, file.Name(), 0x1B, 0x1B, rule.Name, 0x1B)
}
src := path + file.Name()
des := path + rule.Name + file.Name()
MoveAll(file, src, des)
}
src := path + file.Name()
des := path + rule.Name + file.Name()
MoveAll(file, src, des)
}
} else {
fmt.Printf("Error while scanning %c[0;34m%s%c[0m :", 0x1B, file.Name(), 0x1B)
Expand Down
12 changes: 7 additions & 5 deletions utils/tmpbin.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ func Manage(conf *Conf) {
if dir == nil {
return
}
for count, file := range dir {
if count > 10 {
break
}
for _, file := range dir {
modTime, strerr := GetFileModTime(conf.Targetdir + conf.Tmpbin.Name + file.Name())
if strerr == "" {
if conf.Verbose {
Expand All @@ -77,7 +74,12 @@ func Manage(conf *Conf) {
fmt.Printf("Deleting %c[0;34m%s%c[0m\n", 0x1B, file.Name(), 0x1B)
//}
src := conf.Targetdir + conf.Tmpbin.Name + file.Name()
err = os.Remove(src)
if file.IsDir() {
err = os.RemoveAll(src)
} else {
err = os.Remove(src)
}

if err != nil {
fmt.Printf("Error while deleting %c[0;34m%s%c[0m :", 0x1B, file.Name(), 0x1B)
fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B)
Expand Down
10 changes: 6 additions & 4 deletions utils/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ type Conf struct {

// Rule : a template struct for a rule
type Rule struct {
Enable bool `yaml:"enable"`
Name string `yaml:"name"`
Regex []string `yaml:"regex"`
Thresh int `yaml:"threshday"`
Enable bool `yaml:"enable"`
Name string `yaml:"name"`
Regex []string `yaml:"regex"`
Thresh int `yaml:"threshday"`
Maxsize int `yaml:"maxsize"`
Minsize int `yaml:"minsize"`
}

// Rules :
Expand Down

0 comments on commit 7f7fbef

Please sign in to comment.