Add notifications with tests #87
8
cmd.go
8
cmd.go
|
@ -113,6 +113,8 @@ func (cmd commandAppe) Execute(conn *Conn, param string) {
|
|||
targetPath := conn.buildPath(param)
|
||||
conn.writeMessage(150, "Data transfer starting")
|
||||
|
||||
notifiers.BeforeFilePut(conn, targetPath)
|
||||
|
||||
bytes, err := conn.driver.PutFile(targetPath, conn.dataConn, true)
|
||||
if err == nil {
|
||||
msg := "OK, received " + strconv.Itoa(int(bytes)) + " bytes"
|
||||
|
@ -120,6 +122,8 @@ func (cmd commandAppe) Execute(conn *Conn, param string) {
|
|||
} else {
|
||||
conn.writeMessage(450, fmt.Sprint("error during transfer: ", err))
|
||||
}
|
||||
|
||||
notifiers.AfterFilePut(conn, targetPath, err)
|
||||
}
|
||||
|
||||
type commandOpts struct{}
|
||||
|
@ -261,12 +265,16 @@ func (cmd commandDele) RequireAuth() bool {
|
|||
|
||||
func (cmd commandDele) Execute(conn *Conn, param string) {
|
||||
path := conn.buildPath(param)
|
||||
notifiers.BeforeFileDeleted(path)
|
||||
|
||||
err := conn.driver.DeleteFile(path)
|
||||
if err == nil {
|
||||
conn.writeMessage(250, "File deleted")
|
||||
} else {
|
||||
conn.writeMessage(550, fmt.Sprint("File delete failed: ", err))
|
||||
}
|
||||
|
||||
notifiers.AfterFileDeleted(path, err)
|
||||
}
|
||||
|
||||
// commandEprt responds to the EPRT FTP command. It allows the client to
|
||||
|
|
92
notifier.go
Normal file
92
notifier.go
Normal file
|
@ -0,0 +1,92 @@
|
|||
// Copyright 2020 The goftp Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package server
|
||||
|
||||
// Notifier represents a notification operator interface
|
||||
type Notifier interface {
|
||||
BeforeUserLogin(conn *Conn, userName, password string)
|
||||
BeforeFilePut(conn *Conn, dstPath string)
|
||||
BeforeFileDeleted(dstPath string)
|
||||
BeforeDirCreated(dstPath string)
|
||||
BeforeDirDeleted(dstPath string)
|
||||
BeforeFileDownload(dstPath string)
|
||||
AfterUserLogin(conn *Conn, userName, password string)
|
||||
AfterFilePut(conn *Conn, dstPath string, err error)
|
||||
AfterFileDeleted(dstPath string, err error)
|
||||
AfterDirCreated(dstPath string)
|
||||
AfterDirDeleted(dstPath string)
|
||||
AfterFileDownload(dstPath string)
|
||||
}
|
||||
|
||||
type notifierList []Notifier
|
||||
|
||||
var (
|
||||
notifiers notifierList
|
||||
)
|
||||
|
||||
// RegisterNotifer registers a notifier
|
||||
func RegisterNotifer(notifier Notifier) {
|
||||
notifiers = append(notifiers, notifier)
|
||||
}
|
||||
|
||||
func (notifiers notifierList) BeforeFilePut(conn *Conn, dstPath string) {
|
||||
for _, notifier := range notifiers {
|
||||
notifier.BeforeFilePut(conn, dstPath)
|
||||
}
|
||||
}
|
||||
|
||||
func (notifierList) BeforeFileDeleted(dstPath string) {
|
||||
for _, notifier := range notifiers {
|
||||
notifier.BeforeFileDeleted(dstPath)
|
||||
}
|
||||
}
|
||||
|
||||
func (notifierList) BeforeDirCreated(dstPath string) {
|
||||
for _, notifier := range notifiers {
|
||||
notifier.BeforeDirCreated(dstPath)
|
||||
}
|
||||
}
|
||||
|
||||
func (notifierList) BeforeDirDeleted(dstPath string) {
|
||||
for _, notifier := range notifiers {
|
||||
notifier.BeforeDirDeleted(dstPath)
|
||||
}
|
||||
}
|
||||
|
||||
func (notifierList) BeforeFileDownload(dstPath string) {
|
||||
for _, notifier := range notifiers {
|
||||
notifier.BeforeFileDownload(dstPath)
|
||||
}
|
||||
}
|
||||
|
||||
func (notifierList) AfterFilePut(conn *Conn, dstPath string, err error) {
|
||||
for _, notifier := range notifiers {
|
||||
notifier.AfterFilePut(conn, dstPath, err)
|
||||
}
|
||||
}
|
||||
|
||||
func (notifierList) AfterFileDeleted(dstPath string, err error) {
|
||||
for _, notifier := range notifiers {
|
||||
notifier.AfterFileDeleted(dstPath, err)
|
||||
}
|
||||
}
|
||||
|
||||
func (notifierList) AfterDirCreated(dstPath string) {
|
||||
for _, notifier := range notifiers {
|
||||
notifier.AfterDirCreated(dstPath)
|
||||
}
|
||||
}
|
||||
|
||||
func (notifierList) AfterDirDeleted(dstPath string) {
|
||||
for _, notifier := range notifiers {
|
||||
notifier.AfterDirDeleted(dstPath)
|
||||
}
|
||||
}
|
||||
|
||||
func (notifierList) AfterFileDownload(dstPath string) {
|
||||
for _, notifier := range notifiers {
|
||||
notifier.AfterFileDownload(dstPath)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user