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 20:37:49 +01:00
|
|
|
"StoreBackEnd/pkg/utils"
|
|
|
|
"bufio"
|
|
|
|
"strconv"
|
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 20:37:49 +01:00
|
|
|
func (rule SendFileRule) Execute(data string) (string, bool, func(reader *bufio.Reader) (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]
|
2022-03-08 20:37:49 +01:00
|
|
|
fileSize, _ := strconv.Atoi(values[2])
|
2022-03-08 17:44:10 +01:00
|
|
|
// 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 20:37:49 +01:00
|
|
|
callback := func(reader *bufio.Reader) (string, bool) {
|
|
|
|
hasReceive := utils.ReceiveFile(fileName, fileSize, reader)
|
2022-03-08 17:44:10 +01:00
|
|
|
println("HEY1")
|
2022-03-08 20:37:49 +01:00
|
|
|
/*
|
|
|
|
file, _ := os.Create(fmt.Sprintf("/home/benjamin/sbe/%s", fileName))
|
|
|
|
_, err := io.Copy(file, reader)
|
|
|
|
println("HEY1")
|
|
|
|
if err != nil {
|
|
|
|
println("Can't copy file")
|
|
|
|
return "SEND_ERROR\r", false
|
|
|
|
}
|
|
|
|
|
|
|
|
*/
|
|
|
|
if !hasReceive {
|
2022-03-08 17:44:10 +01:00
|
|
|
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)
|
|
|
|
}
|