2019-08-26 20:56:17 +00:00
|
|
|
package awsuploader
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
2020-04-29 20:51:32 +00:00
|
|
|
"github.com/cloudflare/cloudflared/logger"
|
2019-08-26 20:56:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// DirectoryUploadManager is used to manage file uploads on an interval from a directory
|
|
|
|
type DirectoryUploadManager struct {
|
2020-04-29 20:51:32 +00:00
|
|
|
logger logger.Service
|
2019-08-26 20:56:17 +00:00
|
|
|
uploader Uploader
|
|
|
|
rootDirectory string
|
|
|
|
sweepInterval time.Duration
|
|
|
|
ticker *time.Ticker
|
|
|
|
shutdownC chan struct{}
|
|
|
|
workQueue chan string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDirectoryUploadManager create a new DirectoryUploadManager
|
|
|
|
// uploader is an Uploader to use as an actual uploading engine
|
|
|
|
// directory is the directory to sweep for files to upload
|
|
|
|
// sweepInterval is how often to iterate the directory and upload the files within
|
2020-04-29 20:51:32 +00:00
|
|
|
func NewDirectoryUploadManager(logger logger.Service, uploader Uploader, directory string, sweepInterval time.Duration, shutdownC chan struct{}) *DirectoryUploadManager {
|
2019-08-26 20:56:17 +00:00
|
|
|
workerCount := 10
|
|
|
|
manager := &DirectoryUploadManager{
|
|
|
|
logger: logger,
|
|
|
|
uploader: uploader,
|
|
|
|
rootDirectory: directory,
|
|
|
|
sweepInterval: sweepInterval,
|
|
|
|
shutdownC: shutdownC,
|
|
|
|
workQueue: make(chan string, workerCount),
|
|
|
|
}
|
|
|
|
|
|
|
|
//start workers
|
|
|
|
for i := 0; i < workerCount; i++ {
|
|
|
|
go manager.worker()
|
|
|
|
}
|
|
|
|
|
|
|
|
return manager
|
|
|
|
}
|
|
|
|
|
|
|
|
// Upload a file using the uploader
|
|
|
|
// This is useful for "out of band" uploads that need to be triggered immediately instead of waiting for the sweep
|
|
|
|
func (m *DirectoryUploadManager) Upload(filepath string) error {
|
|
|
|
return m.uploader.Upload(filepath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the upload ticker to walk the directories
|
|
|
|
func (m *DirectoryUploadManager) Start() {
|
|
|
|
m.ticker = time.NewTicker(m.sweepInterval)
|
|
|
|
go m.run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DirectoryUploadManager) run() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-m.shutdownC:
|
|
|
|
m.ticker.Stop()
|
|
|
|
return
|
|
|
|
case <-m.ticker.C:
|
|
|
|
m.sweep()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sweep the directory and kick off uploads
|
|
|
|
func (m *DirectoryUploadManager) sweep() {
|
|
|
|
filepath.Walk(m.rootDirectory, func(path string, info os.FileInfo, err error) error {
|
2019-08-30 20:20:49 +00:00
|
|
|
if err != nil || info.IsDir() {
|
2019-08-26 20:56:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
//30 days ago
|
|
|
|
retentionTime := 30 * (time.Hour * 24)
|
|
|
|
checkTime := time.Now().Add(-time.Duration(retentionTime))
|
|
|
|
|
|
|
|
//delete the file it is stale
|
2019-09-03 21:28:06 +00:00
|
|
|
if info.ModTime().Before(checkTime) {
|
2019-08-26 20:56:17 +00:00
|
|
|
os.Remove(path)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
//add the upload to the work queue
|
|
|
|
go func() {
|
|
|
|
m.workQueue <- path
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// worker handles upload requests
|
|
|
|
func (m *DirectoryUploadManager) worker() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-m.shutdownC:
|
|
|
|
return
|
|
|
|
case filepath := <-m.workQueue:
|
|
|
|
if err := m.Upload(filepath); err != nil {
|
2020-04-29 20:51:32 +00:00
|
|
|
m.logger.Errorf("Cannot upload file to s3 bucket: %s", err)
|
2019-08-26 20:56:17 +00:00
|
|
|
} else {
|
|
|
|
os.Remove(filepath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|