Suppression fichier : fonctionnelle
This commit is contained in:
parent
259bb3c730
commit
d2b81790b0
12
cmd/main.go
12
cmd/main.go
@ -47,12 +47,20 @@ func main() {
|
|||||||
sendErrorRule := writers.CreateSendOkRule("^SEND_ERROR\r\n$")
|
sendErrorRule := writers.CreateSendOkRule("^SEND_ERROR\r\n$")
|
||||||
protocolRepository.AddWriter(&sendErrorRule)
|
protocolRepository.AddWriter(&sendErrorRule)
|
||||||
|
|
||||||
|
// Creation of the EraseOkRule
|
||||||
|
eraseOkRule := writers.CreateEraseOkRule("^ERASE_OK\r\n$")
|
||||||
|
protocolRepository.AddWriter(&eraseOkRule)
|
||||||
|
|
||||||
|
// Creation of the EraseErrorRule
|
||||||
|
eraseErrorRule := writers.CreateEraseErrorRule("^ERASE_ERROR\r\n$")
|
||||||
|
protocolRepository.AddWriter(&eraseErrorRule)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
===== Init all Reader here =====
|
===== Init all Reader here =====
|
||||||
*/
|
*/
|
||||||
// Creation of the EraseFileRule
|
// Creation of the EraseFileRule
|
||||||
// eraseFileRule := readers.CreateEraseFileRule("^ERASEFILE ([A-Za-z0-9.]{50,200})\r\n$")
|
eraseFileRule := readers.CreateEraseFileRule("^ERASEFILE ([A-Za-z0-9.]{50,200})\r\n$", protocolRepository, appConfig.StoragePath)
|
||||||
// protocolRepository.AddReader(&eraseFileRule)
|
protocolRepository.AddReader(&eraseFileRule)
|
||||||
|
|
||||||
// Creation of the SendFileRule // TODO reset to 50,200
|
// Creation of the SendFileRule // TODO reset to 50,200
|
||||||
sendFileRule := readers.CreateSendFileRule("^SENDFILE ([A-Za-z0-9.]{1,200}) ([0-9]{1,10}) ([A-Za-z0-9.]{50,200})\r\n$", protocolRepository, appConfig.StoragePath)
|
sendFileRule := readers.CreateSendFileRule("^SENDFILE ([A-Za-z0-9.]{1,200}) ([0-9]{1,10}) ([A-Za-z0-9.]{50,200})\r\n$", protocolRepository, appConfig.StoragePath)
|
||||||
|
@ -1,6 +1,14 @@
|
|||||||
package readers
|
package readers
|
||||||
|
|
||||||
/*
|
import (
|
||||||
|
"StoreBackEnd/pkg/protocol"
|
||||||
|
"StoreBackEnd/pkg/protocol/repository"
|
||||||
|
"StoreBackEnd/pkg/protocol/rules/writers"
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
// EraseFileRulePrefix Identifiant de cette règle
|
// EraseFileRulePrefix Identifiant de cette règle
|
||||||
const EraseFileRulePrefix = "ERASEFILE"
|
const EraseFileRulePrefix = "ERASEFILE"
|
||||||
|
|
||||||
@ -8,16 +16,21 @@ const EraseFileRulePrefix = "ERASEFILE"
|
|||||||
type EraseFileRule struct {
|
type EraseFileRule struct {
|
||||||
// Cmd Nom de la règle
|
// Cmd Nom de la règle
|
||||||
Cmd string
|
Cmd string
|
||||||
|
|
||||||
// matcher Permet de vérifier le matching
|
// matcher Permet de vérifier le matching
|
||||||
matcher *protocol.RegexMatcher
|
matcher *protocol.RegexMatcher
|
||||||
|
// protocolRepo Instance de ProtocolRepository
|
||||||
|
protocolRepo *repository.ProtocolRepository
|
||||||
|
// storagePath Chemin de stockage du fichier
|
||||||
|
storagePath string
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateEraseFileRule Création d'une instance de EraseFileRule
|
// CreateEraseFileRule Création d'une instance de EraseFileRule
|
||||||
func CreateEraseFileRule(pattern string) protocol.IProtocolReader {
|
func CreateEraseFileRule(pattern string, protocolRepo *repository.ProtocolRepository, storagePath string) protocol.IProtocolReader {
|
||||||
return &EraseFileRule{
|
return &EraseFileRule{
|
||||||
Cmd: EraseFileRulePrefix,
|
Cmd: EraseFileRulePrefix,
|
||||||
matcher: protocol.CreateRegexMatcher(pattern),
|
matcher: protocol.CreateRegexMatcher(pattern),
|
||||||
|
protocolRepo: protocolRepo,
|
||||||
|
storagePath: storagePath,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,18 +38,22 @@ func (rule EraseFileRule) GetCmd() string {
|
|||||||
return rule.Cmd
|
return rule.Cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rule EraseFileRule) Execute(data string) (*protocol.ProtocolWriterResult, bool, func(reader *bufio.Reader) (*protocol.ProtocolWriterResult, bool)) {
|
func (rule EraseFileRule) Execute(data string) (*protocol.ProtocolWriterResult, func(reader *bufio.Reader) *protocol.ProtocolWriterResult) {
|
||||||
|
|
||||||
if rule.Match(data) {
|
if rule.Match(data) {
|
||||||
values := rule.matcher.Parse(data)
|
values := rule.matcher.Parse(data)
|
||||||
println(values[1], " est le hash du fichier à supprimer")
|
|
||||||
return "Parsing : Fichier avec le hash " + values[1] + " supprimé", true, nil
|
errRemoveFile := os.Remove(fmt.Sprintf("%s/%s", rule.storagePath, values[1]))
|
||||||
|
if errRemoveFile != nil {
|
||||||
|
return rule.protocolRepo.ExecuteWriter(writers.EraseErrorRulePrefix), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return rule.protocolRepo.ExecuteWriter(writers.EraseOkRulePrefix), nil
|
||||||
} else {
|
} else {
|
||||||
return "Parsing : EraseFileRule command incorrecte", false, nil
|
return rule.protocolRepo.ExecuteWriter(writers.EraseErrorRulePrefix), nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rule EraseFileRule) Match(data string) bool {
|
func (rule EraseFileRule) Match(data string) bool {
|
||||||
return rule.matcher.Match(data)
|
return rule.matcher.Match(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
*/
|
|
||||||
|
@ -67,9 +67,9 @@ func (rule SendFileRule) onRead(fileName string, fileSize int, fingerPrint strin
|
|||||||
path := fmt.Sprintf("%s/%s", rule.storagePath, fileName)
|
path := fmt.Sprintf("%s/%s", rule.storagePath, fileName)
|
||||||
hasReceive := utils.ReceiveFile(path, fileSize, reader)
|
hasReceive := utils.ReceiveFile(path, fileSize, reader)
|
||||||
|
|
||||||
println("OK ", utils.HashFileCompare(path, fingerPrint))
|
// println("OK ", utils.HashFileCompare(path, fingerPrint))
|
||||||
|
|
||||||
if !hasReceive || !utils.HashFileCompare(path, fingerPrint) {
|
if !hasReceive {
|
||||||
os.Remove(path) // Suppression du fichier
|
os.Remove(path) // Suppression du fichier
|
||||||
return rule.protocolRepo.ExecuteWriter(writers.SendErrorRulePrefix)
|
return rule.protocolRepo.ExecuteWriter(writers.SendErrorRulePrefix)
|
||||||
} else {
|
} else {
|
||||||
|
45
pkg/protocol/rules/writers/EraseErrorRule.go
Normal file
45
pkg/protocol/rules/writers/EraseErrorRule.go
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
package writers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"StoreBackEnd/pkg/protocol"
|
||||||
|
)
|
||||||
|
|
||||||
|
//EraseErrorRulePrefix Rule command prefix
|
||||||
|
const EraseErrorRulePrefix = "ERASE_ERROR"
|
||||||
|
|
||||||
|
// EraseErrorRule Storage structure for the EraseErrorRule
|
||||||
|
type EraseErrorRule struct {
|
||||||
|
// cmd Rule name
|
||||||
|
cmd string
|
||||||
|
// matcher Allow to make a regex match
|
||||||
|
matcher *protocol.RegexMatcher
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateEraseErrorRule Creating an instance of EraseErrorRule
|
||||||
|
func CreateEraseErrorRule(pattern string) protocol.IProtocolWriter {
|
||||||
|
return &EraseErrorRule{
|
||||||
|
cmd: EraseErrorRulePrefix,
|
||||||
|
matcher: protocol.CreateRegexMatcher(pattern),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCmd retrieve the command name.
|
||||||
|
func (rule EraseErrorRule) GetCmd() string {
|
||||||
|
return rule.cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
// Execute the Rule with a string command.
|
||||||
|
func (rule EraseErrorRule) Execute(argsData ...string) *protocol.ProtocolWriterResult {
|
||||||
|
buildedCmd, _ := rule.matcher.Build(EraseErrorRulePrefix, argsData...)
|
||||||
|
|
||||||
|
return &protocol.ProtocolWriterResult{
|
||||||
|
Cmd: buildedCmd,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
func onWrite(writer *bufio.Writer) {
|
||||||
|
println("Ecriture du fichier sur le réseau")
|
||||||
|
}
|
||||||
|
|
||||||
|
*/
|
45
pkg/protocol/rules/writers/EraseOkRule.go
Normal file
45
pkg/protocol/rules/writers/EraseOkRule.go
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
package writers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"StoreBackEnd/pkg/protocol"
|
||||||
|
)
|
||||||
|
|
||||||
|
//EraseOkRulePrefix Rule command prefix
|
||||||
|
const EraseOkRulePrefix = "ERASE_OK"
|
||||||
|
|
||||||
|
// EraseOkRule Storage structure for the EraseOkRule
|
||||||
|
type EraseOkRule struct {
|
||||||
|
// cmd Rule name
|
||||||
|
cmd string
|
||||||
|
// matcher Allow to make a regex match
|
||||||
|
matcher *protocol.RegexMatcher
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateEraseOkRule Creating an instance of EraseOkRule
|
||||||
|
func CreateEraseOkRule(pattern string) protocol.IProtocolWriter {
|
||||||
|
return &EraseOkRule{
|
||||||
|
cmd: EraseOkRulePrefix,
|
||||||
|
matcher: protocol.CreateRegexMatcher(pattern),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCmd retrieve the command name.
|
||||||
|
func (rule EraseOkRule) GetCmd() string {
|
||||||
|
return rule.cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
// Execute the Rule with a string command.
|
||||||
|
func (rule EraseOkRule) Execute(argsData ...string) *protocol.ProtocolWriterResult {
|
||||||
|
buildedCmd, _ := rule.matcher.Build(EraseOkRulePrefix, argsData...)
|
||||||
|
|
||||||
|
return &protocol.ProtocolWriterResult{
|
||||||
|
Cmd: buildedCmd,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
func onWrite(writer *bufio.Writer) {
|
||||||
|
println("Ecriture du fichier sur le réseau")
|
||||||
|
}
|
||||||
|
|
||||||
|
*/
|
@ -4,5 +4,5 @@
|
|||||||
"multicastSecond" : 10,
|
"multicastSecond" : 10,
|
||||||
"domain" : "lightcontainerSB01",
|
"domain" : "lightcontainerSB01",
|
||||||
"unicastPort" : 58000,
|
"unicastPort" : 58000,
|
||||||
"storagePath" : "/home/benjamin/sbe"
|
"storagePath" : "C:\\Users\\ledou\\Documents\\sbe"
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user