From 477db13dc1e88d4f72953a79657c2a2ae7587a73 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Sun, 13 Mar 2022 17:04:24 +0100 Subject: [PATCH] =?UTF-8?q?-=20D=C3=A9coupe=20de=20la=20m=C3=A9thode=20Fil?= =?UTF-8?q?eReceiver?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/utils/FileReceiver.go | 23 ++++++++++++++++++++--- resources/AppConfig.json | 2 +- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/pkg/utils/FileReceiver.go b/pkg/utils/FileReceiver.go index a9b77c6..fc4e075 100644 --- a/pkg/utils/FileReceiver.go +++ b/pkg/utils/FileReceiver.go @@ -11,21 +11,30 @@ func ReceiveFile(path string, fileSize int, reader *bufio.Reader) bool { if fileErr != nil { return false } + defer file.Close() // Params buffer := make([]byte, 1024) currentSize := 0 + b, done := copyFile(currentSize, fileSize, reader, buffer, file) + if done { + return b + } + return true +} + +func copyFile(currentSize int, fileSize int, reader *bufio.Reader, buffer []byte, file *os.File) (bool, bool) { // Retrieving file for currentSize < fileSize { length, err := reader.Read(buffer) if err != nil { - return false + return false, true } _, err = file.WriteAt(buffer, int64(currentSize)) if err != nil { - return false + return false, true } currentSize += length check := fileSize - currentSize @@ -33,5 +42,13 @@ func ReceiveFile(path string, fileSize int, reader *bufio.Reader) bool { buffer = make([]byte, check) } } - return true + return false, false +} + +func fileExists(filename string) bool { + info, err := os.Stat(filename) + if os.IsNotExist(err) { + return false + } + return !info.IsDir() } diff --git a/resources/AppConfig.json b/resources/AppConfig.json index e4f2a33..c548bda 100644 --- a/resources/AppConfig.json +++ b/resources/AppConfig.json @@ -4,5 +4,5 @@ "multicastSecond" : 10, "domain" : "lightcontainerSB01", "unicastPort" : 58000, - "storagePath" : "C:\\Users\\ledou\\Documents\\sbe" + "storagePath" : "/home/benjamin/sbe" } \ No newline at end of file