mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
135 lines
3.4 KiB
Go
135 lines
3.4 KiB
Go
// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
|
|
// Portions Copyright 2023 The OpenAgent 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 controllers
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/hanzoai/ai/conf"
|
|
"github.com/hanzoai/ai/util"
|
|
)
|
|
|
|
var (
|
|
cacheDir string
|
|
appDir string
|
|
cacheMap = map[string]string{}
|
|
|
|
// cacheMap is reached from both surfaces — the HTTP handlers here and the ZAP
|
|
// twins — so concurrent callers are the ordinary case. A Go map wants a guard
|
|
// under those conditions, and cacheMu is it. Read through cachedPath and write
|
|
// through rememberPath; nothing else touches the map.
|
|
cacheMu sync.RWMutex
|
|
)
|
|
|
|
// cachedPath answers where a prefix's file was put, if it has been.
|
|
func cachedPath(prefix string) (string, bool) {
|
|
cacheMu.RLock()
|
|
defer cacheMu.RUnlock()
|
|
v, ok := cacheMap[prefix]
|
|
return v, ok
|
|
}
|
|
|
|
// rememberPath records where a prefix's file was put.
|
|
func rememberPath(prefix, path string) {
|
|
cacheMu.Lock()
|
|
defer cacheMu.Unlock()
|
|
cacheMap[prefix] = path
|
|
}
|
|
|
|
func init() {
|
|
cacheDir = conf.GetConfigString("cacheDir")
|
|
appDir = conf.GetConfigString("appDir")
|
|
}
|
|
|
|
func getAppPath(filename string) string {
|
|
return fmt.Sprintf("%s/%s.ctf", appDir, filename)
|
|
}
|
|
|
|
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) error {
|
|
prefix := getCachePrefix(filename)
|
|
if prefix == "" {
|
|
return nil
|
|
}
|
|
|
|
path := fmt.Sprintf("%s/%s/%s", cacheDir, key, filename)
|
|
util.EnsureFileFolderExists(path)
|
|
err := util.WriteBytesToPath(bs, path)
|
|
return err
|
|
}
|
|
|
|
// ActivateFile
|
|
// @Title ActivateFile
|
|
// @Tag File API
|
|
// @Description activate file
|
|
// @Param key query string true "The key of the file"
|
|
// @Param filename query string true "The name of the file"
|
|
// @Success 200 {object} controllers.Response The Response object
|
|
// @router /activate-file [post]
|
|
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.ResponseOk(false)
|
|
return
|
|
}
|
|
|
|
path := fmt.Sprintf("%s/%s", cacheDir, key)
|
|
rememberPath(prefix, path)
|
|
|
|
if !util.FileExist(getAppPath(filename)) {
|
|
util.CopyFile(getAppPath(filename), getAppPath(prefix))
|
|
}
|
|
|
|
c.ResponseOk(true)
|
|
}
|
|
|
|
// GetActiveFile
|
|
// @Title GetActiveFile
|
|
// @Tag File API
|
|
// @Description get active file
|
|
// @Param prefix query string true "The prefix of the file"
|
|
// @Success 200 {string} string "get active file"
|
|
// @router /get-active-file [get]
|
|
func (c *ApiController) GetActiveFile() {
|
|
prefix := c.Input().Get("prefix")
|
|
|
|
res := ""
|
|
if v, ok := cachedPath(prefix); ok {
|
|
res = v
|
|
}
|
|
|
|
c.ResponseOk(res)
|
|
}
|