|
@@ -12,7 +12,7 @@ import (
|
|
|
|
|
|
)
|
|
|
|
|
|
-// Struct for torrent entry in 'New torrents' list
|
|
|
+// Struct for torrent entry in 'New torrents' list
|
|
|
type Entry struct {
|
|
|
Num int
|
|
|
Link string
|
|
@@ -278,81 +278,71 @@ func DownloadTorrentHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
|
func ForumHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
- forumpath := mux.Vars(r)["rest"]
|
|
|
+ forumPath := mux.Vars(r)["rest"]
|
|
|
+
|
|
|
w.Write([]byte("now at forum\n"))
|
|
|
- fmt.Println(len(forumpath))
|
|
|
- if len(forumpath) == 0 {
|
|
|
- // Serve forum root
|
|
|
- w.Write([]byte("forum root\n"))
|
|
|
- rootUuid := "7e92ebbd-750f-4a34-ad62-bc6a4f47cbfa"
|
|
|
- folders, err := dBConnector.TakeChildFolders(rootUuid, 10)
|
|
|
- if err != nil {
|
|
|
- w.Write([]byte("error getting folders from server"))
|
|
|
- fmt.Println("ERROR:", err)
|
|
|
- }
|
|
|
- for _, folder := range folders {
|
|
|
- w.Write([]byte(fmt.Sprintf("<a href=%s>%s</a>\n", (r.URL.String()+"/"+folder.Snake), folder.Title)))
|
|
|
- }
|
|
|
+
|
|
|
+ folder, err := dBConnector.TakeForumFolderByPath(forumPath)
|
|
|
+ if err != nil {
|
|
|
+ // forumErrorTemplate
|
|
|
+ if strings.Contains(err.Error(), "Not found") {
|
|
|
+ w.Write([]byte(err.Error()))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ w.Write([]byte("Internal server error"))
|
|
|
+ fmt.Println(err)
|
|
|
return
|
|
|
}
|
|
|
- if len(forumpath) > 0 {
|
|
|
- // Serve subfolders
|
|
|
- dirs := strings.Split(forumpath,"/")
|
|
|
-
|
|
|
- //var currentUuid string
|
|
|
- var parentUuid string
|
|
|
- parentUuid = "7e92ebbd-750f-4a34-ad62-bc6a4f47cbfa"
|
|
|
-
|
|
|
- var folder Folder
|
|
|
- for _, dir :=range dirs {
|
|
|
- var err error
|
|
|
- folder, err = dBConnector.TakeChildFolderBySnake(parentUuid, dir)
|
|
|
- if err != nil {
|
|
|
- fmt.Println("ERROR:", err)
|
|
|
- return
|
|
|
- }
|
|
|
- if folder == (Folder{}) {
|
|
|
- // Child not found
|
|
|
- w.Write([]byte(fmt.Sprintf("\nNot found %v", dir)))
|
|
|
- return
|
|
|
- }
|
|
|
- parentUuid = folder.Uuid
|
|
|
|
|
|
- w.Write([]byte(fmt.Sprintf("/%v", dir)))
|
|
|
- }
|
|
|
+ w.Write([]byte(fmt.Sprintf("\nType of %v is: %d\n", folder.Title, folder.Type)))
|
|
|
|
|
|
- w.Write([]byte(fmt.Sprintf("\nType of %v is: %d\n", folder.Title, folder.Type)))
|
|
|
+ // tempIndex := strings.LastIndex(r.URL.String(), fmt.Sprintf("/%s",folder.Snake))
|
|
|
+ // if tempIndex != -1 {
|
|
|
+ // //Non root path
|
|
|
+ // backUrl := r.URL.String()[:tempIndex]
|
|
|
+ // _ = backUrl
|
|
|
+ // }
|
|
|
|
|
|
- if folder.Type == 1 {
|
|
|
- // Load subfolders
|
|
|
- folders, err := dBConnector.TakeChildFolders(folder.Uuid, 10)
|
|
|
- if err != nil {
|
|
|
- w.Write([]byte("error getting folders from server"))
|
|
|
- fmt.Println("ERROR:", err)
|
|
|
- }
|
|
|
- for _, folder := range folders {
|
|
|
- w.Write([]byte(fmt.Sprintf("<a href=%s>%s type:%d</a>\n", (r.URL.String()+"/"+folder.Snake), folder.Title, folder.Type)))
|
|
|
- }
|
|
|
- }
|
|
|
+ if folder.Type == 1 {
|
|
|
+ HandleForumFolder(folder, w, r)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if folder.Type == 2 {
|
|
|
+ HandleForumTopic(folder, w, r)
|
|
|
+ return
|
|
|
+ }
|
|
|
|
|
|
- if folder.Type == 2 {
|
|
|
- // Load messages
|
|
|
- messages, err := dBConnector.TakeTopicMessages(folder.Uuid, 0)
|
|
|
- if err != nil {
|
|
|
- w.Write([]byte("error getting messages from server"))
|
|
|
- fmt.Println("ERROR:", err)
|
|
|
- }
|
|
|
+ w.Write([]byte("Internal server error"))
|
|
|
+ fmt.Println("ERROR: Uncrecognized folder type -", folder.Type)
|
|
|
+}
|
|
|
|
|
|
- if len(messages) == 0 {
|
|
|
- // Should be imposible
|
|
|
- w.Write([]byte("No messages in this topic"))
|
|
|
- return
|
|
|
- }
|
|
|
+func HandleForumFolder(folder Folder, w http.ResponseWriter, r *http.Request) {
|
|
|
+ folders, err := dBConnector.TakeChildFolders(folder.Uuid, 10)
|
|
|
+ if err != nil {
|
|
|
+ w.Write([]byte("error getting folders from server"))
|
|
|
+ fmt.Println("ERROR:", err)
|
|
|
+ }
|
|
|
+ for _, folder := range folders {
|
|
|
+ w.Write([]byte(fmt.Sprintf("<a href=%s>%s type:%d</a>\n", (r.URL.String()+"/"+folder.Snake), folder.Title, folder.Type)))
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
|
|
|
- for _, message := range messages {
|
|
|
- w.Write([]byte(fmt.Sprintf("Author:\n%s\n Time:\n %s\n Text:\n %s\n\n", message.Author, message.Time, message.Text)))
|
|
|
- }
|
|
|
- }
|
|
|
+func HandleForumTopic(topic Folder, w http.ResponseWriter, r *http.Request) {
|
|
|
+ messages, err := dBConnector.TakeTopicMessages(topic.Uuid, 0)
|
|
|
+ if err != nil {
|
|
|
+ w.Write([]byte("error getting messages from server"))
|
|
|
+ fmt.Println("ERROR:", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(messages) == 0 {
|
|
|
+ // Should be imposible
|
|
|
+ w.Write([]byte("No messages in this topic"))
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
+ for _, message := range messages {
|
|
|
+ w.Write([]byte(fmt.Sprintf("Author:\n%s\n Time:\n %s\n Text:\n %s\n\n", message.Author, message.Time, message.Text)))
|
|
|
+ }
|
|
|
}
|