You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.0 KiB
47 lines
1.0 KiB
3 years ago
|
package sender
|
||
3 years ago
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"os"
|
||
3 years ago
|
|
||
|
"github.com/Unbewohnte/FTU/checksum"
|
||
3 years ago
|
)
|
||
|
|
||
3 years ago
|
// Struct that represents the served file. Used internally in the sender
|
||
3 years ago
|
type File struct {
|
||
|
path string
|
||
|
Filename string
|
||
|
Filesize uint64
|
||
|
SentBytes uint64
|
||
|
LeftBytes uint64
|
||
|
SentPackets uint64
|
||
|
Handler *os.File
|
||
3 years ago
|
CheckSum checksum.CheckSum
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
// Prepares a file for serving. Used for preparing info before sending a fileinfo packet by sender
|
||
3 years ago
|
func getFile(path string) (*File, error) {
|
||
|
info, err := os.Stat(path)
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("could not get a fileinfo: %s", err)
|
||
|
}
|
||
|
handler, err := os.Open(path)
|
||
|
if err != nil {
|
||
3 years ago
|
return nil, fmt.Errorf("wasn`t able to open the file: %s", err)
|
||
|
}
|
||
|
checksum, err := checksum.GetPartialCheckSum(handler)
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("could not get a partial file checksum: %s", err)
|
||
3 years ago
|
}
|
||
3 years ago
|
|
||
3 years ago
|
return &File{
|
||
|
path: path,
|
||
|
Filename: info.Name(),
|
||
|
Filesize: uint64(info.Size()),
|
||
|
SentBytes: 0,
|
||
|
LeftBytes: uint64(info.Size()),
|
||
|
Handler: handler,
|
||
3 years ago
|
CheckSum: checksum,
|
||
3 years ago
|
}, nil
|
||
|
}
|