Suppression fichier : fonctionnelle
This commit is contained in:
@@ -1,6 +1,14 @@
|
||||
package readers
|
||||
|
||||
/*
|
||||
import (
|
||||
"StoreBackEnd/pkg/protocol"
|
||||
"StoreBackEnd/pkg/protocol/repository"
|
||||
"StoreBackEnd/pkg/protocol/rules/writers"
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// EraseFileRulePrefix Identifiant de cette règle
|
||||
const EraseFileRulePrefix = "ERASEFILE"
|
||||
|
||||
@@ -8,16 +16,21 @@ const EraseFileRulePrefix = "ERASEFILE"
|
||||
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) protocol.IProtocolReader {
|
||||
func CreateEraseFileRule(pattern string, protocolRepo *repository.ProtocolRepository, storagePath string) protocol.IProtocolReader {
|
||||
return &EraseFileRule{
|
||||
Cmd: EraseFileRulePrefix,
|
||||
matcher: protocol.CreateRegexMatcher(pattern),
|
||||
Cmd: EraseFileRulePrefix,
|
||||
matcher: protocol.CreateRegexMatcher(pattern),
|
||||
protocolRepo: protocolRepo,
|
||||
storagePath: storagePath,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,18 +38,22 @@ func (rule EraseFileRule) GetCmd() string {
|
||||
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) {
|
||||
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 {
|
||||
return "Parsing : EraseFileRule command incorrecte", false, nil
|
||||
return rule.protocolRepo.ExecuteWriter(writers.EraseErrorRulePrefix), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (rule EraseFileRule) Match(data string) bool {
|
||||
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)
|
||||
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
|
||||
return rule.protocolRepo.ExecuteWriter(writers.SendErrorRulePrefix)
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user