2023-10-13 09:43:27 +00:00
|
|
|
package fstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"io"
|
2023-10-16 08:50:19 +00:00
|
|
|
|
2023-10-13 09:43:27 +00:00
|
|
|
"git.crumpington.com/public/jldb/lib/errs"
|
|
|
|
)
|
|
|
|
|
|
|
|
type command struct {
|
|
|
|
Path string
|
|
|
|
Store bool
|
|
|
|
TempID uint64
|
|
|
|
FileSize int64 // In bytes.
|
|
|
|
File io.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c command) Reader(buf *bytes.Buffer) (int64, io.Reader) {
|
|
|
|
buf.Reset()
|
|
|
|
vars := []any{
|
|
|
|
uint32(len(c.Path)),
|
|
|
|
c.Store,
|
|
|
|
c.TempID,
|
|
|
|
c.FileSize,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range vars {
|
|
|
|
binary.Write(buf, binary.LittleEndian, v)
|
|
|
|
}
|
|
|
|
buf.Write([]byte(c.Path))
|
|
|
|
|
|
|
|
if c.Store {
|
|
|
|
return int64(buf.Len()) + c.FileSize, io.MultiReader(buf, c.File)
|
|
|
|
} else {
|
|
|
|
return int64(buf.Len()), buf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *command) ReadFrom(r io.Reader) error {
|
|
|
|
pathLen := uint32(0)
|
|
|
|
|
|
|
|
ptrs := []any{
|
|
|
|
&pathLen,
|
|
|
|
&c.Store,
|
|
|
|
&c.TempID,
|
|
|
|
&c.FileSize,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ptr := range ptrs {
|
|
|
|
if err := binary.Read(r, binary.LittleEndian, ptr); err != nil {
|
|
|
|
return errs.IO.WithErr(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pathBuf := make([]byte, pathLen)
|
|
|
|
if _, err := r.Read(pathBuf); err != nil {
|
|
|
|
return errs.IO.WithErr(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Path = string(pathBuf)
|
|
|
|
c.File = r
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|