/
object.go
80 lines (69 loc) · 1.86 KB
/
object.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package storage
import (
"os"
)
const (
undefined int = iota
StorageObjectFolderType //folder type
StorageObjectContentType //file type
)
//Object represents a storage object
type Object interface {
//URL return storage url
URL() string
//Type returns storage type either folder or file
Type() int
//IsFolder returns true if object is a folder
IsFolder() bool
//IsContent returns true if object is a file
IsContent() bool
//Wrap wraps source storage object
Wrap(source interface{})
//Unwrap unwraps source storage object into provided target.
Unwrap(target interface{}) error
FileInfo() os.FileInfo
}
//AbstractObject represents abstract storage object
type AbstractObject struct {
Object
url string
objectType int
Source interface{}
fileInfo os.FileInfo
}
//URL return storage url
func (o *AbstractObject) URL() string {
return o.url
}
//Type returns storage type StorageObjectFolderType or StorageObjectContentType
func (o *AbstractObject) Type() int {
return o.objectType
}
//IsFolder returns true if object is a folder
func (o *AbstractObject) IsFolder() bool {
return o.objectType == StorageObjectFolderType
}
//IsContent returns true if object is a file
func (o *AbstractObject) IsContent() bool {
return o.objectType == StorageObjectContentType
}
//Wrap wraps source storage object
func (o *AbstractObject) Wrap(source interface{}) {
o.Source = source
}
func (o *AbstractObject) FileInfo() os.FileInfo {
return o.fileInfo
}
//NewAbstractStorageObject creates a new abstract storage object
func NewAbstractStorageObject(url string, source interface{}, fileInfo os.FileInfo) *AbstractObject {
var result = &AbstractObject{
url: url,
Source: source,
fileInfo: fileInfo,
objectType: StorageObjectContentType,
}
if fileInfo.IsDir() {
result.objectType = StorageObjectFolderType
}
return result
}