2022-03-08 10:25:00 +01:00
|
|
|
package readers
|
|
|
|
|
2022-03-08 12:01:51 +01:00
|
|
|
import (
|
|
|
|
"StoreBackEnd/pkg/protocol"
|
2022-03-08 17:44:10 +01:00
|
|
|
"fmt"
|
2022-03-08 14:48:14 +01:00
|
|
|
"io"
|
2022-03-08 17:44:10 +01:00
|
|
|
"net"
|
|
|
|
"os"
|
2022-03-08 12:01:51 +01:00
|
|
|
)
|
2022-03-08 10:25:00 +01:00
|
|
|
|
|
|
|
// SendFileRulePrefix Rule command prefix
|
|
|
|
const SendFileRulePrefix = "ffe_sendfile"
|
|
|
|
|
|
|
|
// SendFileRule Storage structure for the SendFileRule
|
|
|
|
type SendFileRule struct {
|
|
|
|
// cmd Rule name
|
|
|
|
cmd string
|
|
|
|
// matcher Allow to make a regex match
|
|
|
|
matcher *protocol.RegexMatcher
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateSendFileRule Creating an instance of SendFileRule
|
|
|
|
func CreateSendFileRule(pattern string) protocol.IProtocolReader {
|
|
|
|
return &SendFileRule{
|
|
|
|
cmd: SendFileRulePrefix,
|
|
|
|
matcher: protocol.CreateRegexMatcher(pattern),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCmd retrieve the command name.
|
|
|
|
func (rule SendFileRule) GetCmd() string {
|
|
|
|
return rule.cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute the Rule with a string command.
|
2022-03-08 17:44:10 +01:00
|
|
|
func (rule SendFileRule) Execute(data string) (string, bool, func(r net.Conn) (string, bool)) {
|
2022-03-08 10:25:00 +01:00
|
|
|
if rule.Match(data) { // TODO : cloture this command.
|
|
|
|
values := rule.matcher.Parse(data)
|
2022-03-08 12:01:51 +01:00
|
|
|
|
2022-03-08 17:44:10 +01:00
|
|
|
// Values
|
|
|
|
fileName := values[1]
|
|
|
|
// fileSize := values[2]
|
|
|
|
// fileContentHash := values[3]
|
|
|
|
|
2022-03-08 12:01:51 +01:00
|
|
|
// print received data
|
2022-03-08 10:25:00 +01:00
|
|
|
println(values[1], " File Name Hash")
|
|
|
|
println(values[2], " File Size")
|
|
|
|
println(values[3], " File Content Hash")
|
2022-03-08 12:01:51 +01:00
|
|
|
|
|
|
|
// function callback
|
2022-03-08 17:44:10 +01:00
|
|
|
callback := func(con net.Conn) (string, bool) {
|
|
|
|
file, _ := os.Create(fmt.Sprintf("D:\\tmp\\%s", fileName))
|
2022-03-08 12:01:51 +01:00
|
|
|
|
2022-03-08 17:44:10 +01:00
|
|
|
_, err := io.Copy(file, con)
|
|
|
|
println("HEY1")
|
|
|
|
if err != nil {
|
|
|
|
println("Can't copy file")
|
|
|
|
return "SEND_ERROR\r", false
|
|
|
|
}
|
|
|
|
println("HEY2")
|
2022-03-08 14:48:14 +01:00
|
|
|
return "SEND_OK\r", true
|
2022-03-08 12:01:51 +01:00
|
|
|
}
|
2022-03-08 14:48:14 +01:00
|
|
|
return "SEND_OK\r", true, callback
|
2022-03-08 10:25:00 +01:00
|
|
|
} else {
|
2022-03-08 14:48:14 +01:00
|
|
|
println("AHAHAHAHAHAHA")
|
|
|
|
return "SEND_ERROR\r", false, nil
|
2022-03-08 10:25:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match make a match with the current Rule and check if it matches.
|
|
|
|
func (rule SendFileRule) Match(data string) bool {
|
|
|
|
return rule.matcher.Match(data)
|
|
|
|
}
|