From b198f51c1e00ad85ee7caa8c500d34cf009888c5 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Thu, 10 Mar 2022 14:35:41 +0100 Subject: [PATCH] =?UTF-8?q?R=C3=A9cup=C3=A9ration=20d'un=20fichier=20avec?= =?UTF-8?q?=20BUFFER.=20DOnc=20plus=20optimis=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/utils/FileReceiver.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/pkg/utils/FileReceiver.go b/pkg/utils/FileReceiver.go index 292cd5f..46b299b 100644 --- a/pkg/utils/FileReceiver.go +++ b/pkg/utils/FileReceiver.go @@ -3,7 +3,6 @@ package utils import ( "bufio" "fmt" - "io" "os" ) @@ -14,12 +13,19 @@ func ReceiveFile(fileName string, fileSize int, reader *bufio.Reader) bool { return false } - println(1) defer file.Close() - _, err := io.CopyN(file, reader, int64(fileSize)) - println(2) - if err != nil { - return false + currentSize := 0 + buffer := make([]byte, 1024) + + for currentSize < fileSize { + length, err := reader.Read(buffer) + if err != nil { + return false + } + + currentSize += length + file.WriteAt(buffer, int64(currentSize)) } + return true }