aboutsummaryrefslogtreecommitdiff
path: root/hotline/file_header.go
blob: 61b8e288f4a8d79fb3ee3d42d51b43f9a993c08a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package hotline

import (
	"encoding/binary"
	"slices"
)

type FileHeader struct {
	Size     []byte // Total size of FileHeader payload
	Type     []byte // 0 for file, 1 for dir
	FilePath []byte // encoded file path
}

func NewFileHeader(fileName string, isDir bool) FileHeader {
	fh := FileHeader{
		Size:     make([]byte, 2),
		Type:     []byte{0x00, 0x00},
		FilePath: EncodeFilePath(fileName),
	}
	if isDir {
		fh.Type = []byte{0x00, 0x01}
	}

	encodedPathLen := uint16(len(fh.FilePath) + len(fh.Type))
	binary.BigEndian.PutUint16(fh.Size, encodedPathLen)

	return fh
}

func (fh *FileHeader) Payload() []byte {
	return slices.Concat(
		fh.Size,
		fh.Type,
		fh.FilePath,
	)
}