diff --git a/GoBlog b/GoBlog old mode 100644 new mode 100755 index 82a088c..8ad31ef Binary files a/GoBlog and b/GoBlog differ diff --git a/create.sh b/create.sh old mode 100644 new mode 100755 diff --git a/goblog.db b/goblog.db new file mode 100644 index 0000000..02d87df Binary files /dev/null and b/goblog.db differ diff --git a/journey-eblog.conf b/journey-eblog.conf new file mode 100644 index 0000000..d6ba707 --- /dev/null +++ b/journey-eblog.conf @@ -0,0 +1,16 @@ +server { +listen 0.0.0.0:80; +server_name eblog1.com; +access_log /var/log/nginx/journey-eblog.log; +location / { +proxy_pass http://127.0.0.1:57847; + } +} +server { +listen 0.0.0.0:80; +server_name www.eblog1.com; +access_log /var/log/nginx/journey-eblog.log; +location / { +proxy_pass http://127.0.0.1:57847; + } +} diff --git a/journey-exampleblog.conf b/journey-exampleblog.conf new file mode 100644 index 0000000..c0e02c1 --- /dev/null +++ b/journey-exampleblog.conf @@ -0,0 +1,16 @@ +server { +listen 0.0.0.0:80; +server_name example1.com; +access_log /var/log/nginx/journey-exampleblog.log; +location / { +proxy_pass http://127.0.0.1:7081; + } +} +server { +listen 0.0.0.0:80; +server_name www.example1.com; +access_log /var/log/nginx/journey-exampleblog.log; +location / { +proxy_pass http://127.0.0.1:7081; + } +} diff --git a/main.go b/main.go index 5419696..bc2eb9a 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,13 @@ import ( "strconv" "time" + //for uploader + "io" + "io/ioutil" + "net/http" + "path" + + "github.com/boltdb/bolt" "github.com/julienschmidt/httprouter" ) @@ -248,6 +255,101 @@ func AdminHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) } } + + +// uploaderHandler expects two fields to be posted, userid and avatarFile. +func uploaderHandler(w http.ResponseWriter, req *http.Request) { + userId := req.FormValue("userid") + file, header, err := req.FormFile("avatarFile") + if err != nil { + io.WriteString(w, err.Error()) + return + } + data, err := ioutil.ReadAll(file) + if err != nil { + io.WriteString(w, err.Error()) + return + } + filename := path.Join("avatars", userId+path.Ext(header.Filename)) + err = ioutil.WriteFile(filename, data, 0777) + if err != nil { + io.WriteString(w, err.Error()) + return + } + io.WriteString(w, "Successful") +} + + +func UploadPage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { + success := r.FormValue("success") + + username := getUser(w, r) + if username != "" { + db, err := bolt.Open("goblog.db", 0600, nil) + if err != nil { + fmt.Println(err) + } + defer db.Close() + + baseT := template.Must(template.New("base").Parse(base)) + baseT = template.Must(baseT.Parse(upload)) + + baseT.ExecuteTemplate(w, "base", map[string]interface{}{ + "PageName": "upload", + "User": username, + "Blogs": getBlogsForUser(db, username), + //[]TODO "Theme": upl + "Success": success, + }) + } else { + http.Redirect(w, r, "/error/You must be authenticated!", http.StatusFound) + } +} + +func UploadHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { + blogname := r.FormValue("blogname") // <- Subdomain + // websiteOriginal := r.FormValue("website") + port := rand.Intn(63000) + 2000 + + // website, err := checkUrl(websiteOriginal) + // if err != nil { + // http.Redirect(w, r, fmt.Sprintf("/error/%s is not a valid url", websiteOriginal), http.StatusFound) + // return + // } + + re := regexp.MustCompile("[^A-Za-z]") + blogname = re.ReplaceAllString(blogname, "") + website := blogname + ".goblog.pw" + + blogcheck := []byte("") + + username := getUser(w, r) + if username != "" { + db, err := bolt.Open("goblog.db", 0600, nil) + if err != nil { + fmt.Println(err) + } + defer db.Close() + db.View(func(tx *bolt.Tx) error { + b := tx.Bucket([]byte("BlogMappingBucket")) + blogcheck = b.Get([]byte(blogname)) + return nil + }) + + if blogcheck != nil { //was == + uploaderHandler() //TODO probably BUG + return + } + } else { + http.Redirect(w, r, "/error/Failure adding theme! Please choose a different name!", http.StatusFound) + return + } + } else { + http.Redirect(w, r, "/error/You must be authenticated!", http.StatusFound) + return + } +} + func addBlogToUser(db *bolt.DB, username string, blogname string, website string) { existingblogs := []byte("") @@ -414,6 +516,8 @@ func main() { router.POST("/login/", LoginHandler) router.GET("/signup/", SignupPage) router.POST("/signup/", SignupHandler) + router.GET("/upload/", UploadPage) + router.POST("/upload/", UploadHandler) router.GET("/admin/", AdminPage) router.POST("/admin/", AdminHandler) router.GET("/logout/", LogoutHandler) diff --git a/pages.go b/pages.go index b7aa261..5ba7c96 100644 --- a/pages.go +++ b/pages.go @@ -238,6 +238,53 @@ var errorPage = ` {{end}} ` + + +var upload = ` +{{define "content"}} +
+ +
+
+ + +Upload a new Theme to your GoBlog + + +
+ +
+ + +
+
+ + +
+ +
+ +
+
+ +
+ +
+ + +
+ + +
+ +
+ + +{{end}} +` + + + var mainPage = ` {{define "content"}} diff --git a/templates/upload.html b/templates/upload.html new file mode 100644 index 0000000..93143d0 --- /dev/null +++ b/templates/upload.html @@ -0,0 +1,21 @@ + + + Upload + + + +
+ +
+ +
+ + +
+ +
+
+ + \ No newline at end of file diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..6ff70a4 --- /dev/null +++ b/test.txt @@ -0,0 +1 @@ +test txttopUsed \ No newline at end of file diff --git a/theme.go b/theme.go new file mode 100644 index 0000000..cc2244d --- /dev/null +++ b/theme.go @@ -0,0 +1,73 @@ +package main +// new-from ch3 pg83 https://www.packtpub.com/application-development/go-programming-blueprints +package main + +import ( + "errors" + "io/ioutil" + "path" +) + +// ErrNoAvatar is the error that is returned when the +// Avatar instance is unable to provide an avatar URL. +var ErrNoAvatarURL = errors.New("chat: Unable to get an avatar URL.") + +// Avatar represents types capable of representing +// user profile pictures. +type Avatar interface { + // GetAvatarURL gets the avatar URL for the specified client, + // or returns an error if something goes wrong. + // ErrNoAvatarURL is returned if the object is unable to get + // a URL for the specified client. + GetAvatarURL(ChatUser) (string, error) +} + +type TryAvatars []Avatar + +func (a TryAvatars) GetAvatarURL(u ChatUser) (string, error) { + for _, avatar := range a { + if url, err := avatar.GetAvatarURL(u); err == nil { + return url, nil + } + } + return "", ErrNoAvatarURL +} + +type FileSystemAvatar struct{} + +var UseFileSystemAvatar FileSystemAvatar + +func (_ FileSystemAvatar) GetAvatarURL(u ChatUser) (string, error) { + if files, err := ioutil.ReadDir("avatars"); err == nil { + for _, file := range files { + if file.IsDir() { + continue + } + if match, _ := path.Match(u.UniqueID()+"*", file.Name()); match { + return "/avatars/" + file.Name(), nil + } + } + } + return "", ErrNoAvatarURL +} + +type AuthAvatar struct{} + +var UseAuthAvatar AuthAvatar + +func (_ AuthAvatar) GetAvatarURL(u ChatUser) (string, error) { + url := u.AvatarURL() + if len(url) > 0 { + return u.AvatarURL(), nil + } + return "", ErrNoAvatarURL +} + +type GravatarAvatar struct{} + +var UseGravatar GravatarAvatar + +func (_ GravatarAvatar) GetAvatarURL(u ChatUser) (string, error) { + return "//www.gravatar.com/avatar/" + u.UniqueID(), nil +} + diff --git a/theme_test.go b/theme_test.go new file mode 100644 index 0000000..1d2e68e --- /dev/null +++ b/theme_test.go @@ -0,0 +1,68 @@ +import ( + "io/ioutil" + "os" + "path" + + gomniauthtest "github.com/stretchr/gomniauth/test" + + "testing" +) + +func TestAuthAvatar(t *testing.T) { + + var authAvatar AuthAvatar + testUser := &gomniauthtest.TestUser{} + testUser.On("AvatarURL").Return("", ErrNoAvatarURL) + testChatUser := &chatUser{User: testUser} + url, err := authAvatar.GetAvatarURL(testChatUser) + if err != ErrNoAvatarURL { + t.Error("AuthAvatar.GetAvatarURL should return ErrNoAvatarURL when no value present") + } + + testUrl := "http://url-to-gravatar/" + testUser = &gomniauthtest.TestUser{} + testChatUser.User = testUser + testUser.On("AvatarURL").Return(testUrl, nil) + url, err = authAvatar.GetAvatarURL(testChatUser) + if err != nil { + t.Error("AuthAvatar.GetAvatarURL should return no error when value present") + } else { + if url != testUrl { + t.Error("AuthAvatar.GetAvatarURL should return correct URL") + } + } +} +func TestGravatarAvatar(t *testing.T) { + + var gravatarAvitar GravatarAvatar + user := &chatUser{uniqueID: "abc"} + + url, err := gravatarAvitar.GetAvatarURL(user) + if err != nil { + t.Error("GravatarAvitar.GetAvatarURL should not return an error") + } + if url != "//www.gravatar.com/avatar/abc" { + t.Errorf("GravatarAvitar.GetAvatarURL wrongly returned %s", url) + } + +} + +func TestFileSystemAvatar(t *testing.T) { + + // make a test avatar file + filename := path.Join("avatars", "abc.jpg") + ioutil.WriteFile(filename, []byte{}, 0777) + defer func() { os.Remove(filename) }() + + var fileSystemAvatar FileSystemAvatar + user := &chatUser{uniqueID: "abc"} + + url, err := fileSystemAvatar.GetAvatarURL(user) + if err != nil { + t.Error("FileSystemAvatar.GetAvatarURL should not return an error") + } + if url != "/avatars/abc.jpg" { + t.Errorf("FileSystemAvatar.GetAvatarURL wrongly returned %s", url) + } + +} diff --git a/upload.go b/upload.go new file mode 100644 index 0000000..e8b43e1 --- /dev/null +++ b/upload.go @@ -0,0 +1,30 @@ +package main +// new-from ch3 pg83 https://www.packtpub.com/application-development/go-programming-blueprints +import ( + "io" + "io/ioutil" + "net/http" + "path" +) + +// uploaderHandler expects two fields to be posted, userid and avatarFile. +func uploaderHandler(w http.ResponseWriter, req *http.Request) { + userId := req.FormValue("userid") + file, header, err := req.FormFile("avatarFile") + if err != nil { + io.WriteString(w, err.Error()) + return + } + data, err := ioutil.ReadAll(file) + if err != nil { + io.WriteString(w, err.Error()) + return + } + filename := path.Join("avatars", userId+path.Ext(header.Filename)) + err = ioutil.WriteFile(filename, data, 0777) + if err != nil { + io.WriteString(w, err.Error()) + return + } + io.WriteString(w, "Successful") +} diff --git a/wiki b/wiki new file mode 100644 index 0000000..7b1d1de Binary files /dev/null and b/wiki differ