robotfs/utils/entry.go
2025-05-18 00:21:56 +08:00

76 lines
1.8 KiB
Go

package utils
import (
"fmt"
"time"
"robotfs/pb"
"google.golang.org/protobuf/proto"
)
type Entry struct {
FullPath FullPath
IsDir bool
Size uint64
CreateTime int64
S3Key string
ContentType string
Etag string
VersionID string
LastModificationTime int64
Extended map[string][]byte
}
func MakeEntry(fullPath FullPath, isDirectory bool) *Entry {
return &Entry{
FullPath: fullPath,
IsDir: isDirectory,
CreateTime: time.Now().Unix(),
LastModificationTime: time.Now().Unix(),
}
}
func (entry *Entry) Encode() ([]byte, error) {
message := entry.ToProto()
return proto.Marshal(message)
}
func (entry *Entry) Decode(blob []byte) error {
message := &pb.FileEntry{}
if err := proto.Unmarshal(blob, message); err != nil {
return fmt.Errorf("decoding value blob for %s: %v", entry.FullPath, err)
}
entry.FromProto(message)
return nil
}
func (entry *Entry) ToProto() *pb.FileEntry {
return &pb.FileEntry{
FullPath: string(entry.FullPath),
IsDir: entry.IsDir,
Size: entry.Size,
CreateTime: entry.CreateTime,
S3Key: entry.S3Key,
ContentType: entry.ContentType,
Etag: entry.Etag,
VersionId: entry.VersionID,
LastModificationTime: entry.LastModificationTime,
Extended: entry.Extended,
}
}
func (entry *Entry) FromProto(pb *pb.FileEntry) {
entry.FullPath = FullPath(pb.FullPath)
entry.IsDir = pb.IsDir
entry.Size = pb.Size
entry.CreateTime = pb.CreateTime
entry.S3Key = pb.S3Key
entry.ContentType = pb.ContentType
entry.Etag = pb.Etag
entry.VersionID = pb.VersionId
entry.LastModificationTime = pb.LastModificationTime
entry.Extended = pb.Extended
}