assets.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package media_manager
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  6. )
  7. func (m *MediaBucket) RemapAssets(declaration *plugin_entities.PluginDeclaration, assets map[string][]byte) ([]string, error) {
  8. remappedAssetIds := make(map[string]string)
  9. assetsIds := []string{}
  10. remap := func(filename string) (string, error) {
  11. if id, ok := remappedAssetIds[filename]; ok {
  12. return id, nil
  13. }
  14. file, ok := assets[filename]
  15. if !ok {
  16. return "", fmt.Errorf("file not found: %s", filename)
  17. }
  18. id, err := m.Upload(filename, file)
  19. if err != nil {
  20. return "", err
  21. }
  22. assetsIds = append(assetsIds, id)
  23. remappedAssetIds[filename] = id
  24. return id, nil
  25. }
  26. var err error
  27. if declaration.Model != nil {
  28. if declaration.Model.IconSmall != nil {
  29. if declaration.Model.IconSmall.EnUS != "" {
  30. declaration.Model.IconSmall.EnUS, err = remap(declaration.Model.IconSmall.EnUS)
  31. if err != nil {
  32. return nil, errors.Join(err, fmt.Errorf("failed to remap model icon small en_US"))
  33. }
  34. }
  35. if declaration.Model.IconSmall.ZhHans != "" {
  36. declaration.Model.IconSmall.ZhHans, err = remap(declaration.Model.IconSmall.ZhHans)
  37. if err != nil {
  38. return nil, errors.Join(err, fmt.Errorf("failed to remap model icon small zh_Hans"))
  39. }
  40. }
  41. if declaration.Model.IconSmall.JaJp != "" {
  42. declaration.Model.IconSmall.JaJp, err = remap(declaration.Model.IconSmall.JaJp)
  43. if err != nil {
  44. return nil, errors.Join(err, fmt.Errorf("failed to remap model icon small ja_JP"))
  45. }
  46. }
  47. if declaration.Model.IconSmall.PtBr != "" {
  48. declaration.Model.IconSmall.PtBr, err = remap(declaration.Model.IconSmall.PtBr)
  49. if err != nil {
  50. return nil, errors.Join(err, fmt.Errorf("failed to remap model icon small pt_BR"))
  51. }
  52. }
  53. }
  54. if declaration.Model.IconLarge != nil {
  55. if declaration.Model.IconLarge.EnUS != "" {
  56. declaration.Model.IconLarge.EnUS, err = remap(declaration.Model.IconLarge.EnUS)
  57. if err != nil {
  58. return nil, errors.Join(err, fmt.Errorf("failed to remap model icon large en_US"))
  59. }
  60. }
  61. if declaration.Model.IconLarge.ZhHans != "" {
  62. declaration.Model.IconLarge.ZhHans, err = remap(declaration.Model.IconLarge.ZhHans)
  63. if err != nil {
  64. return nil, errors.Join(err, fmt.Errorf("failed to remap model icon large zh_Hans"))
  65. }
  66. }
  67. if declaration.Model.IconLarge.JaJp != "" {
  68. declaration.Model.IconLarge.JaJp, err = remap(declaration.Model.IconLarge.JaJp)
  69. if err != nil {
  70. return nil, errors.Join(err, fmt.Errorf("failed to remap model icon large ja_JP"))
  71. }
  72. }
  73. if declaration.Model.IconLarge.PtBr != "" {
  74. declaration.Model.IconLarge.PtBr, err = remap(declaration.Model.IconLarge.PtBr)
  75. if err != nil {
  76. return nil, errors.Join(err, fmt.Errorf("failed to remap model icon large pt_BR"))
  77. }
  78. }
  79. }
  80. }
  81. if declaration.Tool != nil {
  82. if declaration.Tool.Identity.Icon != "" {
  83. declaration.Tool.Identity.Icon, err = remap(declaration.Tool.Identity.Icon)
  84. if err != nil {
  85. return nil, errors.Join(err, fmt.Errorf("failed to remap tool icon"))
  86. }
  87. }
  88. }
  89. if declaration.Agent != nil {
  90. if declaration.Agent.Identity.Icon != "" {
  91. declaration.Agent.Identity.Icon, err = remap(declaration.Agent.Identity.Icon)
  92. if err != nil {
  93. return nil, errors.Join(err, fmt.Errorf("failed to remap agent icon"))
  94. }
  95. }
  96. }
  97. if declaration.Icon != "" {
  98. declaration.Icon, err = remap(declaration.Icon)
  99. if err != nil {
  100. return nil, errors.Join(err, fmt.Errorf("failed to remap plugin icon"))
  101. }
  102. }
  103. return assetsIds, nil
  104. }