38 lines
1.1 KiB
Go
38 lines
1.1 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/easyai/easyai-ai-gateway/apps/api/internal/config"
|
|
)
|
|
|
|
func (s *Server) serveGeneratedStaticAsset(w http.ResponseWriter, r *http.Request) {
|
|
s.serveLocalStaticAsset(w, r, s.cfg.LocalGeneratedStorageDir, config.DefaultLocalGeneratedStorageDir)
|
|
}
|
|
|
|
func (s *Server) serveUploadedStaticAsset(w http.ResponseWriter, r *http.Request) {
|
|
s.serveLocalStaticAsset(w, r, s.cfg.LocalUploadedStorageDir, config.DefaultLocalUploadedStorageDir)
|
|
}
|
|
|
|
func (s *Server) serveLocalStaticAsset(w http.ResponseWriter, r *http.Request, storageDir string, fallbackStorageDir string) {
|
|
fileName := filepath.Base(strings.TrimSpace(r.PathValue("asset")))
|
|
if fileName == "" || fileName == "." || fileName == ".." || fileName == string(filepath.Separator) {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
storageDir = strings.TrimSpace(storageDir)
|
|
if storageDir == "" {
|
|
storageDir = fallbackStorageDir
|
|
}
|
|
filePath := filepath.Join(storageDir, fileName)
|
|
info, err := os.Stat(filePath)
|
|
if err != nil || info.IsDir() {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
http.ServeFile(w, r, filePath)
|
|
}
|