mirror of
https://github.com/rclone/rclone.git
synced 2025-01-03 12:59:32 +01:00
fs/object: add WithMetadata and WithMimetype to static and memory objects
This commit is contained in:
parent
57bde20acd
commit
458845ce89
@ -12,20 +12,32 @@ import (
|
|||||||
"github.com/rclone/rclone/fs/hash"
|
"github.com/rclone/rclone/fs/hash"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// StaticObjectInfo is an ObjectInfo which can be constructed from scratch
|
||||||
|
type StaticObjectInfo struct {
|
||||||
|
remote string
|
||||||
|
modTime time.Time
|
||||||
|
size int64
|
||||||
|
storable bool
|
||||||
|
hashes map[hash.Type]string
|
||||||
|
fs fs.Info
|
||||||
|
meta fs.Metadata
|
||||||
|
mimeType string
|
||||||
|
}
|
||||||
|
|
||||||
// NewStaticObjectInfo returns a static ObjectInfo
|
// NewStaticObjectInfo returns a static ObjectInfo
|
||||||
// If hashes is nil and fs is not nil, the hash map will be replaced with
|
// If hashes is nil and fs is not nil, the hash map will be replaced with
|
||||||
// empty hashes of the types supported by the fs.
|
// empty hashes of the types supported by the fs.
|
||||||
func NewStaticObjectInfo(remote string, modTime time.Time, size int64, storable bool, hashes map[hash.Type]string, fs fs.Info) fs.ObjectInfo {
|
func NewStaticObjectInfo(remote string, modTime time.Time, size int64, storable bool, hashes map[hash.Type]string, f fs.Info) *StaticObjectInfo {
|
||||||
info := &staticObjectInfo{
|
info := &StaticObjectInfo{
|
||||||
remote: remote,
|
remote: remote,
|
||||||
modTime: modTime,
|
modTime: modTime,
|
||||||
size: size,
|
size: size,
|
||||||
storable: storable,
|
storable: storable,
|
||||||
hashes: hashes,
|
hashes: hashes,
|
||||||
fs: fs,
|
fs: f,
|
||||||
}
|
}
|
||||||
if fs != nil && hashes == nil {
|
if f != nil && hashes == nil {
|
||||||
set := fs.Hashes().Array()
|
set := f.Hashes().Array()
|
||||||
info.hashes = make(map[hash.Type]string)
|
info.hashes = make(map[hash.Type]string)
|
||||||
for _, ht := range set {
|
for _, ht := range set {
|
||||||
info.hashes[ht] = ""
|
info.hashes[ht] = ""
|
||||||
@ -34,22 +46,50 @@ func NewStaticObjectInfo(remote string, modTime time.Time, size int64, storable
|
|||||||
return info
|
return info
|
||||||
}
|
}
|
||||||
|
|
||||||
type staticObjectInfo struct {
|
// WithMetadata adds meta to the ObjectInfo
|
||||||
remote string
|
func (i *StaticObjectInfo) WithMetadata(meta fs.Metadata) *StaticObjectInfo {
|
||||||
modTime time.Time
|
i.meta = meta
|
||||||
size int64
|
return i
|
||||||
storable bool
|
|
||||||
hashes map[hash.Type]string
|
|
||||||
fs fs.Info
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *staticObjectInfo) Fs() fs.Info { return i.fs }
|
// WithMimeType adds meta to the ObjectInfo
|
||||||
func (i *staticObjectInfo) Remote() string { return i.remote }
|
func (i *StaticObjectInfo) WithMimeType(mimeType string) *StaticObjectInfo {
|
||||||
func (i *staticObjectInfo) String() string { return i.remote }
|
i.mimeType = mimeType
|
||||||
func (i *staticObjectInfo) ModTime(ctx context.Context) time.Time { return i.modTime }
|
return i
|
||||||
func (i *staticObjectInfo) Size() int64 { return i.size }
|
}
|
||||||
func (i *staticObjectInfo) Storable() bool { return i.storable }
|
|
||||||
func (i *staticObjectInfo) Hash(ctx context.Context, h hash.Type) (string, error) {
|
// Fs returns read only access to the Fs that this object is part of
|
||||||
|
func (i *StaticObjectInfo) Fs() fs.Info {
|
||||||
|
return i.fs
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remote returns the remote path
|
||||||
|
func (i *StaticObjectInfo) Remote() string {
|
||||||
|
return i.remote
|
||||||
|
}
|
||||||
|
|
||||||
|
// String returns a description of the Object
|
||||||
|
func (i *StaticObjectInfo) String() string {
|
||||||
|
return i.remote
|
||||||
|
}
|
||||||
|
|
||||||
|
// ModTime returns the modification date of the file
|
||||||
|
func (i *StaticObjectInfo) ModTime(ctx context.Context) time.Time {
|
||||||
|
return i.modTime
|
||||||
|
}
|
||||||
|
|
||||||
|
// Size returns the size of the file
|
||||||
|
func (i *StaticObjectInfo) Size() int64 {
|
||||||
|
return i.size
|
||||||
|
}
|
||||||
|
|
||||||
|
// Storable says whether this object can be stored
|
||||||
|
func (i *StaticObjectInfo) Storable() bool {
|
||||||
|
return i.storable
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hash returns the requested hash of the contents
|
||||||
|
func (i *StaticObjectInfo) Hash(ctx context.Context, h hash.Type) (string, error) {
|
||||||
if len(i.hashes) == 0 {
|
if len(i.hashes) == 0 {
|
||||||
return "", hash.ErrUnsupported
|
return "", hash.ErrUnsupported
|
||||||
}
|
}
|
||||||
@ -59,6 +99,24 @@ func (i *staticObjectInfo) Hash(ctx context.Context, h hash.Type) (string, error
|
|||||||
return "", hash.ErrUnsupported
|
return "", hash.ErrUnsupported
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Metadata on the object
|
||||||
|
func (i *StaticObjectInfo) Metadata(ctx context.Context) (fs.Metadata, error) {
|
||||||
|
return i.meta, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MimeType returns the content type of the Object if
|
||||||
|
// known, or "" if not
|
||||||
|
func (i *StaticObjectInfo) MimeType(ctx context.Context) string {
|
||||||
|
return i.mimeType
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check interfaces
|
||||||
|
var (
|
||||||
|
_ fs.ObjectInfo = (*StaticObjectInfo)(nil)
|
||||||
|
_ fs.Metadataer = (*StaticObjectInfo)(nil)
|
||||||
|
_ fs.MimeTyper = (*StaticObjectInfo)(nil)
|
||||||
|
)
|
||||||
|
|
||||||
// MemoryFs is an in memory Fs, it only supports FsInfo and Put
|
// MemoryFs is an in memory Fs, it only supports FsInfo and Put
|
||||||
var MemoryFs memoryFs
|
var MemoryFs memoryFs
|
||||||
|
|
||||||
@ -133,6 +191,7 @@ type MemoryObject struct {
|
|||||||
remote string
|
remote string
|
||||||
modTime time.Time
|
modTime time.Time
|
||||||
content []byte
|
content []byte
|
||||||
|
meta fs.Metadata
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewMemoryObject returns an in memory Object with the modTime and content passed in
|
// NewMemoryObject returns an in memory Object with the modTime and content passed in
|
||||||
@ -144,6 +203,12 @@ func NewMemoryObject(remote string, modTime time.Time, content []byte) *MemoryOb
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithMetadata adds meta to the MemoryObject
|
||||||
|
func (o *MemoryObject) WithMetadata(meta fs.Metadata) *MemoryObject {
|
||||||
|
o.meta = meta
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
// Content returns the underlying buffer
|
// Content returns the underlying buffer
|
||||||
func (o *MemoryObject) Content() []byte {
|
func (o *MemoryObject) Content() []byte {
|
||||||
return o.content
|
return o.content
|
||||||
@ -237,3 +302,14 @@ func (o *MemoryObject) Update(ctx context.Context, in io.Reader, src fs.ObjectIn
|
|||||||
func (o *MemoryObject) Remove(ctx context.Context) error {
|
func (o *MemoryObject) Remove(ctx context.Context) error {
|
||||||
return errors.New("memoryObject.Remove not supported")
|
return errors.New("memoryObject.Remove not supported")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Metadata on the object
|
||||||
|
func (o *MemoryObject) Metadata(ctx context.Context) (fs.Metadata, error) {
|
||||||
|
return o.meta, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check interfaces
|
||||||
|
var (
|
||||||
|
_ fs.Object = (*MemoryObject)(nil)
|
||||||
|
_ fs.Metadataer = (*MemoryObject)(nil)
|
||||||
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user