-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
186 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
internal/core/plugin_manager/basic_manager/remap_assets.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package basic_manager | ||
|
||
import "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities" | ||
|
||
// RemapAssets will take the assets and remap them to a media id | ||
func (r *BasicPluginRuntime) RemapAssets( | ||
declaration *plugin_entities.PluginDeclaration, | ||
assets map[string][]byte, | ||
) error { | ||
// TODO: implement | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package basic_manager | ||
|
||
import "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/media_manager" | ||
|
||
type BasicPluginRuntime struct { | ||
mediaManager *media_manager.MediaManager | ||
} | ||
|
||
func NewBasicPluginRuntime(mediaManager *media_manager.MediaManager) BasicPluginRuntime { | ||
return BasicPluginRuntime{mediaManager: mediaManager} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package media_manager | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"os" | ||
"path" | ||
|
||
lru "github.com/hashicorp/golang-lru/v2" | ||
"github.com/langgenius/dify-plugin-daemon/internal/utils/log" | ||
"github.com/langgenius/dify-plugin-daemon/internal/utils/strings" | ||
) | ||
|
||
type MediaManager struct { | ||
storagePath string | ||
cache *lru.Cache[string, []byte] | ||
} | ||
|
||
func NewMediaManager(storage_path string, cache_size uint16) *MediaManager { | ||
// mkdir -p storage_path | ||
if err := os.MkdirAll(storage_path, 0o755); err != nil { | ||
log.Error("Failed to create storage path: %s", err) | ||
} | ||
|
||
// lru.New only raises error when cache_size is a negative number, which is impossible | ||
cache, _ := lru.New[string, []byte](int(cache_size)) | ||
|
||
return &MediaManager{storagePath: storage_path, cache: cache} | ||
} | ||
|
||
// Upload uploads a file to the media manager and returns an identifier | ||
func (m *MediaManager) Upload(file []byte) (string, error) { | ||
// calculate checksum | ||
checksum := sha256.Sum256(append(file, []byte(strings.RandomString(10))...)) | ||
|
||
id := hex.EncodeToString(checksum[:]) | ||
|
||
// store locally | ||
filePath := path.Join(m.storagePath, id) | ||
err := os.WriteFile(filePath, file, 0o644) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return id, nil | ||
} | ||
|
||
func (m *MediaManager) Get(id string) ([]byte, error) { | ||
// check if id is in cache | ||
data, ok := m.cache.Get(id) | ||
if ok { | ||
return data, nil | ||
} | ||
|
||
// check if id is in storage | ||
filePath := path.Join(m.storagePath, id) | ||
if _, err := os.Stat(filePath); os.IsNotExist(err) { | ||
return nil, err | ||
} | ||
|
||
// read file | ||
file, err := os.ReadFile(filePath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// store in cache | ||
m.cache.Add(id, file) | ||
|
||
return file, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package plugin_entities | ||
|
||
type RemoteAssetPayload struct { | ||
Filename string `json:"filename" validate:"required"` | ||
Data string `json:"data" validate:"required"` | ||
} |