39 lines
744 B
Go
39 lines
744 B
Go
package utils
|
|
|
|
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 {
|
|
file, fileErr := os.Create(fmt.Sprintf("/home/benjamin/sbe/%s", fileName))
|
|
if fileErr != nil {
|
|
return false
|
|
}
|
|
defer file.Close()
|
|
|
|
// Params
|
|
buffer := make([]byte, 1024)
|
|
currentSize := 0
|
|
|
|
// Retrieving file
|
|
for currentSize < fileSize {
|
|
length, err := reader.Read(buffer)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
_, err = file.WriteAt(buffer, int64(currentSize))
|
|
if err != nil {
|
|
return false
|
|
}
|
|
currentSize += length
|
|
check := fileSize - currentSize
|
|
if 1024 > check && check > 0 {
|
|
buffer = make([]byte, check)
|
|
}
|
|
}
|
|
return true
|
|
}
|