2018-12-24 17:45:15 +00:00
|
|
|
/*
|
2019-05-12 20:55:30 +00:00
|
|
|
* Copyright © 2018-2019 A Bunch Tell LLC.
|
2018-12-24 17:45:15 +00:00
|
|
|
*
|
|
|
|
* This file is part of WriteFreely.
|
|
|
|
*
|
|
|
|
* WriteFreely is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, included
|
|
|
|
* in the LICENSE file in this source code package.
|
|
|
|
*/
|
2018-12-31 06:05:26 +00:00
|
|
|
|
2018-11-08 06:19:03 +00:00
|
|
|
package writefreely
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
|
|
|
"bytes"
|
|
|
|
"encoding/csv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2019-05-20 21:34:11 +00:00
|
|
|
|
|
|
|
"github.com/writeas/web-core/log"
|
2018-11-08 06:19:03 +00:00
|
|
|
)
|
|
|
|
|
2019-11-07 08:24:04 +00:00
|
|
|
func exportPostsCSV(hostName string, u *User, posts *[]PublicPost) []byte {
|
2018-11-08 06:19:03 +00:00
|
|
|
var b bytes.Buffer
|
|
|
|
|
|
|
|
r := [][]string{
|
|
|
|
{"id", "slug", "blog", "url", "created", "title", "body"},
|
|
|
|
}
|
|
|
|
for _, p := range *posts {
|
|
|
|
var blog string
|
|
|
|
if p.Collection != nil {
|
|
|
|
blog = p.Collection.Alias
|
2019-11-07 08:24:04 +00:00
|
|
|
p.Collection.hostName = hostName
|
2018-11-08 06:19:03 +00:00
|
|
|
}
|
2019-11-07 08:24:04 +00:00
|
|
|
f := []string{p.ID, p.Slug.String, blog, p.CanonicalURL(hostName), p.Created8601(), p.Title.String, strings.Replace(p.Content, "\n", "\\n", -1)}
|
2018-11-08 06:19:03 +00:00
|
|
|
r = append(r, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
w := csv.NewWriter(&b)
|
|
|
|
w.WriteAll(r) // calls Flush internally
|
|
|
|
if err := w.Error(); err != nil {
|
2019-05-20 21:34:11 +00:00
|
|
|
log.Info("error writing csv: %v", err)
|
2018-11-08 06:19:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return b.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
type exportedTxt struct {
|
2019-05-30 14:38:55 +00:00
|
|
|
Name, Title, Body string
|
|
|
|
|
|
|
|
Mod time.Time
|
2018-11-08 06:19:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func exportPostsZip(u *User, posts *[]PublicPost) []byte {
|
|
|
|
// Create a buffer to write our archive to.
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
|
|
|
|
// Create a new zip archive.
|
|
|
|
w := zip.NewWriter(b)
|
|
|
|
|
|
|
|
// Add some files to the archive.
|
|
|
|
var filename string
|
|
|
|
files := []exportedTxt{}
|
|
|
|
for _, p := range *posts {
|
|
|
|
filename = ""
|
|
|
|
if p.Collection != nil {
|
|
|
|
filename += p.Collection.Alias + "/"
|
|
|
|
}
|
|
|
|
if p.Slug.String != "" {
|
|
|
|
filename += p.Slug.String + "_"
|
|
|
|
}
|
|
|
|
filename += p.ID + ".txt"
|
2019-05-30 14:38:55 +00:00
|
|
|
files = append(files, exportedTxt{filename, p.Title.String, p.Content, p.Created})
|
2018-11-08 06:19:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
head := &zip.FileHeader{Name: file.Name}
|
|
|
|
head.SetModTime(file.Mod)
|
|
|
|
f, err := w.CreateHeader(head)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("export zip header: %v", err)
|
|
|
|
}
|
2019-05-30 14:38:55 +00:00
|
|
|
var fullPost string
|
|
|
|
if file.Title != "" {
|
|
|
|
fullPost = "# " + file.Title + "\n\n"
|
|
|
|
}
|
|
|
|
fullPost += file.Body
|
|
|
|
_, err = f.Write([]byte(fullPost))
|
2018-11-08 06:19:03 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("export zip write: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure to check the error on Close.
|
|
|
|
err := w.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("export zip close: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.Bytes()
|
|
|
|
}
|
|
|
|
|
2019-05-12 20:55:30 +00:00
|
|
|
func compileFullExport(app *App, u *User) *ExportUser {
|
2018-11-08 06:19:03 +00:00
|
|
|
exportUser := &ExportUser{
|
|
|
|
User: u,
|
|
|
|
}
|
|
|
|
|
2019-08-12 19:35:17 +00:00
|
|
|
colls, err := app.db.GetCollections(u, app.cfg.App.Host)
|
2018-11-08 06:19:03 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("unable to fetch collections: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
posts, err := app.db.GetAnonymousPosts(u)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("unable to fetch anon posts: %v", err)
|
|
|
|
}
|
|
|
|
exportUser.AnonymousPosts = *posts
|
|
|
|
|
|
|
|
var collObjs []CollectionObj
|
|
|
|
for _, c := range *colls {
|
|
|
|
co := &CollectionObj{Collection: c}
|
2019-08-07 13:26:07 +00:00
|
|
|
co.Posts, err = app.db.GetPosts(app.cfg, &c, 0, true, false, true)
|
2018-11-08 06:19:03 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("unable to get collection posts: %v", err)
|
|
|
|
}
|
|
|
|
app.db.GetPostsCount(co, true)
|
|
|
|
collObjs = append(collObjs, *co)
|
|
|
|
}
|
|
|
|
exportUser.Collections = &collObjs
|
|
|
|
|
|
|
|
return exportUser
|
|
|
|
}
|