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.
 
 
 
 
 

125 lines
2.7 KiB

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