package readers import ( "StoreBackEnd/pkg/protocol" "StoreBackEnd/pkg/protocol/repository" "StoreBackEnd/pkg/protocol/rules/writers" "StoreBackEnd/pkg/utils" "bufio" "fmt" ) // EraseFileRulePrefix Identifiant de cette règle const EraseFileRulePrefix = "ERASEFILE" // EraseFileRule Demande de suppression d'un fichier type EraseFileRule struct { // Cmd Nom de la règle Cmd string // matcher Permet de vérifier le matching 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 func CreateEraseFileRule(pattern string, protocolRepo *repository.ProtocolRepository, storagePath string) protocol.IProtocolReader { return &EraseFileRule{ Cmd: EraseFileRulePrefix, matcher: protocol.CreateRegexMatcher(pattern), protocolRepo: protocolRepo, storagePath: storagePath, } } func (rule EraseFileRule) GetCmd() string { return rule.Cmd } func (rule EraseFileRule) Execute(data string) (*protocol.ProtocolWriterResult, func(reader *bufio.Reader) *protocol.ProtocolWriterResult) { if rule.Match(data) { values := rule.matcher.Parse(data) hasRemoveFile := utils.DeleteFile(fmt.Sprintf("%s/%s", rule.storagePath, values[1]), 10) if !hasRemoveFile { return rule.protocolRepo.ExecuteWriter(writers.EraseErrorRulePrefix), nil } return rule.protocolRepo.ExecuteWriter(writers.EraseOkRulePrefix), nil } else { return rule.protocolRepo.ExecuteWriter(writers.EraseErrorRulePrefix), nil } } func (rule EraseFileRule) Match(data string) bool { return rule.matcher.Match(data) }