This repository has been archived by the owner on Nov 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
working on filter system for processing.
- Loading branch information
Showing
7 changed files
with
225 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package model | ||
|
||
import ( | ||
"github.com/sabhiram/go-gitignore" | ||
) | ||
|
||
type Filter struct { | ||
Include []string `json:"include"` | ||
Exclude []string `json:"exclude"` | ||
|
||
incMatcher *ignore.GitIgnore | ||
excMatcher *ignore.GitIgnore | ||
} | ||
|
||
func (flt *Filter) ValidPath(pathToTest string) bool { | ||
|
||
if flt.incMatcher == nil { | ||
incMatcher, err := ignore.CompileIgnoreLines(flt.Include...) | ||
if err != nil { | ||
return true | ||
} | ||
flt.incMatcher = incMatcher | ||
} | ||
if flt.excMatcher == nil { | ||
excMatcher, err := ignore.CompileIgnoreLines(flt.Exclude...) | ||
if err != nil { | ||
return true | ||
} | ||
flt.excMatcher = excMatcher | ||
} | ||
|
||
//includes always override excludes | ||
matched := flt.incMatcher.MatchesPath(pathToTest) | ||
if matched { | ||
return true //if the file matches a pattern in "include", we need to tell the processor to continue | ||
} | ||
|
||
//test excludes next | ||
matched = flt.excMatcher.MatchesPath(pathToTest) | ||
if matched { | ||
return false //if the file matches a pattern in "exclude", we need to tell the processor to skip it | ||
} | ||
|
||
//by default, we'll include all files | ||
return true | ||
} |
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,103 @@ | ||
package model | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestFilter_ValidPath(t *testing.T) { | ||
|
||
filter := Filter{ | ||
Include: []string{"hello", "helloworld"}, | ||
Exclude: []string{".DS_Store", ".*"}, | ||
} | ||
|
||
assert.True(t, filter.ValidPath("subdirectory/hello")) | ||
|
||
assert.False(t, filter.ValidPath(".DS_Store")) | ||
assert.False(t, filter.ValidPath("subdirectory/.DS_Store")) | ||
assert.False(t, filter.ValidPath("subdirectory/.anything")) | ||
assert.False(t, filter.ValidPath(".anything")) | ||
|
||
assert.True(t, filter.ValidPath("hello/.DS_Store")) | ||
} | ||
|
||
func TestFilter_FromJson(t *testing.T) { | ||
|
||
bodyJson := ` | ||
{ | ||
"include": [ | ||
"*.doc", | ||
"*.docx", | ||
"*.xls", | ||
"*.xlsx", | ||
"*.ppt", | ||
"*.pptx", | ||
"*.pages", | ||
"*.numbers", | ||
"*.key", | ||
"*.pdf", | ||
"*.rtf", | ||
"*.md", | ||
"*.jpg", | ||
"*.jpeg", | ||
"*.png", | ||
"*.gif", | ||
"*.webp", | ||
"*.tiff", | ||
"*.tif", | ||
"*.html" | ||
], | ||
"exclude": [ | ||
".*", | ||
".DS_Store", | ||
".AppleDouble", | ||
".LSOverride", | ||
"._*", | ||
".DocumentRevisions-V100", | ||
".fseventsd", | ||
".Spotlight-V100", | ||
".TemporaryItems", | ||
".Trashes", | ||
".VolumeIcon.icns", | ||
".com.apple.timemachine.donotpresent", | ||
".AppleDB", | ||
".AppleDesktop", | ||
"Network Trash Folder", | ||
"Temporary Items", | ||
".apdisk", | ||
"Thumbs.db", | ||
"Thumbs.db:encryptable", | ||
"ehthumbs.db", | ||
"ehthumbs_vista.db", | ||
"*.stackdump", | ||
"Desktop.ini", | ||
"desktop.ini", | ||
"*.cab", | ||
"*.msi", | ||
"*.msix", | ||
"*.msm", | ||
"*.msp", | ||
"*.lnk" | ||
] | ||
} | ||
` | ||
|
||
var filter Filter | ||
err := json.Unmarshal([]byte(bodyJson), &filter) | ||
assert.Nil(t, err) | ||
|
||
assert.True(t, filter.ValidPath("subdirectory/hello.png")) | ||
assert.True(t, filter.ValidPath("subdirectory/hello.jpeg")) | ||
assert.True(t, filter.ValidPath("subdirectory/hello.gif")) | ||
assert.True(t, filter.ValidPath("subdirectory/embedded.msi/inpath")) | ||
|
||
assert.False(t, filter.ValidPath(".DS_Store")) | ||
assert.False(t, filter.ValidPath("subdirectory/.DS_Store")) | ||
assert.False(t, filter.ValidPath("subdirectory/Thumbs.db")) | ||
assert.False(t, filter.ValidPath("helloworld/testing.cab")) | ||
assert.False(t, filter.ValidPath("subdirectory/.Trashes")) | ||
} |
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,31 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/analogj/lodestone-processor/pkg/model" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
func GetIncludeExcludeData(apiEndpoint *url.URL) (model.Filter, error) { | ||
|
||
//manipulate the path | ||
apiEndpoint.Path = "/api/v1/data/filetypes.json" | ||
|
||
resp, err := http.Get(apiEndpoint.String()) | ||
if err != nil { | ||
return model.Filter{}, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
bodyJson, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return model.Filter{}, err | ||
} | ||
|
||
var filter model.Filter | ||
err = json.Unmarshal(bodyJson, &filter) | ||
|
||
return filter, err | ||
} |
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