Browse Source

Add activateFile()

HEAD
Yang Luo 3 years ago
parent
commit
c6f0ce6c21
4 changed files with 114 additions and 25 deletions
  1. +54
    -4
      controllers/file_cache.go
  2. +2
    -0
      routers/router.go
  3. +51
    -21
      web/src/FileTree.js
  4. +7
    -0
      web/src/backend/FileBackend.js

+ 54
- 4
controllers/file_cache.go View File

@@ -8,11 +8,61 @@ import (
)

var cacheDir = "C:/casbase_cache"
var cacheMap = map[string]string{}

func getCachePrefix(filename string) string {
if !strings.HasPrefix(filename, "ECG_") && !strings.HasPrefix(filename, "EEG_") && !strings.HasPrefix(filename, "Impedance_") {
return ""
}

tokens := strings.SplitN(filename, "_", 2)
res := tokens[0]
return res
}

func addFileToCache(key string, filename string, bs []byte) {
if strings.HasPrefix(filename, "ECG_") || strings.HasPrefix(filename, "EEG_") || strings.HasPrefix(filename, "Impedance_") {
path := fmt.Sprintf("%s/%s/%s", cacheDir, key, filename)
util.EnsureFileFolderExists(path)
util.WriteBytesToPath(bs, path)
prefix := getCachePrefix(filename)
if prefix == "" {
return
}

path := fmt.Sprintf("%s/%s/%s", cacheDir, key, filename)
util.EnsureFileFolderExists(path)
util.WriteBytesToPath(bs, path)
}

func (c *ApiController) ActivateFile() {
_, ok := c.RequireSignedIn()
if !ok {
return
}

key := c.Input().Get("key")
filename := c.Input().Get("filename")

prefix := getCachePrefix(filename)
if prefix == "" {
c.Data["json"] = false
c.ServeJSON()
return
}

path := fmt.Sprintf("%s/%s", cacheDir, key)
cacheMap[prefix] = path
fmt.Printf("%v\n", cacheMap)

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

func (c *ApiController) GetActiveFile() {
prefix := c.Input().Get("prefix")

res := ""
if v, ok := cacheMap[prefix]; ok {
res = v
}

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

+ 2
- 0
routers/router.go View File

@@ -55,6 +55,8 @@ func initAPI() {
beego.Router("/api/update-file", &controllers.ApiController{}, "POST:UpdateFile")
beego.Router("/api/add-file", &controllers.ApiController{}, "POST:AddFile")
beego.Router("/api/delete-file", &controllers.ApiController{}, "POST:DeleteFile")
beego.Router("/api/activate-file", &controllers.ApiController{}, "POST:ActivateFile")
beego.Router("/api/get-active-file", &controllers.ApiController{}, "GET:GetActiveFile")

beego.Router("/api/get-permissions", &controllers.ApiController{}, "GET:GetPermissions")
beego.Router("/api/get-permission", &controllers.ApiController{}, "GET:GetPermission")


+ 51
- 21
web/src/FileTree.js View File

@@ -11,7 +11,6 @@ import * as PermissionBackend from "./backend/PermissionBackend";
import * as PermissionUtil from "./PermissionUtil";
import * as Conf from "./Conf";
import FileTable from "./FileTable";
import DataChart from "./DataChart";

import {Controlled as CodeMirror} from "react-codemirror2";
import "codemirror/lib/codemirror.css";
@@ -231,6 +230,16 @@ class FileTree extends React.Component {
)
}

getCachePrefix(filename) {
if (!filename.startsWith("ECG_") && !filename.startsWith("EEG_") && !filename.startsWith("Impedance_")) {
return "";
}

const tokens = filename.split("_");
const res = tokens[0];
return res;
}

renderTree(store) {
const onSelect = (selectedKeys, info) => {
if (!this.isFileReadable(info.node)) {
@@ -239,26 +248,47 @@ class FileTree extends React.Component {
}

if (selectedKeys.length !== 0) {
const path = selectedKeys[0];
const ext = Setting.getExtFromPath(path);
if (ext !== "") {
const url = `${store.domain}/${path}`;

if (!this.isExtForDocViewer((ext) && !this.isExtForFileViewer(ext))) {
this.setState({
loading: true,
});

fetch(url, {method: 'GET'})
.then(res => res.text())
.then(res => {
this.setState({
text: res,
loading: false,
const fetchFile = () => {
const path = selectedKeys[0];
const ext = Setting.getExtFromPath(path);
if (ext !== "") {
const url = `${store.domain}/${path}`;

if (!this.isExtForDocViewer((ext) && !this.isExtForFileViewer(ext))) {
this.setState({
loading: true,
});

fetch(url, {method: 'GET'})
.then(res => res.text())
.then(res => {
this.setState({
text: res,
loading: false,
});
});
});
}
}
}

const key = info.node.key;
const filename = info.node.title;
if (this.getCachePrefix(filename) !== "") {
FileBackend.activateFile(key, filename)
.then((res) => {
if (res === true) {
// Setting.showMessage("success", `File activated successfully`);
fetchFile();
} else {
Setting.showMessage("error", `File failed to activate: ${res}`);
}
})
.catch(error => {
Setting.showMessage("error", `File failed to activate: ${error}`);
});
} else {
fetchFile();
}
}

this.setState({
@@ -546,10 +576,10 @@ class FileTree extends React.Component {
const ext = Setting.getExtFromPath(path);
const url = `${store.domain}/${path}`;

if (this.isDataFile(filename)) {
const appName = "ecg_1";
const prefix = this.getCachePrefix(filename);
if (prefix !== "") {
return (
<iframe src={`${Conf.AppUrl}${appName}`} width={"100%"} height={"600px"} />
<iframe src={`${Conf.AppUrl}${prefix}`} width={"100%"} height={"600px"} />
// <DataChart filename={filename} url={url} height={this.getEditorHeightCss()} />
)
} else if (this.isExtForDocViewer(ext)) {


+ 7
- 0
web/src/backend/FileBackend.js View File

@@ -25,3 +25,10 @@ export function deleteFile(storeId, key, isLeaf) {
credentials: 'include',
}).then(res => res.json());
}
export function activateFile(key, filename) {
return fetch(`${Setting.ServerUrl}/api/activate-file?key=${key}&filename=${filename}`, {
method: 'POST',
credentials: 'include',
}).then(res => res.json());
}

Loading…
Cancel
Save