Author: Pedro Lucas Porcellis <porcellis@eletrotupi.com>
watcher: flesh out the file watcher/uploader
cmd/curcuma/main.go | 3 + go.mod | 1 go.sum | 2 + watcher.go | 84 +++++++++++++++++++++++++++++++++++++++++++++++
diff --git a/cmd/curcuma/main.go b/cmd/curcuma/main.go index 94babce6a2e9c5f49fd4aa3a94f8324ebdcc9618..541649bd10e33a98a6a935dfd342ecb7b06ebe19 100644 --- a/cmd/curcuma/main.go +++ b/cmd/curcuma/main.go @@ -47,6 +47,9 @@ if err != nil { log.Fatalln(err) } + wtc := curcuma.NewWatcher(minioClient, config) + go wtc.Observe() + srv := curcuma.NewServer(minioClient, config) srv.Start() } diff --git a/go.mod b/go.mod index 02833aa1b9f123b4346e0ca47023345034668112..43a31f365add0bfc8ccaf44eab0b4c4b6520dbe4 100644 --- a/go.mod +++ b/go.mod @@ -5,5 +5,6 @@ require ( github.com/go-chi/chi/v5 v5.0.3 // indirect github.com/minio/minio-go/v7 v7.0.12 // indirect + github.com/radovskyb/watcher v1.0.7 // indirect github.com/vaughan0/go-ini v0.0.0-20130923145212-a98ad7ee00ec // indirect ) diff --git a/go.sum b/go.sum index 2e82949b0d616bb2c1e7d303fcfb7836dbfa3412..7ea1e90f8bea75dd13a1719b9c0eb3348acf7160 100644 --- a/go.sum +++ b/go.sum @@ -32,6 +32,8 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/radovskyb/watcher v1.0.7 h1:AYePLih6dpmS32vlHfhCeli8127LzkIgwJGcwwe8tUE= +github.com/radovskyb/watcher v1.0.7/go.mod h1:78okwvY5wPdzcb1UYnip1pvrZNIVEIh/Cm+ZuvsUYIg= github.com/rs/xid v1.2.1 h1:mhH9Nq+C1fY2l1XIpgxIiUOfNpRBYH1kKcr+qfKgjRc= github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= diff --git a/watcher.go b/watcher.go new file mode 100644 index 0000000000000000000000000000000000000000..9e349d7dd6ffc538b17d0572268fa3d9d87ad9c8 --- /dev/null +++ b/watcher.go @@ -0,0 +1,84 @@ +package curcuma + +import ( + "context" + "fmt" + "log" + "time" + "mime" + "strings" + "path" + + "github.com/radovskyb/watcher" + + "github.com/minio/minio-go/v7" + "github.com/vaughan0/go-ini" +) + +type Watcher struct { + MinioClient *minio.Client + Config ini.File +} + +func NewWatcher(mc *minio.Client, config ini.File) (*Watcher) { + return &Watcher{ + MinioClient: mc, + Config: config, + } +} + +func (w *Watcher) Observe() { + dir, _ := w.Config.Get("fs", "path") + watchmen := watcher.New() + watchmen.SetMaxEvents(1) + + watchmen.FilterOps(watcher.Write, watcher.Create) + + go func() { + for { + select { + case event := <-watchmen.Event: + fmt.Println(event) // Print the event's info. + if !event.FileInfo.IsDir() { + s3Path := strings.Replace(event.Path, dir, "", 1) + + go upload(w, event.Path, s3Path) + } + case err := <-watchmen.Error: + log.Fatalln(err) + case <-watchmen.Closed: + return + } + } + }() + + if err := watchmen.AddRecursive(dir); err != nil { + log.Fatalln(err) + } + + // TODO: Make this configurable + if err := watchmen.Start(time.Millisecond * 100); err != nil { + log.Fatalln(err) + } +} + +func upload(w *Watcher, filePath string, s3Path string) { + ctx := context.Background() + bucketName, _ := w.Config.Get("s3", "bucket") + + if strings.HasPrefix(s3Path, "/") { + s3Path = s3Path[1:] + } + + contentType := mime.TypeByExtension(path.Ext(s3Path)) + + info, err := w.MinioClient.FPutObject(ctx, bucketName, s3Path, + filePath, minio.PutObjectOptions{ContentType: contentType}, + ) + + if err != nil { + log.Fatalln(err) + } + + log.Printf("Successfully uploaded file: %s %s", s3Path, info.Size) +}