2022-03-08 10:25:00 +01:00
|
|
|
package readers
|
|
|
|
|
2022-03-08 12:01:51 +01:00
|
|
|
import (
|
|
|
|
"StoreBackEnd/pkg/protocol"
|
2022-03-12 15:57:27 +01:00
|
|
|
"StoreBackEnd/pkg/protocol/repository"
|
|
|
|
"StoreBackEnd/pkg/protocol/rules/writers"
|
2022-03-08 20:37:49 +01:00
|
|
|
"StoreBackEnd/pkg/utils"
|
|
|
|
"bufio"
|
2022-03-13 12:29:05 +01:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2022-03-08 20:37:49 +01:00
|
|
|
"strconv"
|
2022-03-08 12:01:51 +01:00
|
|
|
)
|
2022-03-08 10:25:00 +01:00
|
|
|
|
|
|
|
// SendFileRulePrefix Rule command prefix
|
2022-03-14 11:10:23 +01:00
|
|
|
const SendFileRulePrefix = "SENDFILE"
|
2022-03-08 10:25:00 +01:00
|
|
|
|
|
|
|
// SendFileRule Storage structure for the SendFileRule
|
|
|
|
type SendFileRule struct {
|
|
|
|
// cmd Rule name
|
|
|
|
cmd string
|
|
|
|
// matcher Allow to make a regex match
|
|
|
|
matcher *protocol.RegexMatcher
|
2022-03-12 15:57:27 +01:00
|
|
|
// protocolRepo Instance de ProtocolRepository
|
|
|
|
protocolRepo *repository.ProtocolRepository
|
2022-03-13 12:29:05 +01:00
|
|
|
// storagePath Chemin de stockage du fichier
|
|
|
|
storagePath string
|
2022-03-08 10:25:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateSendFileRule Creating an instance of SendFileRule
|
2022-03-13 12:29:05 +01:00
|
|
|
func CreateSendFileRule(pattern string, protocolRepo *repository.ProtocolRepository, storagePath string) protocol.IProtocolReader {
|
2022-03-08 10:25:00 +01:00
|
|
|
return &SendFileRule{
|
2022-03-12 15:57:27 +01:00
|
|
|
cmd: SendFileRulePrefix,
|
|
|
|
matcher: protocol.CreateRegexMatcher(pattern),
|
|
|
|
protocolRepo: protocolRepo,
|
2022-03-13 12:29:05 +01:00
|
|
|
storagePath: storagePath,
|
2022-03-08 10:25:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCmd retrieve the command name.
|
|
|
|
func (rule SendFileRule) GetCmd() string {
|
|
|
|
return rule.cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute the Rule with a string command.
|
2022-03-12 15:57:27 +01:00
|
|
|
func (rule SendFileRule) Execute(data string) (*protocol.ProtocolWriterResult, func(reader *bufio.Reader) *protocol.ProtocolWriterResult) {
|
2022-03-20 15:32:45 +01:00
|
|
|
|
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-13 12:29:05 +01:00
|
|
|
fileContentHash := values[3]
|
|
|
|
|
2022-03-08 12:01:51 +01:00
|
|
|
// function callback
|
2022-03-13 12:29:05 +01:00
|
|
|
callback := rule.onRead(fileName, fileSize, fileContentHash)
|
|
|
|
|
2022-03-12 15:57:27 +01:00
|
|
|
return rule.protocolRepo.ExecuteWriter(writers.SendOkRulePrefix), callback
|
|
|
|
} else {
|
|
|
|
return rule.protocolRepo.ExecuteWriter(writers.SendErrorRulePrefix), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-13 12:29:05 +01:00
|
|
|
func (rule SendFileRule) onRead(fileName string, fileSize int, fingerPrint string) func(reader *bufio.Reader) *protocol.ProtocolWriterResult {
|
2022-03-08 20:37:49 +01:00
|
|
|
|
2022-03-12 15:57:27 +01:00
|
|
|
return func(reader *bufio.Reader) *protocol.ProtocolWriterResult {
|
2022-03-13 12:29:05 +01:00
|
|
|
path := fmt.Sprintf("%s/%s", rule.storagePath, fileName)
|
2022-03-15 20:27:49 +01:00
|
|
|
|
2022-03-15 17:11:31 +01:00
|
|
|
if utils.ReceiveFile(path, fileSize, reader) {
|
|
|
|
hashCompare, err := utils.HashFileCompare(path, fingerPrint)
|
|
|
|
if err == nil && hashCompare {
|
|
|
|
return rule.protocolRepo.ExecuteWriter(writers.SendOkRulePrefix)
|
|
|
|
}
|
2022-03-08 12:01:51 +01:00
|
|
|
}
|
2022-03-15 20:27:49 +01:00
|
|
|
|
2022-03-15 17:11:31 +01:00
|
|
|
os.Remove(path)
|
|
|
|
return rule.protocolRepo.ExecuteWriter(writers.SendErrorRulePrefix)
|
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)
|
|
|
|
}
|