goldsmith/file.go

188 lines
3.8 KiB
Go
Raw Normal View History

2015-12-18 04:14:39 +00:00
package goldsmith
import (
"bytes"
2024-02-19 18:39:05 +00:00
"fmt"
2015-12-18 04:14:39 +00:00
"io"
"os"
2016-01-13 04:12:50 +00:00
"path/filepath"
2016-06-11 23:24:06 +00:00
"time"
2015-12-18 04:14:39 +00:00
)
2024-02-19 18:39:05 +00:00
type (
Prop any
PropMap map[string]Prop
)
2019-04-07 20:43:25 +00:00
// File represents in-memory or on-disk files in a chain.
2018-12-08 19:18:51 +00:00
type File struct {
relPath string
props map[string]Prop
modTime time.Time
size int64
2015-12-18 07:06:28 +00:00
dataPath string
reader *bytes.Reader
2018-12-08 19:18:51 +00:00
index int
2018-12-08 19:18:51 +00:00
}
2016-06-11 23:24:06 +00:00
2019-07-27 19:17:00 +00:00
// Rename modifies the file path relative to the source directory.
2024-02-19 18:39:05 +00:00
func (self *File) Rename(path string) error {
if filepath.IsAbs(path) {
return fmt.Errorf("unexpected absolute path: %s", path)
}
2024-02-19 18:39:05 +00:00
self.relPath = path
return nil
2019-07-27 19:17:00 +00:00
}
2019-04-07 20:43:25 +00:00
// Path returns the file path relative to the source directory.
func (self *File) Path() string {
2022-10-15 20:12:44 +00:00
return filepath.ToSlash(self.relPath)
2015-12-18 04:14:39 +00:00
}
2019-04-07 20:43:25 +00:00
// Dir returns the containing directory of the file.
func (self *File) Dir() string {
2022-05-16 03:26:09 +00:00
return filepath.ToSlash(filepath.Dir(self.relPath))
2018-12-08 19:18:51 +00:00
}
2024-02-19 18:39:05 +00:00
// Name returns the base name of the file.
func (self *File) Name() string {
return filepath.Base(self.relPath)
}
2019-04-07 20:43:25 +00:00
// Ext returns the extension of the file.
func (self *File) Ext() string {
2022-05-16 00:32:18 +00:00
return filepath.Ext(self.relPath)
2018-12-08 19:18:51 +00:00
}
2019-04-07 20:43:25 +00:00
// Size returns the file length in bytes.
func (self *File) Size() int64 {
return self.size
2018-12-08 19:18:51 +00:00
}
2019-04-07 20:43:25 +00:00
// ModTime returns the time of the file's last modification.
func (self *File) ModTime() time.Time {
return self.modTime
2018-12-08 19:18:51 +00:00
}
2019-04-07 20:43:25 +00:00
// Read reads file data into the provided buffer.
func (self *File) Read(data []byte) (int, error) {
if err := self.load(); err != nil {
2018-12-08 19:18:51 +00:00
return 0, err
}
return self.reader.Read(data)
2018-12-08 19:18:51 +00:00
}
2019-04-07 20:43:25 +00:00
// Write writes file data into the provided writer.
func (self *File) WriteTo(writer io.Writer) (int64, error) {
if err := self.load(); err != nil {
2018-12-08 19:18:51 +00:00
return 0, err
}
return self.reader.WriteTo(writer)
2018-12-08 19:18:51 +00:00
}
2019-04-07 20:43:25 +00:00
// Seek updates the file pointer to the desired position.
func (self *File) Seek(offset int64, whence int) (int64, error) {
2023-11-11 19:04:37 +00:00
if self.reader == nil && offset == 0 && (whence == io.SeekStart || whence == io.SeekCurrent) {
2018-12-08 19:18:51 +00:00
return 0, nil
}
if err := self.load(); err != nil {
2018-12-08 19:18:51 +00:00
return 0, err
}
return self.reader.Seek(offset, whence)
2018-12-08 19:18:51 +00:00
}
2024-02-19 18:39:05 +00:00
// GoString returns value for string formatting.
func (self *File) GoString() string {
return self.relPath
}
2024-02-19 18:39:05 +00:00
// RemoveProp deletes the metadata property for the provided name.
func (self *File) RemoveProp(name string) {
delete(self.props, name)
2020-08-13 04:03:49 +00:00
}
2024-02-19 18:39:05 +00:00
// SetProp updates the metadata property for the provided name.
func (self *File) SetProp(name string, value Prop) {
self.props[name] = value
}
2024-02-19 18:39:05 +00:00
// Prop returns the metadata property for the provided name.
func (self *File) Prop(name string) (Prop, bool) {
value, ok := self.props[name]
return value, ok
}
2024-02-19 18:39:05 +00:00
// PropOrDef returns the metadata property for the provided name or the default.
func (self *File) PropOrDef(name string, valueDef Prop) Prop {
if value, ok := self.Prop(name); ok {
return value
}
return valueDef
}
// Props returns all of the metadata properties.
func (self *File) Props() PropMap {
return self.props
}
2024-02-19 18:39:05 +00:00
// CopyProps copies all metadata properties from the provided file.
func (self *File) CopyProps(file *File) {
for key, value := range file.props {
self.props[key] = value
}
2024-02-19 18:39:05 +00:00
}
2024-02-19 18:39:05 +00:00
func (self *File) load() error {
if self.reader != nil {
return nil
}
data, err := os.ReadFile(self.dataPath)
if err != nil {
return err
}
self.reader = bytes.NewReader(data)
return nil
}
func (self *File) export(targetDir string) error {
targetPath := filepath.Join(targetDir, self.relPath)
if targetInfo, err := os.Stat(targetPath); err == nil && !targetInfo.ModTime().Before(self.ModTime()) {
2021-08-17 03:39:38 +00:00
return nil
}
2022-05-16 00:32:18 +00:00
if err := os.MkdirAll(filepath.Dir(targetPath), 0755); err != nil {
2015-12-18 04:14:39 +00:00
return err
}
2018-12-08 19:18:51 +00:00
fw, err := os.Create(targetPath)
2015-12-18 04:14:39 +00:00
if err != nil {
return err
}
2015-12-18 10:49:52 +00:00
defer fw.Close()
2024-02-19 18:39:05 +00:00
if err := self.load(); err != nil {
return err
2015-12-18 04:14:39 +00:00
}
2024-02-19 18:39:05 +00:00
if _, err := self.Seek(0, io.SeekStart); err != nil {
return err
2015-12-18 04:14:39 +00:00
}
2024-02-19 18:39:05 +00:00
if _, err := self.WriteTo(fw); err != nil {
2015-12-18 04:14:39 +00:00
return err
}
return nil
}