Réception d'un fichier et sauvegarde de celui-ci terminée

This commit is contained in:
Benjamin
2022-03-08 20:37:49 +01:00
parent a987a7eb64
commit 6fa5f6c1c4
7 changed files with 53 additions and 22 deletions

View File

@@ -2,7 +2,7 @@ package readers
import (
"StoreBackEnd/pkg/protocol"
"net"
"bufio"
)
// EraseFileRulePrefix Identifiant de cette règle
@@ -29,7 +29,7 @@ func (rule EraseFileRule) GetCmd() string {
return rule.Cmd
}
func (rule EraseFileRule) Execute(data string) (string, bool, func(r net.Conn) (string, bool)) {
func (rule EraseFileRule) Execute(data string) (string, bool, func(r *bufio.Reader) (string, bool)) {
if rule.Match(data) {
values := rule.matcher.Parse(data)
println(values[1], " est le hash du fichier à supprimer")

View File

@@ -2,10 +2,9 @@ package readers
import (
"StoreBackEnd/pkg/protocol"
"fmt"
"io"
"net"
"os"
"StoreBackEnd/pkg/utils"
"bufio"
"strconv"
)
// SendFileRulePrefix Rule command prefix
@@ -33,13 +32,13 @@ func (rule SendFileRule) GetCmd() string {
}
// Execute the Rule with a string command.
func (rule SendFileRule) Execute(data string) (string, bool, func(r net.Conn) (string, bool)) {
func (rule SendFileRule) Execute(data string) (string, bool, func(reader *bufio.Reader) (string, bool)) {
if rule.Match(data) { // TODO : cloture this command.
values := rule.matcher.Parse(data)
// Values
fileName := values[1]
// fileSize := values[2]
fileSize, _ := strconv.Atoi(values[2])
// fileContentHash := values[3]
// print received data
@@ -48,13 +47,20 @@ func (rule SendFileRule) Execute(data string) (string, bool, func(r net.Conn) (s
println(values[3], " File Content Hash")
// function callback
callback := func(con net.Conn) (string, bool) {
file, _ := os.Create(fmt.Sprintf("D:\\tmp\\%s", fileName))
_, err := io.Copy(file, con)
callback := func(reader *bufio.Reader) (string, bool) {
hasReceive := utils.ReceiveFile(fileName, fileSize, reader)
println("HEY1")
if err != nil {
println("Can't copy file")
/*
file, _ := os.Create(fmt.Sprintf("/home/benjamin/sbe/%s", fileName))
_, err := io.Copy(file, reader)
println("HEY1")
if err != nil {
println("Can't copy file")
return "SEND_ERROR\r", false
}
*/
if !hasReceive {
return "SEND_ERROR\r", false
}
println("HEY2")