watch file transfer dir for changes

This commit is contained in:
William Harrell 2022-11-04 22:43:18 -04:00
parent cfc7b15211
commit 7c6029aa99

View File

@ -8,6 +8,7 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/fsnotify/fsnotify"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@ -211,6 +212,30 @@ func (ws *WebSocketHandler) Start() {
ws.logger.Err(err).Msg("sync clipboard") ws.logger.Err(err).Msg("sync clipboard")
}) })
// watch for file changes
if ws.conf.FileTransfer {
watcher, err := fsnotify.NewWatcher()
if err != nil {
ws.logger.Err(err).Msg("unable to start file transfer dir watcher")
return
}
go func() {
for {
select {
case <-watcher.Events:
ws.sendFileTransferUpdate()
case err := <-watcher.Errors:
ws.logger.Err(err).Msg("error in file transfer dir watcher")
}
}
}()
if err := watcher.Add(ws.conf.FileTransferPath); err != nil {
ws.logger.Err(err).Msg("unable to add file transfer path to watcher")
}
}
} }
func (ws *WebSocketHandler) Shutdown() error { func (ws *WebSocketHandler) Shutdown() error {
@ -354,6 +379,30 @@ func (ws *WebSocketHandler) MakeFilePath(filename string) string {
return fmt.Sprintf("%s%s", ws.conf.FileTransferPath, filename) return fmt.Sprintf("%s%s", ws.conf.FileTransferPath, filename)
} }
func (ws *WebSocketHandler) sendFileTransferUpdate() {
files, err := utils.ListFiles(ws.conf.FileTransferPath)
if err != nil {
ws.logger.Err(err).Msg("unable to ls file transfer path")
return
}
message := message.FileList{
Event: event.FILETRANSFER_LIST,
Cwd: ws.conf.FileTransferPath,
Files: *files,
}
var broadcastErr error
if ws.conf.UnprivFileTransfer {
broadcastErr = ws.sessions.Broadcast(message, nil)
} else {
broadcastErr = ws.sessions.AdminBroadcast(message, nil)
}
if broadcastErr != nil {
ws.logger.Err(broadcastErr).Msg("unable to broadcast file list")
}
}
func (ws *WebSocketHandler) authenticate(r *http.Request) (bool, error) { func (ws *WebSocketHandler) authenticate(r *http.Request) (bool, error) {
passwords, ok := r.URL.Query()["password"] passwords, ok := r.URL.Query()["password"]
if !ok || len(passwords[0]) < 1 { if !ok || len(passwords[0]) < 1 {