diff --git a/core/storage/local/local.go b/core/storage/local/local.go index 5c335ddb9..5f6ab943a 100644 --- a/core/storage/local/local.go +++ b/core/storage/local/local.go @@ -66,7 +66,7 @@ func (lfs *localFS) ReadTags(path ...string) (map[string]metadata.Info, error) { if err != nil { return nil, err } - v.FileInfo = localFileInfo{info} + v.FileInfo = newLocalFileInfo(info) res[path] = v } } @@ -77,15 +77,31 @@ func (lfs *localFS) ReadTags(path ...string) (map[string]metadata.Info, error) { // with metadata.FileInfo type localFileInfo struct { fs.FileInfo + ts times.Timespec +} + +// newLocalFileInfo creates a localFileInfo with preloaded time information +func newLocalFileInfo(info fs.FileInfo) localFileInfo { + return localFileInfo{ + FileInfo: info, + ts: times.Get(info), + } } func (lfi localFileInfo) BirthTime() time.Time { - if ts := times.Get(lfi.FileInfo); ts.HasBirthTime() { - return ts.BirthTime() + if lfi.ts.HasBirthTime() { + return lfi.ts.BirthTime() } return time.Now() } +func (lfi localFileInfo) ChangeTime() time.Time { + if lfi.ts.HasChangeTime() { + return lfi.ts.ChangeTime() + } + return lfi.ModTime() +} + func init() { storage.Register(storage.LocalSchemaID, newLocalStorage) } diff --git a/model/metadata/metadata.go b/model/metadata/metadata.go index 471c2434c..2d5e4d8d3 100644 --- a/model/metadata/metadata.go +++ b/model/metadata/metadata.go @@ -83,10 +83,8 @@ type Metadata struct { hasPicture bool } -func (md Metadata) FilePath() string { return md.filePath } -func (md Metadata) ModTime() time.Time { return md.fileInfo.ModTime() } -func (md Metadata) BirthTime() time.Time { return md.fileInfo.BirthTime() } -func (md Metadata) Size() int64 { return md.fileInfo.Size() } +func (md Metadata) FilePath() string { return md.filePath } +func (md Metadata) Size() int64 { return md.fileInfo.Size() } func (md Metadata) Suffix() string { return strings.ToLower(strings.TrimPrefix(path.Ext(md.filePath), ".")) } @@ -100,6 +98,18 @@ func (md Metadata) Int(key model.TagName) int64 { v, _ := strconv.A func (md Metadata) Bool(key model.TagName) bool { v, _ := strconv.ParseBool(md.first(key)); return v } func (md Metadata) Date(key model.TagName) Date { return md.date(key) } func (md Metadata) NumAndTotal(key model.TagName) (int, int) { return md.tuple(key) } + +func (md Metadata) BirthTime() time.Time { return md.fileInfo.BirthTime() } +func (md Metadata) ModTime() time.Time { + type ChangeTimer interface { + ChangeTime() time.Time + } + if ct, ok := md.fileInfo.(ChangeTimer); ok { + return ct.ChangeTime() + } + return md.fileInfo.ModTime() +} + func (md Metadata) Float(key model.TagName, def ...float64) float64 { return float(md.first(key), def...) }