]>
Commit | Line | Data |
---|---|---|
1 | package hotline | |
2 | ||
3 | import ( | |
4 | "encoding/binary" | |
5 | "io" | |
6 | "slices" | |
7 | ) | |
8 | ||
9 | type FileHeader struct { | |
10 | Size [2]byte // Total size of FileHeader payload | |
11 | Type [2]byte // 0 for file, 1 for dir | |
12 | FilePath []byte // encoded file path | |
13 | ||
14 | readOffset int // Internal offset to track read progress | |
15 | } | |
16 | ||
17 | func NewFileHeader(fileName string, isDir bool) FileHeader { | |
18 | fh := FileHeader{ | |
19 | FilePath: EncodeFilePath(fileName), | |
20 | } | |
21 | if isDir { | |
22 | fh.Type = [2]byte{0x00, 0x01} | |
23 | } | |
24 | ||
25 | encodedPathLen := uint16(len(fh.FilePath) + len(fh.Type)) | |
26 | binary.BigEndian.PutUint16(fh.Size[:], encodedPathLen) | |
27 | ||
28 | return fh | |
29 | } | |
30 | ||
31 | func (fh *FileHeader) Read(p []byte) (int, error) { | |
32 | buf := slices.Concat( | |
33 | fh.Size[:], | |
34 | fh.Type[:], | |
35 | fh.FilePath, | |
36 | ) | |
37 | ||
38 | if fh.readOffset >= len(buf) { | |
39 | return 0, io.EOF // All bytes have been read | |
40 | } | |
41 | ||
42 | n := copy(p, buf[fh.readOffset:]) | |
43 | fh.readOffset += n | |
44 | ||
45 | return n, nil | |
46 | } |