2022-02-20 11:33:47 +01:00
|
|
|
package readers
|
2022-02-19 18:10:52 +01:00
|
|
|
|
2022-03-13 15:49:06 +01:00
|
|
|
import (
|
|
|
|
"StoreBackEnd/pkg/protocol"
|
|
|
|
"StoreBackEnd/pkg/protocol/repository"
|
|
|
|
"StoreBackEnd/pkg/protocol/rules/writers"
|
2022-03-19 16:59:28 +01:00
|
|
|
"StoreBackEnd/pkg/utils"
|
2022-03-13 15:49:06 +01:00
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2022-03-08 10:25:00 +01:00
|
|
|
// EraseFileRulePrefix Identifiant de cette règle
|
|
|
|
const EraseFileRulePrefix = "ERASEFILE"
|
2022-02-22 08:34:45 +01:00
|
|
|
|
2022-02-19 18:10:52 +01:00
|
|
|
// 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
|
2022-03-13 15:49:06 +01:00
|
|
|
// protocolRepo Instance de ProtocolRepository
|
|
|
|
protocolRepo *repository.ProtocolRepository
|
|
|
|
// storagePath Chemin de stockage du fichier
|
|
|
|
storagePath string
|
2022-02-19 18:10:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateEraseFileRule Création d'une instance de EraseFileRule
|
2022-03-13 15:49:06 +01:00
|
|
|
func CreateEraseFileRule(pattern string, protocolRepo *repository.ProtocolRepository, storagePath string) protocol.IProtocolReader {
|
2022-02-19 18:10:52 +01:00
|
|
|
return &EraseFileRule{
|
2022-03-13 15:49:06 +01:00
|
|
|
Cmd: EraseFileRulePrefix,
|
|
|
|
matcher: protocol.CreateRegexMatcher(pattern),
|
|
|
|
protocolRepo: protocolRepo,
|
|
|
|
storagePath: storagePath,
|
2022-02-19 18:10:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 08:47:03 +01:00
|
|
|
func (rule EraseFileRule) GetCmd() string {
|
|
|
|
return rule.Cmd
|
|
|
|
}
|
|
|
|
|
2022-03-13 15:49:06 +01:00
|
|
|
func (rule EraseFileRule) Execute(data string) (*protocol.ProtocolWriterResult, func(reader *bufio.Reader) *protocol.ProtocolWriterResult) {
|
|
|
|
|
2022-02-20 12:35:05 +01:00
|
|
|
if rule.Match(data) {
|
2022-02-22 10:52:07 +01:00
|
|
|
values := rule.matcher.Parse(data)
|
2022-03-13 15:49:06 +01:00
|
|
|
|
2022-03-19 18:16:57 +01:00
|
|
|
hasRemoveFile := utils.DeleteFile(fmt.Sprintf("%s/%s", rule.storagePath, values[1]), 10)
|
2022-03-19 16:59:28 +01:00
|
|
|
if !hasRemoveFile {
|
2022-03-13 15:49:06 +01:00
|
|
|
return rule.protocolRepo.ExecuteWriter(writers.EraseErrorRulePrefix), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return rule.protocolRepo.ExecuteWriter(writers.EraseOkRulePrefix), nil
|
2022-02-19 18:10:52 +01:00
|
|
|
} else {
|
2022-03-13 15:49:06 +01:00
|
|
|
return rule.protocolRepo.ExecuteWriter(writers.EraseErrorRulePrefix), nil
|
2022-02-19 18:10:52 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-20 12:35:05 +01:00
|
|
|
|
|
|
|
func (rule EraseFileRule) Match(data string) bool {
|
|
|
|
return rule.matcher.Match(data)
|
|
|
|
}
|