Browse Source

Add add folder.

HEAD
Yang Luo 3 years ago
parent
commit
b339923361
4 changed files with 61 additions and 16 deletions
  1. +13
    -6
      object/file.go
  2. +16
    -1
      storage/aliyun.go
  3. +26
    -3
      web/src/FileTree.js
  4. +6
    -6
      web/src/backend/FileBackend.js

+ 13
- 6
object/file.go View File

@@ -1,18 +1,24 @@
package object

import "github.com/casbin/casbase/storage"
import (
"fmt"

"github.com/casbin/casbase/storage"
)

func UpdateFile(storeId string, key string, file *File) bool {
return true
}

func AddFile(storeId string, file *File) bool {
affected, err := adapter.engine.Insert(file)
if err != nil {
panic(err)
store := GetStore(storeId)
if store == nil {
return false
}

return affected != 0
objectKey := fmt.Sprintf("%s/_hidden.ini", file.Key)
storage.PutObject(store.Bucket, objectKey)
return true
}

func DeleteFile(storeId string, file *File) bool {
@@ -21,6 +27,7 @@ func DeleteFile(storeId string, file *File) bool {
return false
}

storage.DeleteObject(store.Bucket, file.Key)
objectKey := fmt.Sprintf("%s", file.Key)
storage.DeleteObject(store.Bucket, objectKey)
return true
}

+ 16
- 1
storage/aliyun.go View File

@@ -1,6 +1,10 @@
package storage

import "github.com/aliyun/aliyun-oss-go-sdk/oss"
import (
"bytes"

"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func getBucket(bucketName string) *oss.Bucket {
client, err := oss.New(endpoint, clientId, clientSecret)
@@ -44,6 +48,17 @@ func ListObjects(bucketName string) []oss.ObjectProperties {
return res
}

func PutObject(bucketName string, key string) {
bucket := getBucket(bucketName)

fileBuffer := bytes.NewBuffer(nil)

err := bucket.PutObject(key, fileBuffer)
if err != nil {
panic(err)
}
}

func DeleteObject(bucketName string, key string) {
bucket := getBucket(bucketName)



+ 26
- 3
web/src/FileTree.js View File

@@ -5,10 +5,10 @@ import * as Setting from "./Setting";
import * as FileBackend from "./backend/FileBackend";
import DocViewer, { DocViewerRenderers } from "react-doc-viewer";
import FileViewer from 'react-file-viewer';
import i18next from "i18next";

import {Controlled as CodeMirror} from "react-codemirror2";
import "codemirror/lib/codemirror.css";
import i18next from "i18next";
// require("codemirror/theme/material-darker.css");
// require("codemirror/mode/javascript/javascript");

@@ -77,8 +77,31 @@ class FileTree extends React.Component {
}
}

newFile() {
return {
key: "docs/newfolder",
title: "newfolder",
isLeaf: false,
};
}

addFile() {
const storeId = `${this.props.store.owner}/${this.props.store.name}`;
const file = this.newFile();
FileBackend.addFile(storeId, file)
.then((res) => {
Setting.showMessage("success", `File added successfully`);
window.location.reload();
}
)
.catch(error => {
Setting.showMessage("error", `File failed to add: ${error}`);
});
}

deleteFile(file) {
FileBackend.deleteFile(`${this.props.store.owner}/${this.props.store.name}`, file)
const storeId = `${this.props.store.owner}/${this.props.store.name}`;
FileBackend.deleteFile(storeId, file)
.then((res) => {
Setting.showMessage("success", `File deleted successfully`);
window.location.reload();
@@ -253,7 +276,7 @@ class FileTree extends React.Component {
<div>
<Tooltip title={i18next.t("store:New folder")}>
<Button style={{marginRight: "5px"}} icon={<FolderAddOutlined />} size="small" onClick={(e) => {
Setting.showMessage("error", "New folder");
this.addFile();
e.stopPropagation();
}} />
</Tooltip>


+ 6
- 6
web/src/backend/FileBackend.js View File

@@ -1,26 +1,26 @@
import * as Setting from "../Setting";
export function updateFile(storeName, name, file) {
export function updateFile(storeId, name, file) {
let newFile = Setting.deepCopy(file);
return fetch(`${Setting.ServerUrl}/api/update-file?store=${storeName}&name=${name}`, {
return fetch(`${Setting.ServerUrl}/api/update-file?store=${storeId}&name=${name}`, {
method: 'POST',
credentials: 'include',
body: JSON.stringify(newFile),
}).then(res => res.json());
}
export function addFile(storeName, file) {
export function addFile(storeId, file) {
let newFile = Setting.deepCopy(file);
return fetch(`${Setting.ServerUrl}/api/add-file?store=${storeName}`, {
return fetch(`${Setting.ServerUrl}/api/add-file?store=${storeId}`, {
method: 'POST',
credentials: 'include',
body: JSON.stringify(newFile),
}).then(res => res.json());
}
export function deleteFile(storeName, file) {
export function deleteFile(storeId, file) {
let newFile = Setting.deepCopy(file);
return fetch(`${Setting.ServerUrl}/api/delete-file?store=${storeName}`, {
return fetch(`${Setting.ServerUrl}/api/delete-file?store=${storeId}`, {
method: 'POST',
credentials: 'include',
body: JSON.stringify(newFile),


Loading…
Cancel
Save