add: [filerwatcher] base64 encoding
parent
d7664a3a9e
commit
e6be49a2a1
|
@ -2,63 +2,72 @@ package inputreader
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"github.com/gomodule/redigo/redis"
|
"encoding/base64"
|
||||||
"github.com/rjeczalik/notify"
|
"github.com/rjeczalik/notify"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
// FileWatcherReader is a abstraction a folder watcher
|
// FileWatcherReader is an abstraction of a folder watcher
|
||||||
// and behaves like a reader
|
// and behaves like a reader
|
||||||
type FileWatcherReader struct {
|
type FileWatcherReader struct {
|
||||||
// Folder to watch
|
// Folder to watch
|
||||||
folderfd os.File
|
folderfd *os.File
|
||||||
// Notify Channel
|
// Notify Channel
|
||||||
eic chan notify.EventInfo
|
eic chan notify.EventInfo
|
||||||
|
// TearDown channel
|
||||||
|
exit chan string
|
||||||
// Current buffer
|
// Current buffer
|
||||||
buf []byte
|
buf []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewLPOPReader creates a new RedisLPOPReader
|
// NewFileWatcherReader creates a new FileWatcherReader
|
||||||
func NewFileWatcherReader(f os.File) (*FileWatcherReader, error) {
|
func NewFileWatcherReader(f *os.File) (*FileWatcherReader, error) {
|
||||||
r := &FileWatcherReader{
|
r := &FileWatcherReader{
|
||||||
folderfd: f,
|
folderfd: f,
|
||||||
eic: make(chan notify.EventInfo, 1),
|
eic: make(chan notify.EventInfo, 1),
|
||||||
}
|
}
|
||||||
|
|
||||||
return r, nil
|
return r, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read waits for new file event uses a bytes reader to copy
|
// Read waits for InCloseWrite file event uses a bytes reader to copy
|
||||||
// the resulting file in p
|
// the resulting file in p
|
||||||
func (fw *FileWatcherReader) Read(p []byte) (n int, err error) {
|
func (fw *FileWatcherReader) Read(p []byte) (n int, err error) {
|
||||||
if err := notify.Watch("./...", fw.eic, notify.Remove); err != nil {
|
if err := notify.Watch("./...", fw.eic, notify.InCloseWrite); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
defer notify.Stop(fw.eic)
|
defer notify.Stop(fw.eic)
|
||||||
|
|
||||||
// Create a go routing listening the the channel
|
for{
|
||||||
|
select{
|
||||||
// select, on new event, stream content of the file
|
case ei := <-fw.eic:
|
||||||
|
// New File, let's read its content
|
||||||
// Block until event is received
|
var err error
|
||||||
ei := <-fw.eic
|
fw.buf, err = ioutil.ReadFile(ei.Path())
|
||||||
log.Println("Got event:", ei)
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
// TODO grab the ei.Path content
|
}
|
||||||
|
// base64 stream encoder
|
||||||
// Encode it in base64
|
b64buf := new(bytes.Buffer)
|
||||||
// push in buffer
|
b64encoder := base64.NewEncoder(base64.StdEncoding, b64buf)
|
||||||
// add \n
|
// Encode in Base64 to b64buf
|
||||||
|
b64encoder.Write(fw.buf)
|
||||||
//buf = append(buf, "\n"...)
|
// Close the encoder to flush partially written blocks
|
||||||
//rreader := bytes.NewReader(buf)
|
b64encoder.Close()
|
||||||
//n, err = rreader.Read(p)
|
b64buf.WriteString("\n")
|
||||||
//return n, err
|
//rreader := bytes.NewReader(fw.buf)
|
||||||
|
n, err = b64buf.Read(p)
|
||||||
|
return n, err
|
||||||
|
case <-fw.exit:
|
||||||
|
// Exiting
|
||||||
|
return 0, io.EOF
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Teardown is called on error to close the redis connection
|
// Teardown is called on error to stop the Reading loop if needed
|
||||||
func (rl *RedisLPOPReader) Teardown() {
|
func (rl *FileWatcherReader) Teardown() {
|
||||||
(*rl.r).Close()
|
rl.exit <- "exit"
|
||||||
}
|
}
|
Loading…
Reference in New Issue