publish.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package pagebuilder
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http/httptest"
  6. "path"
  7. "path/filepath"
  8. "github.com/qor/oss"
  9. "github.com/qor5/admin/l10n"
  10. "github.com/qor5/admin/publish"
  11. "github.com/qor5/web"
  12. "github.com/sunfmin/reflectutils"
  13. "gorm.io/gorm"
  14. )
  15. func (p *Page) GetPublishActions(db *gorm.DB, ctx context.Context, storage oss.StorageInterface) (objs []*publish.PublishAction, err error) {
  16. var b *Builder
  17. var ok bool
  18. if b, ok = ctx.Value(publish.PublishContextKeyPageBuilder).(*Builder); !ok || b == nil {
  19. return
  20. }
  21. content, err := p.getPublishContent(b, ctx)
  22. if err != nil {
  23. return
  24. }
  25. var localePath string
  26. if l10nBuilder, ok := ctx.Value(publish.PublishContextKeyL10nBuilder).(*l10n.Builder); ok && l10nBuilder != nil && l10nON {
  27. if eventCtx, ok := ctx.Value(publish.PublishContextKeyEventContext).(*web.EventContext); ok && eventCtx != nil {
  28. if locale, ok := l10n.IsLocalizableFromCtx(eventCtx.R.Context()); ok {
  29. localePath = l10nBuilder.GetLocalePath(locale)
  30. }
  31. }
  32. if localeCode, err := reflectutils.Get(p, "LocaleCode"); err == nil {
  33. localePath = l10nBuilder.GetLocalePath(localeCode.(string))
  34. }
  35. }
  36. var category Category
  37. category, err = p.GetCategory(db)
  38. if err != nil {
  39. return
  40. }
  41. objs = append(objs, &publish.PublishAction{
  42. Url: p.getPublishUrl(localePath, category.Path),
  43. Content: content,
  44. IsDelete: false,
  45. })
  46. p.SetOnlineUrl(p.getPublishUrl(localePath, category.Path))
  47. var liveRecord Page
  48. {
  49. lrdb := db.Where("id = ? AND status = ?", p.ID, publish.StatusOnline)
  50. if l10nON {
  51. lrdb = lrdb.Where("locale_code = ?", p.LocaleCode)
  52. }
  53. lrdb.First(&liveRecord)
  54. }
  55. if liveRecord.ID == 0 {
  56. return
  57. }
  58. if liveRecord.GetOnlineUrl() != p.GetOnlineUrl() {
  59. objs = append(objs, &publish.PublishAction{
  60. Url: liveRecord.GetOnlineUrl(),
  61. IsDelete: true,
  62. })
  63. }
  64. return
  65. }
  66. func (p *Page) GetUnPublishActions(db *gorm.DB, ctx context.Context, storage oss.StorageInterface) (objs []*publish.PublishAction, err error) {
  67. objs = append(objs, &publish.PublishAction{
  68. Url: p.GetOnlineUrl(),
  69. IsDelete: true,
  70. })
  71. return
  72. }
  73. func generatePublishUrl(localePath, categoryPath, slug string) string {
  74. return path.Join("/", localePath, categoryPath, slug, "/index.html")
  75. }
  76. func (p *Page) getPublishUrl(localePath, categoryPath string) string {
  77. return generatePublishUrl(localePath, categoryPath, p.Slug)
  78. }
  79. func (p *Page) getAccessUrl(publishUrl string) string {
  80. return filepath.Dir(publishUrl)
  81. }
  82. func (p *Page) getPublishContent(b *Builder, ctx context.Context) (r string, err error) {
  83. w := httptest.NewRecorder()
  84. req := httptest.NewRequest("GET", fmt.Sprintf("/?id=%d&version=%s&locale=%s", p.ID, p.GetVersion(), p.GetLocale()), nil)
  85. b.preview.ServeHTTP(w, req)
  86. r = w.Body.String()
  87. return
  88. }