Ajout récupération de fichier (fingerprint à check)
This commit is contained in:
38
pkg/protocol/rules/writers/RetrieveErrorRule.go
Normal file
38
pkg/protocol/rules/writers/RetrieveErrorRule.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package writers
|
||||
|
||||
import (
|
||||
"StoreBackEnd/pkg/protocol"
|
||||
)
|
||||
|
||||
// RetrieveErrorRulePrefix Rule command prefix
|
||||
const RetrieveErrorRulePrefix = "RETRIEVE_ERROR"
|
||||
|
||||
// RetrieveErrorRule Storage structure for the RetrieveErrorRule
|
||||
type RetrieveErrorRule struct {
|
||||
// cmd Rule name
|
||||
cmd string
|
||||
// matcher Allow to make a regex match
|
||||
matcher *protocol.RegexMatcher
|
||||
}
|
||||
|
||||
// CreateRetrieveErrorRule Creating an instance of RetrieveErrorRule
|
||||
func CreateRetrieveErrorRule(pattern string) protocol.IProtocolWriter {
|
||||
return &RetrieveErrorRule{
|
||||
cmd: RetrieveErrorRulePrefix,
|
||||
matcher: protocol.CreateRegexMatcher(pattern),
|
||||
}
|
||||
}
|
||||
|
||||
// GetCmd retrieve the command name.
|
||||
func (rule RetrieveErrorRule) GetCmd() string {
|
||||
return rule.cmd
|
||||
}
|
||||
|
||||
// Execute the Rule with a string command.
|
||||
func (rule RetrieveErrorRule) Execute(argsData ...string) *protocol.ProtocolWriterResult {
|
||||
buildedCmd, _ := rule.matcher.Build(RetrieveErrorRulePrefix, argsData...)
|
||||
|
||||
return &protocol.ProtocolWriterResult{
|
||||
Cmd: buildedCmd,
|
||||
}
|
||||
}
|
||||
58
pkg/protocol/rules/writers/RetrieveOkRule.go
Normal file
58
pkg/protocol/rules/writers/RetrieveOkRule.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package writers
|
||||
|
||||
import (
|
||||
"StoreBackEnd/pkg/protocol"
|
||||
"StoreBackEnd/pkg/utils"
|
||||
"bufio"
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// RetrieveOkRulePrefix Rule command prefix
|
||||
const RetrieveOkRulePrefix = "RETRIEVE_OK"
|
||||
|
||||
// RetrieveOkRule Storage structure for the RetrieveOkRule
|
||||
type RetrieveOkRule struct {
|
||||
// cmd Rule name
|
||||
cmd string
|
||||
// matcher Allow to make a regex match
|
||||
matcher *protocol.RegexMatcher
|
||||
// storagePath Chemin de stockage du fichier
|
||||
storagePath string
|
||||
}
|
||||
|
||||
// CreateRetrieveOkRule Creating an instance of RetrieveOkRule
|
||||
func CreateRetrieveOkRule(pattern string, storagePath string) protocol.IProtocolWriter {
|
||||
return &RetrieveOkRule{
|
||||
cmd: RetrieveOkRulePrefix,
|
||||
matcher: protocol.CreateRegexMatcher(pattern),
|
||||
storagePath: storagePath,
|
||||
}
|
||||
}
|
||||
|
||||
// GetCmd retrieve the command name.
|
||||
func (rule RetrieveOkRule) GetCmd() string {
|
||||
return rule.cmd
|
||||
}
|
||||
|
||||
// Execute the Rule with a string command.
|
||||
func (rule RetrieveOkRule) Execute(argsData ...string) *protocol.ProtocolWriterResult {
|
||||
buildedCmd, _ := rule.matcher.Build(RetrieveOkRulePrefix, argsData...)
|
||||
|
||||
return &protocol.ProtocolWriterResult{
|
||||
Cmd: buildedCmd,
|
||||
Write: func(writer *bufio.Writer) {
|
||||
fileSize, _ := strconv.Atoi(argsData[1])
|
||||
rule.onWrite(writer, argsData[0], fileSize)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (rule RetrieveOkRule) onWrite(writer *bufio.Writer, hashFileName string, fileSize int) {
|
||||
path := fmt.Sprintf("%s/%s", rule.storagePath, hashFileName)
|
||||
if utils.SendFile(path, fileSize, writer) {
|
||||
println("Fichier envoyé au client")
|
||||
} else {
|
||||
println("Fichier non-envoyé au client")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user