/
store_provider.go
159 lines (134 loc) · 3.71 KB
/
store_provider.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2023 The casbin Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package object
import (
"fmt"
"strings"
"github.com/casibase/casibase/storage"
)
func (store *Store) createPathIfNotExisted(tokens []string, size int64, url string, lastModifiedTime string, isLeaf bool) {
currentFile := store.FileTree
for i, token := range tokens {
if currentFile.Children == nil {
currentFile.Children = []*File{}
}
if currentFile.ChildrenMap == nil {
currentFile.ChildrenMap = map[string]*File{}
}
tmpFile, ok := currentFile.ChildrenMap[token]
if ok {
currentFile = tmpFile
continue
}
isLeafTmp := false
if i == len(tokens)-1 {
isLeafTmp = isLeaf
}
key := strings.Join(tokens[:i+1], "/")
newFile := &File{
Key: key,
Title: token,
IsLeaf: isLeafTmp,
Url: url,
Children: []*File{},
ChildrenMap: map[string]*File{},
}
if i == len(tokens)-1 {
newFile.Size = size
newFile.CreatedTime = lastModifiedTime
if token == "_hidden.ini" {
continue
}
} else if i == len(tokens)-2 {
if tokens[len(tokens)-1] == "_hidden.ini" {
newFile.CreatedTime = lastModifiedTime
}
}
currentFile.Children = append(currentFile.Children, newFile)
currentFile.ChildrenMap[token] = newFile
currentFile = newFile
}
}
func isObjectLeaf(object *storage.Object) bool {
isLeaf := true
if object.Key[len(object.Key)-1] == '/' {
isLeaf = false
}
return isLeaf
}
func (store *Store) Populate(origin string) error {
storageProviderObj, err := store.GetStorageProviderObj()
if err != nil {
return err
}
objects, err := storageProviderObj.ListObjects("")
if err != nil {
return err
}
if store.FileTree == nil {
store.FileTree = &File{
Key: "/",
Title: store.DisplayName,
CreatedTime: store.CreatedTime,
IsLeaf: false,
Url: "",
Children: []*File{},
ChildrenMap: map[string]*File{},
}
}
sortedObjects := []*storage.Object{}
for _, object := range objects {
if strings.HasSuffix(object.Key, "/_hidden.ini") {
sortedObjects = append(sortedObjects, object)
}
}
for _, object := range objects {
if !strings.HasSuffix(object.Key, "/_hidden.ini") {
sortedObjects = append(sortedObjects, object)
}
}
for _, object := range sortedObjects {
lastModifiedTime := object.LastModified
isLeaf := isObjectLeaf(object)
size := object.Size
var url string
url, err = getUrlFromPath(object.Url, origin)
if err != nil {
return err
}
tokens := strings.Split(strings.Trim(object.Key, "/"), "/")
store.createPathIfNotExisted(tokens, size, url, lastModifiedTime, isLeaf)
// fmt.Printf("%s, %d, %v\n", object.Key, object.Size, object.LastModified)
}
return nil
}
func (store *Store) GetVideoData() ([]string, error) {
storageProviderObj, err := store.GetStorageProviderObj()
if err != nil {
return nil, err
}
objects, err := storageProviderObj.ListObjects("2023/视频附件")
if err != nil {
return nil, err
}
res := []string{}
for _, object := range objects {
if strings.HasSuffix(object.Key, "/_hidden.ini") {
continue
}
url := fmt.Sprintf("%s/%s", store.StorageProvider, object.Key)
res = append(res, url)
}
return res, nil
}