-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SYNOFILE_THUMB_{some pattern}.jpg should be automatically skipped fro…
- Loading branch information
Showing
19 changed files
with
382 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package namematcher | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
"unicode" | ||
"unicode/utf8" | ||
) | ||
|
||
// List of file patterns used to ban unwanted files | ||
// Pattern can be a part of the path, a file name.. | ||
|
||
type List struct { | ||
re []*regexp.Regexp | ||
patterns []string | ||
} | ||
|
||
func New(patterns ...string) (List, error) { | ||
l := List{} | ||
for _, name := range patterns { | ||
err := l.Set(name) | ||
if err != nil { | ||
return List{}, err | ||
} | ||
} | ||
return l, nil | ||
} | ||
|
||
func (l List) Match(name string) bool { | ||
for _, re := range l.re { | ||
if re.MatchString(name) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func fetchRune(b []byte) ([]byte, rune) { | ||
r, size := utf8.DecodeRune(b) | ||
b = b[size:] | ||
return b, r | ||
} | ||
|
||
// transform a glob styled pattern into a regular expression | ||
func patternToRe(pattern string) (*regexp.Regexp, error) { | ||
var r strings.Builder | ||
var inBrackets bool | ||
var b rune | ||
buf := []byte(pattern) | ||
|
||
r.WriteString("(?i)") // make the pattern case insensitive | ||
|
||
for len(buf) > 0 { | ||
buf, b = fetchRune(buf) | ||
switch b { | ||
case '*': | ||
r.WriteString(`[^./]*`) | ||
case '?': | ||
r.WriteString(`[^./]`) | ||
case '.', '^', '$', '(', ')', '|': | ||
r.WriteRune('\\') | ||
r.WriteRune(b) | ||
case '\\': | ||
r.WriteRune(b) | ||
buf, b = fetchRune(buf) | ||
r.WriteRune(b) | ||
case '[': | ||
inBrackets = true | ||
r.WriteRune(b) | ||
brackets: | ||
for len(buf) > 0 { | ||
buf, b = fetchRune(buf) | ||
switch b { | ||
case ']': | ||
inBrackets = false | ||
r.WriteRune(b) | ||
break brackets | ||
default: | ||
lCase, uCase := unicode.ToLower(b), unicode.ToUpper(b) | ||
r.WriteRune(lCase) | ||
if lCase != uCase { | ||
r.WriteRune(uCase) | ||
} | ||
} | ||
} | ||
default: | ||
r.WriteRune(b) | ||
} | ||
} | ||
if inBrackets { | ||
return nil, fmt.Errorf("invalid file name pattern: %s", pattern) | ||
} | ||
re, err := regexp.Compile(r.String()) | ||
if err != nil { | ||
return nil, fmt.Errorf("invalid file name pattern: %s", pattern) | ||
} | ||
return re, nil | ||
} | ||
|
||
/* | ||
Implements the flag.Value interface for the list of banned files | ||
Check the validity of the pattern | ||
*/ | ||
|
||
func (l *List) Set(s string) error { | ||
if l == nil { | ||
return errors.New("namematcher list not initialized") | ||
} | ||
if s == "" { | ||
return nil | ||
} | ||
re, err := patternToRe(s) | ||
if err != nil { | ||
return err | ||
} | ||
l.re = append(l.re, re) | ||
l.patterns = append(l.patterns, s) | ||
return nil | ||
} | ||
|
||
func (l List) String() string { | ||
var s strings.Builder | ||
for i, pattern := range l.patterns { | ||
if i > 0 { | ||
s.WriteString(", ") | ||
} | ||
s.WriteRune('\'') | ||
s.WriteString(pattern) | ||
s.WriteRune('\'') | ||
} | ||
return s.String() | ||
} | ||
|
||
func (l *List) Get() any { | ||
return *l | ||
} |
Oops, something went wrong.