Skip to content

Commit b98d667

Browse files
Scrape data from a local directory.
1 parent 4c579fd commit b98d667

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

files.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package main
2+
3+
import (
4+
"io/ioutil"
5+
"log"
6+
"os"
7+
"path/filepath"
8+
)
9+
10+
type File struct {
11+
Path string
12+
Key string
13+
Content string
14+
}
15+
16+
func (f *File) Read() {
17+
content, err := ioutil.ReadFile(f.Path)
18+
if err != nil {
19+
log.Printf("[-] Could not read file %s\n", f.Path)
20+
f.Content = ""
21+
} else {
22+
f.Content = string(content)
23+
}
24+
}
25+
26+
func (f *File) Process() {
27+
processContent(f.Key, f.Content)
28+
}
29+
30+
func (f *File) Delete() {
31+
os.Remove(f.Path)
32+
}
33+
34+
func scrapeFiles() {
35+
if conf.LocalPath == "" {
36+
return
37+
}
38+
39+
var files []*File
40+
41+
log.Println("[+] Checking for local pastes.")
42+
43+
filepath.Walk(conf.LocalPath, func(path string, info os.FileInfo, err error) error {
44+
if err != nil {
45+
log.Printf("[-] Error reading %s: %s\n", conf.LocalPath, err.Error())
46+
return nil
47+
}
48+
49+
if info.IsDir() {
50+
log.Printf("[+] Skipping directory %s\n", path)
51+
return nil
52+
}
53+
54+
files = append(files, &File{Path: path, Key: filepath.Base(path)})
55+
56+
return nil
57+
})
58+
59+
for i := range files {
60+
f := files[i]
61+
f.Read()
62+
f.Process()
63+
f.Delete()
64+
}
65+
}

0 commit comments

Comments
 (0)