Переглянути джерело

fix: missing file extension

Yeuoly 10 місяців тому
батько
коміт
140602667c

+ 1 - 1
.env.example

@@ -1,5 +1,5 @@
 SERVER_PORT=5002
-SERVER_KEY=lYkiYYT6owG+71oLerGzA7GXCgOT++6ovaezWAjpCjf+Sjc3ZtU+qUEi+vRjI/+XbV1AaFy691iy+kGDv2Jvy0/eAh8Y1
+SERVER_KEY=lYkiYYT6owG+71oLerGzA7GXCgOT++6ovaezWAjpCjf+Sjc3ZtU+qUEi
 GIN_MODE=release
 PLATFORM=aws_lambda
 

+ 1 - 1
internal/core/plugin_manager/basic_manager/remap_assets.go

@@ -22,7 +22,7 @@ func (r *BasicPluginRuntime) RemapAssets(
 			return "", fmt.Errorf("file not found: %s", filename)
 		}
 
-		id, err := r.mediaManager.Upload(file)
+		id, err := r.mediaManager.Upload(filename, file)
 		if err != nil {
 			return "", err
 		}

+ 9 - 3
internal/core/plugin_manager/media_manager/type.go

@@ -5,6 +5,7 @@ import (
 	"encoding/hex"
 	"os"
 	"path"
+	"path/filepath"
 
 	lru "github.com/hashicorp/golang-lru/v2"
 	"github.com/langgenius/dify-plugin-daemon/internal/utils/log"
@@ -29,20 +30,25 @@ func NewMediaManager(storage_path string, cache_size uint16) *MediaManager {
 }
 
 // Upload uploads a file to the media manager and returns an identifier
-func (m *MediaManager) Upload(file []byte) (string, error) {
+func (m *MediaManager) Upload(name string, file []byte) (string, error) {
 	// calculate checksum
 	checksum := sha256.Sum256(append(file, []byte(strings.RandomString(10))...))
 
 	id := hex.EncodeToString(checksum[:])
 
+	// get file extension
+	ext := filepath.Ext(name)
+
+	filename := id + ext
+
 	// store locally
-	filePath := path.Join(m.storagePath, id)
+	filePath := path.Join(m.storagePath, filename)
 	err := os.WriteFile(filePath, file, 0o644)
 	if err != nil {
 		return "", err
 	}
 
-	return id, nil
+	return filename, nil
 }
 
 func (m *MediaManager) Get(id string) ([]byte, error) {

+ 3 - 3
internal/server/http_server.go

@@ -44,7 +44,7 @@ func (app *App) server(config *app.Config) func() {
 }
 
 func (app *App) pluginInvokeGroup(group *gin.RouterGroup, config *app.Config) {
-	group.Use(CheckingKey(config.PluginServerApiKey))
+	group.Use(CheckingKey(config.ServerKey))
 	group.Use(app.RedirectPluginInvoke())
 	group.Use(app.InitClusterID())
 
@@ -62,7 +62,7 @@ func (app *App) pluginInvokeGroup(group *gin.RouterGroup, config *app.Config) {
 
 func (app *App) remoteDebuggingGroup(group *gin.RouterGroup, config *app.Config) {
 	if config.PluginRemoteInstallingEnabled {
-		group.POST("/key", CheckingKey(config.PluginServerApiKey), controllers.GetRemoteDebuggingKey)
+		group.POST("/key", CheckingKey(config.ServerKey), controllers.GetRemoteDebuggingKey)
 	}
 }
 
@@ -98,7 +98,7 @@ func (app *App) endpointManagementGroup(group *gin.RouterGroup) {
 }
 
 func (app *App) pluginGroup(group *gin.RouterGroup, config *app.Config) {
-	group.Use(CheckingKey(config.PluginServerApiKey))
+	group.Use(CheckingKey(config.ServerKey))
 
 	group.GET("/asset/:id", controllers.GetAsset)
 	group.POST("/:tenant_id/install/pkg", controllers.InstallPluginFromPkg(config))

+ 3 - 0
internal/server/middleware.go

@@ -2,6 +2,7 @@ package server
 
 import (
 	"bytes"
+	"fmt"
 	"io"
 
 	"github.com/gin-gonic/gin"
@@ -14,6 +15,8 @@ func CheckingKey(key string) gin.HandlerFunc {
 	return func(c *gin.Context) {
 		// get header X-Api-Key
 		if c.GetHeader(constants.X_API_KEY) != key {
+			fmt.Println(c.GetHeader(constants.X_API_KEY))
+			fmt.Println(key)
 			c.JSON(401, gin.H{"error": "Unauthorized"})
 			c.Abort()
 			return

+ 0 - 2
internal/types/app/config.go

@@ -10,8 +10,6 @@ type Config struct {
 	ServerPort uint16 `envconfig:"SERVER_PORT" validate:"required"`
 	ServerKey  string `envconfig:"SERVER_KEY" validate:"required"`
 
-	PluginServerApiKey string `envconfig:"PLUGIN_SERVER_API_KEY" validate:"required"`
-
 	DifyInnerApiURL string `envconfig:"DIFY_INNER_API_URL" validate:"required"`
 	DifyInnerApiKey string `envconfig:"DIFY_INNER_API_KEY" validate:"required"`