Début vérification de l'empreinte lors de l'envoi d'un fichier
This commit is contained in:
@@ -6,6 +6,8 @@ import (
|
||||
"StoreBackEnd/pkg/protocol/rules/writers"
|
||||
"StoreBackEnd/pkg/utils"
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
@@ -20,14 +22,17 @@ type SendFileRule struct {
|
||||
matcher *protocol.RegexMatcher
|
||||
// protocolRepo Instance de ProtocolRepository
|
||||
protocolRepo *repository.ProtocolRepository
|
||||
// storagePath Chemin de stockage du fichier
|
||||
storagePath string
|
||||
}
|
||||
|
||||
// CreateSendFileRule Creating an instance of SendFileRule
|
||||
func CreateSendFileRule(pattern string, protocolRepo *repository.ProtocolRepository) protocol.IProtocolReader {
|
||||
func CreateSendFileRule(pattern string, protocolRepo *repository.ProtocolRepository, storagePath string) protocol.IProtocolReader {
|
||||
return &SendFileRule{
|
||||
cmd: SendFileRulePrefix,
|
||||
matcher: protocol.CreateRegexMatcher(pattern),
|
||||
protocolRepo: protocolRepo,
|
||||
storagePath: storagePath,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,29 +45,32 @@ func (rule SendFileRule) GetCmd() string {
|
||||
func (rule SendFileRule) Execute(data string) (*protocol.ProtocolWriterResult, func(reader *bufio.Reader) *protocol.ProtocolWriterResult) {
|
||||
println(0)
|
||||
if rule.Match(data) { // TODO : cloture this command.
|
||||
println("OK")
|
||||
values := rule.matcher.Parse(data)
|
||||
|
||||
// Values
|
||||
fileName := values[1]
|
||||
fileSize, _ := strconv.Atoi(values[2])
|
||||
// fileContentHash := values[3]
|
||||
println(1)
|
||||
fileContentHash := values[3]
|
||||
|
||||
// function callback
|
||||
callback := rule.onRead(fileName, fileSize)
|
||||
println(2)
|
||||
callback := rule.onRead(fileName, fileSize, fileContentHash)
|
||||
|
||||
return rule.protocolRepo.ExecuteWriter(writers.SendOkRulePrefix), callback
|
||||
} else {
|
||||
return rule.protocolRepo.ExecuteWriter(writers.SendErrorRulePrefix), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (rule SendFileRule) onRead(fileName string, fileSize int) func(reader *bufio.Reader) *protocol.ProtocolWriterResult {
|
||||
func (rule SendFileRule) onRead(fileName string, fileSize int, fingerPrint string) func(reader *bufio.Reader) *protocol.ProtocolWriterResult {
|
||||
|
||||
return func(reader *bufio.Reader) *protocol.ProtocolWriterResult {
|
||||
hasReceive := utils.ReceiveFile(fileName, fileSize, reader)
|
||||
println(3)
|
||||
if !hasReceive {
|
||||
path := fmt.Sprintf("%s/%s", rule.storagePath, fileName)
|
||||
hasReceive := utils.ReceiveFile(path, fileSize, reader)
|
||||
|
||||
println("OK ", utils.HashFileCompare(path, fingerPrint))
|
||||
|
||||
if !hasReceive || !utils.HashFileCompare(path, fingerPrint) {
|
||||
os.Remove(path) // Suppression du fichier
|
||||
return rule.protocolRepo.ExecuteWriter(writers.SendErrorRulePrefix)
|
||||
} else {
|
||||
return rule.protocolRepo.ExecuteWriter(writers.SendOkRulePrefix)
|
||||
|
||||
Reference in New Issue
Block a user