Updated structs in import.go. Added jsonReader function (WIP).

import-zip
Sandrockcstm 6 years ago
parent 5e4d5ba0ee
commit 54d82eb01f
  1. 101
      import.go
  2. 3
      templates/user/import.tmpl

@ -1,16 +1,23 @@
package writefreely package writefreely
import ( import (
"archive/zip" "encoding/json"
"fmt" "fmt"
"io" "io/ioutil"
"log"
"os" "os"
) )
type userInfo struct {
Username string `json:"username"`
HasPass bool `json:"has_pass"`
Email string `json:"email"`
Created string `json:"created"`
Collections []importCollection `json:"collections"`
}
type importCollection struct { type importCollection struct {
Alias string `json: "alias"` Alias string `json:"alias"`
Title string `json: "title"` Title string `json:"title"`
Description string `json:"description"` Description string `json:"description"`
StyleSheet string `json:"style_sheet"` StyleSheet string `json:"style_sheet"`
Public bool `json:"public"` Public bool `json:"public"`
@ -21,7 +28,7 @@ type importCollection struct {
} }
type post struct { type post struct {
Id string `json:"id"` ID string `json:"id"`
Slug string `json:"slug"` Slug string `json:"slug"`
Appearance string `json:"appearance"` Appearance string `json:"appearance"`
Language string `json:"language"` Language string `json:"language"`
@ -34,28 +41,68 @@ type post struct {
Views int `json:"views"` Views int `json:"views"`
} }
func zipreader(src string) ([]string, error) { func jsonReader() {
// Open our jsonFile
// Open a zip archive for reading. jsonFile, err := os.Open("skye-201905250022.json")
r, err := zip.OpenReader("testdata/readme.zip") // if we os.Open returns an error then handle it
if err != nil { if err != nil {
log.Fatal(err) fmt.Println(err)
} }
defer r.Close() fmt.Println("Successfully Opened users.json")
// defer the closing of our jsonFile so that we can parse it later on
defer jsonFile.Close()
// Iterate through the files in the archive, // read our opened xmlFile as a byte array.
// printing some of their contents. byteValue, _ := ioutil.ReadAll(jsonFile)
for _, f := range r.File {
fmt.Printf("Contents of %s:\n", f.Name) // we initialize our collections array
rc, err := f.Open() var u userInfo
if err != nil {
log.Fatal(err) // we unmarshal our byteArray which contains our
} // jsonFile's content into 'users' which we defined above
_, err = io.CopyN(os.Stdout, rc, 68) json.Unmarshal(byteValue, &u)
if err != nil { fmt.Println(u.Collections[0].Posts)
log.Fatal(err)
} return
rc.Close() // for _, p := range u.Collections[0].Posts {
fmt.Println() // fmt.Println(p.ID)
} // }
// we iterate through every user within our users array and
// print out the user Type, their name, and their facebook url
// as just an example
// for i := 0; i < len(users.Users); i++ {
// fmt.Println("User Type: " + users.Users[i].Type)
// fmt.Println("User Age: " + strconv.Itoa(users.Users[i].Age))
// fmt.Println("User Name: " + users.Users[i].Name)
// fmt.Println("Facebook Url: " + users.Users[i].Social.Facebook)
// }
} }
// func zipreader(src string) ([]string, error) {
// // Open a zip archive for reading.
// r, err := zip.OpenReader("testdata/readme.zip")
// if err != nil {
// log.Fatal(err)
// }
// defer r.Close()
// // Iterate through the files in the archive,
// // printing some of their contents.
// for _, f := range r.File {
// fmt.Printf("Contents of %s:\n", f.Name)
// rc, err := f.Open()
// if err != nil {
// log.Fatal(err)
// }
// _, err = io.CopyN(os.Stdout, rc, 68)
// if err != nil {
// log.Fatal(err)
// }
// rc.Close()
// fmt.Println()
// }
// return
// }

@ -3,7 +3,7 @@
<div class="snug content-container"> <div class="snug content-container">
<h2 id="posts-header">Import</h2> <h2 id="posts-header">Import</h2>
<p>You may import your data from another instance at any time here.</p> <p>You may import your data from another instance here.</p>
<table class="classy export"> <table class="classy export">
<tr> <tr>
@ -17,6 +17,7 @@
</tr> </tr>
<tr> <tr>
<th>User + Blogs + Posts</th> <th>User + Blogs + Posts</th>
<!-- TODO: Add inputs like: <td><input type="file" accept="text/JSON">JSON<input/></td> -->
<td><p class="text-cta"><a href="">JSON</a></p></td> <td><p class="text-cta"><a href="">JSON</a></p></td>
<td><p class="text-cta"><a href="">Prettified</a></p></td> <td><p class="text-cta"><a href="">Prettified</a></p></td>
</tr> </tr>

Loading…
Cancel
Save