MCPcopy Index your code
hub / github.com/writefreely/writefreely / viewExportPosts

Function viewExportPosts

account.go:603–665  ·  view source on GitHub ↗
(app *App, w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

601}
602
603func viewExportPosts(app *App, w http.ResponseWriter, r *http.Request) ([]byte, string, error) {
604 var filename string
605 var u = &User{}
606 reqJSON := IsJSON(r)
607 if reqJSON {
608 // Use given Authorization header
609 accessToken := r.Header.Get("Authorization")
610 if accessToken == "" {
611 return nil, filename, ErrNoAccessToken
612 }
613
614 userID := app.db.GetUserID(accessToken)
615 if userID == -1 {
616 return nil, filename, ErrBadAccessToken
617 }
618
619 var err error
620 u, err = app.db.GetUserByID(userID)
621 if err != nil {
622 return nil, filename, impart.HTTPError{http.StatusInternalServerError, "Unable to retrieve requested user."}
623 }
624 } else {
625 // Use user cookie
626 session, err := app.sessionStore.Get(r, cookieName)
627 if err != nil {
628 // The cookie should still save, even if there's an error.
629 log.Error("Session: %v; ignoring", err)
630 }
631
632 val := session.Values[cookieUserVal]
633 var ok bool
634 if u, ok = val.(*User); !ok {
635 return nil, filename, ErrNotLoggedIn
636 }
637 }
638
639 filename = u.Username + "-posts-" + time.Now().Truncate(time.Second).UTC().Format("200601021504")
640
641 // Fetch data we're exporting
642 var err error
643 var data []byte
644 posts, err := app.db.GetUserPosts(u)
645 if err != nil {
646 return data, filename, err
647 }
648
649 // Export as CSV
650 if strings.HasSuffix(r.URL.Path, ".csv") {
651 data = exportPostsCSV(app.cfg.App.Host, u, posts)
652 return data, filename, err
653 }
654 if strings.HasSuffix(r.URL.Path, ".zip") {
655 data = exportPostsZip(u, posts)
656 return data, filename, err
657 }
658
659 if r.FormValue("pretty") == "1" {
660 data, err = json.MarshalIndent(posts, "", "\t")

Callers

nothing calls this directly

Calls 7

IsJSONFunction · 0.85
exportPostsCSVFunction · 0.85
exportPostsZipFunction · 0.85
FormatMethod · 0.80
GetUserIDMethod · 0.65
GetUserByIDMethod · 0.65
GetUserPostsMethod · 0.65

Tested by

no test coverage detected