config.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package views
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "github.com/qor/oss"
  7. "github.com/qor5/admin/activity"
  8. "github.com/qor5/admin/microsite"
  9. "github.com/qor5/admin/microsite/utils"
  10. "github.com/qor5/admin/presets"
  11. "github.com/qor5/admin/publish"
  12. publish_view "github.com/qor5/admin/publish/views"
  13. "github.com/qor5/ui/vuetify"
  14. "github.com/qor5/web"
  15. "github.com/qor5/x/i18n"
  16. h "github.com/theplant/htmlgo"
  17. "golang.org/x/text/language"
  18. "gorm.io/gorm"
  19. )
  20. const I18nMicrositeKey i18n.ModuleKey = "I18nMicrositeKey"
  21. func Configure(b *presets.Builder, db *gorm.DB, ab *activity.ActivityBuilder, storage oss.StorageInterface, publisher *publish.Builder, models ...*presets.ModelBuilder) {
  22. b.I18n().
  23. RegisterForModule(language.English, I18nMicrositeKey, Messages_en_US).
  24. RegisterForModule(language.SimplifiedChinese, I18nMicrositeKey, Messages_zh_CN)
  25. publish_view.Configure(b, db, ab, publisher, models...)
  26. for _, model := range models {
  27. model.Editing().Field("Package").ComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
  28. this := obj.(microsite.MicroSiteInterface)
  29. if this.GetPackage().FileName == "" {
  30. return vuetify.VFileInput().Chips(true).ErrorMessages(field.Errors...).Label(field.Label).FieldName(field.Name).Attr("accept", ".rar,.zip,.7z,.tar").Clearable(false).
  31. On("change", web.Plaid().
  32. FieldValue("PackageChanged", "true").String())
  33. }
  34. return web.Scope(
  35. h.Div(
  36. h.Div(
  37. h.Div(
  38. h.Label(i18n.PT(ctx.R, presets.ModelsI18nModuleKey, model.Info().Label(), "Current Package")).Class("v-label v-label--active theme--light").Style("left: 0px; right: auto; position: absolute;"),
  39. h.A().Href(this.GetPackageUrl(storage.GetEndpoint())).Text(this.GetPackage().FileName),
  40. ).Class("v-text-field__slot").Style("padding: 8px 0;"),
  41. ).Class("v-input__slot"),
  42. ).Class("v-input v-input--is-label-active v-input--is-dirty theme--light v-text-field v-text-field--is-booted"),
  43. vuetify.VFileInput().Chips(true).ErrorMessages(field.Errors...).Label(field.Label).FieldName(field.Name).Attr("accept", ".rar,.zip,.7z,.tar").Clearable(false).
  44. Attr("v-model", "locals.file").On("change", web.Plaid().
  45. FieldValue("PackageChanged", "true").String()),
  46. ).Init(fmt.Sprintf(`{ file: new File([""], "%v", {
  47. lastModified: 0,
  48. }) , change: false}`, this.GetPackage().FileName)).
  49. VSlot("{ locals }")
  50. }).
  51. SetterFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) (err error) {
  52. if ctx.R.FormValue("PackageChanged") != "true" {
  53. return
  54. }
  55. this := obj.(microsite.MicroSiteInterface)
  56. if this.GetUnixKey() == "" {
  57. this.SetUnixKey()
  58. }
  59. fs := ctx.R.MultipartForm.File[field.Name]
  60. if len(fs) == 0 {
  61. if this.GetID() != 0 {
  62. err = db.Where("id = ? AND version_name = ?", this.GetID(), this.GetVersionName()).Select("files_list").Find(&this).Error
  63. if err != nil {
  64. return
  65. }
  66. }
  67. return
  68. }
  69. var fileName = fs[0].Filename
  70. var packagePath = this.GetPackagePath(fileName)
  71. f, err := fs[0].Open()
  72. if err != nil {
  73. return
  74. }
  75. fileBytes, err := io.ReadAll(f)
  76. if err != nil {
  77. return
  78. }
  79. filesList, err := this.UnArchiveAndPublish(this.GetPreviewPath, fileName, bytes.NewReader(fileBytes), storage)
  80. if err != nil {
  81. return
  82. }
  83. err = utils.Upload(storage, packagePath, bytes.NewReader(fileBytes))
  84. if err != nil {
  85. return
  86. }
  87. this.SetFilesList(filesList)
  88. this.SetPackage(fileName, packagePath)
  89. return
  90. })
  91. model.Editing().Field("FilesList").ComponentFunc(
  92. func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) (r h.HTMLComponent) {
  93. this := obj.(microsite.MicroSiteInterface)
  94. if this.GetStatus() == publish.StatusOffline || len(this.GetFileList()) == 0 {
  95. return nil
  96. }
  97. var content []h.HTMLComponent
  98. content = append(content,
  99. h.Label(i18n.PT(ctx.R, presets.ModelsI18nModuleKey, model.Info().Label(), field.Label)).Class("v-label v-label--active theme--light").Style("left: 0px; right: auto; position: absolute;"),
  100. )
  101. if this.GetStatus() == publish.StatusOnline {
  102. for k, v := range this.GetFileList() {
  103. if k != 0 {
  104. content = append(content, h.Br())
  105. }
  106. content = append(content, h.A(h.Text(v)).Href(this.GetPublishedUrl(storage.GetEndpoint(), v)))
  107. }
  108. } else {
  109. for k, v := range this.GetFileList() {
  110. if k != 0 {
  111. content = append(content, h.Br())
  112. }
  113. content = append(content, h.A(h.Text(v)).Href(this.GetPreviewUrl(storage.GetEndpoint(), v)))
  114. }
  115. }
  116. return h.Div(
  117. h.Div(
  118. h.Div(
  119. content...,
  120. ).Class("v-text-field__slot").Style("padding: 8px 0;"),
  121. ).Class("v-input__slot"),
  122. ).Class("v-input v-input--is-label-active v-input--is-dirty theme--light v-text-field v-text-field--is-booted")
  123. },
  124. ).SetterFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) (err error) {
  125. return nil
  126. })
  127. }
  128. return
  129. }