2022-03-08 12:01:51 +01:00
|
|
|
package utils
|
2022-03-08 20:37:49 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ReceiveFile Permet de récupérer un fichier sur un reader
|
|
|
|
func ReceiveFile(fileName string, fileSize int, reader *bufio.Reader) bool {
|
2022-03-13 11:12:40 +01:00
|
|
|
file, fileErr := os.Create(fmt.Sprintf("/home/benjamin/sbe/%s", fileName))
|
2022-03-08 20:37:49 +01:00
|
|
|
if fileErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer file.Close()
|
2022-03-12 19:12:19 +01:00
|
|
|
|
|
|
|
// Params
|
2022-03-10 14:35:41 +01:00
|
|
|
buffer := make([]byte, 1024)
|
2022-03-12 19:12:19 +01:00
|
|
|
currentSize := 0
|
2022-03-10 14:35:41 +01:00
|
|
|
|
2022-03-12 19:12:19 +01:00
|
|
|
// Retrieving file
|
2022-03-10 14:35:41 +01:00
|
|
|
for currentSize < fileSize {
|
|
|
|
length, err := reader.Read(buffer)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2022-03-12 19:12:19 +01:00
|
|
|
_, err = file.WriteAt(buffer, int64(currentSize))
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2022-03-10 14:35:41 +01:00
|
|
|
currentSize += length
|
2022-03-12 19:12:19 +01:00
|
|
|
check := fileSize - currentSize
|
|
|
|
if 1024 > check && check > 0 {
|
|
|
|
buffer = make([]byte, check)
|
|
|
|
}
|
2022-03-08 20:37:49 +01:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|