A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

115 lines
2.4 KiB

  1. package writefreely
  2. import (
  3. "archive/zip"
  4. "bytes"
  5. "encoding/csv"
  6. "github.com/writeas/web-core/log"
  7. "strings"
  8. "time"
  9. )
  10. func exportPostsCSV(u *User, posts *[]PublicPost) []byte {
  11. var b bytes.Buffer
  12. r := [][]string{
  13. {"id", "slug", "blog", "url", "created", "title", "body"},
  14. }
  15. for _, p := range *posts {
  16. var blog string
  17. if p.Collection != nil {
  18. blog = p.Collection.Alias
  19. }
  20. f := []string{p.ID, p.Slug.String, blog, p.CanonicalURL(), p.Created8601(), p.Title.String, strings.Replace(p.Content, "\n", "\\n", -1)}
  21. r = append(r, f)
  22. }
  23. w := csv.NewWriter(&b)
  24. w.WriteAll(r) // calls Flush internally
  25. if err := w.Error(); err != nil {
  26. log.Info("error writing csv:", err)
  27. }
  28. return b.Bytes()
  29. }
  30. type exportedTxt struct {
  31. Name, Body string
  32. Mod time.Time
  33. }
  34. func exportPostsZip(u *User, posts *[]PublicPost) []byte {
  35. // Create a buffer to write our archive to.
  36. b := new(bytes.Buffer)
  37. // Create a new zip archive.
  38. w := zip.NewWriter(b)
  39. // Add some files to the archive.
  40. var filename string
  41. files := []exportedTxt{}
  42. for _, p := range *posts {
  43. filename = ""
  44. if p.Collection != nil {
  45. filename += p.Collection.Alias + "/"
  46. }
  47. if p.Slug.String != "" {
  48. filename += p.Slug.String + "_"
  49. }
  50. filename += p.ID + ".txt"
  51. files = append(files, exportedTxt{filename, p.Content, p.Created})
  52. }
  53. for _, file := range files {
  54. head := &zip.FileHeader{Name: file.Name}
  55. head.SetModTime(file.Mod)
  56. f, err := w.CreateHeader(head)
  57. if err != nil {
  58. log.Error("export zip header: %v", err)
  59. }
  60. _, err = f.Write([]byte(file.Body))
  61. if err != nil {
  62. log.Error("export zip write: %v", err)
  63. }
  64. }
  65. // Make sure to check the error on Close.
  66. err := w.Close()
  67. if err != nil {
  68. log.Error("export zip close: %v", err)
  69. }
  70. return b.Bytes()
  71. }
  72. func compileFullExport(app *app, u *User) *ExportUser {
  73. exportUser := &ExportUser{
  74. User: u,
  75. }
  76. colls, err := app.db.GetCollections(u)
  77. if err != nil {
  78. log.Error("unable to fetch collections: %v", err)
  79. }
  80. posts, err := app.db.GetAnonymousPosts(u)
  81. if err != nil {
  82. log.Error("unable to fetch anon posts: %v", err)
  83. }
  84. exportUser.AnonymousPosts = *posts
  85. var collObjs []CollectionObj
  86. for _, c := range *colls {
  87. co := &CollectionObj{Collection: c}
  88. co.Posts, err = app.db.GetPosts(&c, 0, true)
  89. if err != nil {
  90. log.Error("unable to get collection posts: %v", err)
  91. }
  92. app.db.GetPostsCount(co, true)
  93. collObjs = append(collObjs, *co)
  94. }
  95. exportUser.Collections = &collObjs
  96. return exportUser
  97. }