Browse Source

Add store.Populate()

HEAD
Yang Luo 3 years ago
parent
commit
dbc73c435a
10 changed files with 107 additions and 21 deletions
  1. +4
    -1
      controllers/store.go
  2. +1
    -0
      main.go
  3. +9
    -5
      object/store.go
  4. +69
    -1
      object/store_provider.go
  5. +3
    -4
      object/store_test.go
  6. +9
    -4
      web/src/FileTree.js
  7. +3
    -3
      web/src/StoreEditPage.js
  8. +7
    -1
      web/src/StoreListPage.js
  9. +1
    -1
      web/src/locales/en/data.json
  10. +1
    -1
      web/src/locales/zh/data.json

+ 4
- 1
controllers/store.go View File

@@ -21,7 +21,10 @@ func (c *ApiController) GetStores() {
func (c *ApiController) GetStore() {
id := c.Input().Get("id")

c.Data["json"] = object.GetStore(id)
store := object.GetStore(id)
store.Populate()

c.Data["json"] = store
c.ServeJSON()
}



+ 1
- 0
main.go View File

@@ -10,6 +10,7 @@ import (

func main() {
object.InitAdapter()
object.InitStore()

beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
AllowOrigins: []string{"*"},


+ 9
- 5
object/store.go View File

@@ -7,10 +7,14 @@ import (
"xorm.io/core"
)

type Folder struct {
Name string `xorm:"varchar(100)" json:"name"`
Desc string `xorm:"mediumtext" json:"desc"`
Children []*Folder `xorm:"varchar(1000)" json:"children"`
type File struct {
Key string `xorm:"varchar(100)" json:"key"`
Title string `xorm:"varchar(100)" json:"title"`
ModifiedTime string `xorm:"varchar(100)" json:"modifiedTime"`
IsLeaf bool `json:"isLeaf"`
Children []*File `xorm:"varchar(1000)" json:"children"`

ChildrenMap map[string]*File `xorm:"-" json:"-"`
}

type Store struct {
@@ -19,7 +23,7 @@ type Store struct {
CreatedTime string `xorm:"varchar(100)" json:"createdTime"`
DisplayName string `xorm:"varchar(100)" json:"displayName"`

Folders []*Folder `xorm:"mediumtext" json:"folders"`
FileTree *File `xorm:"mediumtext" json:"fileTree"`
}

func GetGlobalStores() []*Store {


+ 69
- 1
object/store_provider.go View File

@@ -1,12 +1,80 @@
package object

import (
"strings"
"time"

"github.com/casbin/casbase/util"
"github.com/casdoor/casdoor/storage"
"github.com/casdoor/oss"
)

var storageProvider oss.StorageInterface

func init() {
func InitStore() {
storageProvider = storage.GetStorageProvider(providerType, clientId, clientSecret, region, bucket, endpoint)
}

func (store *Store) createPathIfNotExisted(tokens []string, lastModifiedTime string, lastIsLeaf bool) {
currentFile := store.FileTree
if currentFile == nil {
currentFile = &File{
Title: "root",
ModifiedTime: util.GetCurrentTime(),
IsLeaf: false,
Children: nil,
ChildrenMap: nil,
}
store.FileTree = currentFile
}

for i, token := range tokens {
if currentFile.ChildrenMap == nil {
currentFile.ChildrenMap = map[string]*File{}
}

file, ok := currentFile.ChildrenMap[token]
if ok {
currentFile = file
continue
}

isLeaf := false
if i == len(tokens)-1 {
isLeaf = lastIsLeaf
}

newFile := &File{
Key: strings.Join(tokens, "/"),
Title: token,
IsLeaf: isLeaf,
Children: []*File{},
ChildrenMap: map[string]*File{},
}

if i == len(tokens)-1 {
newFile.ModifiedTime = lastModifiedTime
}

currentFile.Children = append(currentFile.Children, newFile)
currentFile.ChildrenMap[token] = newFile
currentFile = file
}
}

func (store *Store) Populate() {
objects, _ := storageProvider.List("")
for _, object := range objects {
lastModifiedTime := object.LastModified.Local().Format(time.RFC3339)

lastIsLeaf := true
if object.Path[len(object.Path)-1] == '/' {
lastIsLeaf = false
}

tokens := strings.Split(strings.Trim(object.Path, "/"), "/")
store.createPathIfNotExisted(tokens, lastModifiedTime, lastIsLeaf)

//fmt.Printf("%s, %s, %v\n", object.Path, object.Name, object.LastModified)
}
}

+ 3
- 4
object/store_test.go View File

@@ -4,9 +4,8 @@ import "testing"

func TestUpdateStoreFolders(t *testing.T) {
InitConfig()
InitStore()

objects, _ := storageProvider.List("/")
for _, object := range objects {
println(object.Path)
}
store := getStore("admin", "default")
store.Populate()
}

web/src/FolderTree.js → web/src/FileTree.js View File

@@ -1,6 +1,8 @@
import React from "react";
import {Col, Row, Tree} from 'antd';

const { DirectoryTree } = Tree;

const x = 3;
const y = 2;
const z = 1;
@@ -36,7 +38,7 @@ const generateData = (_level, _preKey, _tns) => {

generateData(z);

class FoldTree extends React.Component {
class FileTree extends React.Component {
constructor(props) {
super(props);
this.state = {
@@ -122,14 +124,17 @@ class FoldTree extends React.Component {
};

return (
<Tree
<DirectoryTree
className="draggable-tree"
multiple={true}
defaultExpandAll={true}
defaultExpandedKeys={this.state.expandedKeys}
draggable
blockNode
onDragEnter={onDragEnter}
onDrop={onDrop}
treeData={this.state.gData}
// treeData={this.state.gData}
treeData={tree.children}
/>
);
}
@@ -149,4 +154,4 @@ class FoldTree extends React.Component {
}
}

export default FoldTree;
export default FileTree;

+ 3
- 3
web/src/StoreEditPage.js View File

@@ -3,7 +3,7 @@ import {Button, Card, Col, Input, Row} from 'antd';
import * as StoreBackend from "./backend/StoreBackend";
import * as Setting from "./Setting";
import i18next from "i18next";
import FolderTree from "./FolderTree";
import FileTree from "./FileTree";
class StoreEditPage extends React.Component {
constructor(props) {
@@ -75,10 +75,10 @@ class StoreEditPage extends React.Component {
</Row>
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={(Setting.isMobile()) ? 22 : 2}>
{i18next.t("store:Folders")}:
{i18next.t("store:File tree")}:
</Col>
<Col span={22} >
<FolderTree tree={this.state.store.folders} />
<FileTree tree={this.state.store.fileTree} />
</Col>
</Row>
</Card>


+ 7
- 1
web/src/StoreListPage.js View File

@@ -34,7 +34,13 @@ class StoreListPage extends React.Component {
name: `store_${this.state.stores.length}`,
createdTime: moment().format(),
displayName: `Store ${this.state.stores.length}`,
folders: [],
fileTree: {
key: "/",
title: "",
modifiedTime: moment().format(),
isLeaf: false,
children: [],
},
}
}


+ 1
- 1
web/src/locales/en/data.json View File

@@ -30,7 +30,7 @@
},
"store": {
"Edit Store": "Edit Store",
"Folders": "Folders"
"File tree": "File tree"
},
"vectorset": {
"Count": "Count",


+ 1
- 1
web/src/locales/zh/data.json View File

@@ -30,7 +30,7 @@
},
"store": {
"Edit Store": "编辑数据仓库",
"Folders": "文件夹"
"File tree": "文件"
},
"vectorset": {
"Count": "个数",


Loading…
Cancel
Save