2020-01-19 06:41:05 +00:00
|
|
|
package vfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2020-08-11 21:56:10 +00:00
|
|
|
"path"
|
2020-01-19 06:41:05 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-08-11 21:56:10 +00:00
|
|
|
// FileContentTyper is an optional interface for vfs.FileInfo
|
|
|
|
type FileContentTyper interface {
|
|
|
|
GetContentType() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// FileInfo implements os.FileInfo for a Cloud Storage file.
|
2020-01-31 18:04:00 +00:00
|
|
|
type FileInfo struct {
|
2020-01-19 06:41:05 +00:00
|
|
|
name string
|
|
|
|
sizeInBytes int64
|
|
|
|
modTime time.Time
|
|
|
|
mode os.FileMode
|
2020-08-11 21:56:10 +00:00
|
|
|
contentType string
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 18:04:00 +00:00
|
|
|
// NewFileInfo creates file info.
|
|
|
|
func NewFileInfo(name string, isDirectory bool, sizeInBytes int64, modTime time.Time) FileInfo {
|
2020-01-19 06:41:05 +00:00
|
|
|
mode := os.FileMode(0644)
|
2020-08-11 21:56:10 +00:00
|
|
|
contentType := ""
|
2020-01-19 06:41:05 +00:00
|
|
|
if isDirectory {
|
|
|
|
mode = os.FileMode(0755) | os.ModeDir
|
2020-08-11 21:56:10 +00:00
|
|
|
contentType = "inode/directory"
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 18:04:00 +00:00
|
|
|
return FileInfo{
|
2020-08-11 21:56:10 +00:00
|
|
|
name: path.Base(name), // we have always Unix style paths here
|
2020-01-19 06:41:05 +00:00
|
|
|
sizeInBytes: sizeInBytes,
|
|
|
|
modTime: modTime,
|
|
|
|
mode: mode,
|
2020-08-11 21:56:10 +00:00
|
|
|
contentType: contentType,
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name provides the base name of the file.
|
2020-01-31 18:04:00 +00:00
|
|
|
func (fi FileInfo) Name() string {
|
2020-01-19 06:41:05 +00:00
|
|
|
return fi.name
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size provides the length in bytes for a file.
|
2020-01-31 18:04:00 +00:00
|
|
|
func (fi FileInfo) Size() int64 {
|
2020-01-19 06:41:05 +00:00
|
|
|
return fi.sizeInBytes
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mode provides the file mode bits
|
2020-01-31 18:04:00 +00:00
|
|
|
func (fi FileInfo) Mode() os.FileMode {
|
2020-01-19 06:41:05 +00:00
|
|
|
return fi.mode
|
|
|
|
}
|
|
|
|
|
|
|
|
// ModTime provides the last modification time.
|
2020-01-31 18:04:00 +00:00
|
|
|
func (fi FileInfo) ModTime() time.Time {
|
2020-01-19 06:41:05 +00:00
|
|
|
return fi.modTime
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsDir provides the abbreviation for Mode().IsDir()
|
2020-01-31 18:04:00 +00:00
|
|
|
func (fi FileInfo) IsDir() bool {
|
2020-01-19 06:41:05 +00:00
|
|
|
return fi.mode&os.ModeDir != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sys provides the underlying data source (can return nil)
|
2020-01-31 18:04:00 +00:00
|
|
|
func (fi FileInfo) Sys() interface{} {
|
2020-01-19 06:41:05 +00:00
|
|
|
return fi.getFileInfoSys()
|
|
|
|
}
|
2020-08-11 21:56:10 +00:00
|
|
|
|
|
|
|
func (fi *FileInfo) setContentType(contenType string) {
|
|
|
|
fi.contentType = contenType
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetContentType implements FileContentTyper interface
|
|
|
|
func (fi FileInfo) GetContentType() string {
|
|
|
|
return fi.contentType
|
|
|
|
}
|