This repository has been archived by the owner on Jun 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
186 lines (156 loc) · 5.14 KB
/
handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"fmt"
"image"
"io"
"io/ioutil"
"mime"
"net/http"
"os"
"strconv"
"strings"
"time"
"encoding/json"
"github.com/cvhariharan/Utils/utils"
)
// Guess image format from gif/jpeg/png/webp
func guessImageFormat(r io.Reader) (format string, err error) {
_, format, err = image.DecodeConfig(r)
return
}
// Guess image mime types from gif/jpeg/png/webp
func guessImageMimeTypes(r io.Reader) string {
format, _ := guessImageFormat(r)
if format == "" {
return ""
}
return mime.TypeByExtension("." + format)
}
func getpost(w http.ResponseWriter, r *http.Request) {
var resp interface{}
var resps []interface{}
username := r.Form.Get("username")
r.ParseForm()
w.Header().Set("Content-Type", "application/json")
postids := r.Form.Get("ids")
simplified := r.Form.Get("simplified")
ids := strings.Split(postids, ",")
fmt.Println(ids)
for _, id := range ids {
id = strings.TrimSpace(id)
fmt.Println(id)
if simplified == "true" {
resp = utils.GetPost(id, true, Session)
} else {
resp = utils.GetPost(id, false, Session)
}
resps = append(resps, resp)
}
j, _ := json.Marshal(resps)
jsonString := `{ "result": ` + string(j) + `, "token": "` + utils.GenerateJWT(username, Session) + "\"}"
w.Write([]byte(jsonString))
}
func createPost(w http.ResponseWriter, r *http.Request) {
var jsonString string
var imgloc string
var success string
w.Header().Set("Content-Type", "application/json")
r.ParseMultipartForm(32 << 20)
username := r.FormValue("username")
title := r.FormValue("title")
message := r.FormValue("message")
tags := r.FormValue("hashtags")
travelcapsule := r.FormValue("travelcapsule")
location := r.FormValue("location")
hashtags := strings.Split(tags, ",")
file, _, err := r.FormFile("image")
if err != nil {
fmt.Println("Error Retrieving the File")
fmt.Println(err)
imgloc = ""
} else {
format := guessImageMimeTypes(file)
timestamp := strconv.FormatInt(time.Now().UTC().UnixNano(), 10)
tempFile, err := os.OpenFile("./temp-images/upload-"+timestamp+format, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
fmt.Println(err)
}
defer tempFile.Close()
fileBytes, err := ioutil.ReadAll(file)
if err != nil {
fmt.Println(err)
}
tempFile.Write(fileBytes)
imgloc = tempFile.Name() + format
}
success = utils.CreatePost(travelcapsule, title, message, imgloc, hashtags, username, location, Session)
if success != "" {
jsonString = `{ "result": "successfully uploaded", "token": "` + utils.GenerateJWT(username, Session) + "\", \"travelcapsule\" : \"" + success + "\" }"
} else {
jsonString = `{ "error": "could not create post", "token": "` + utils.GenerateJWT(username, Session) + "\" }"
}
w.Write([]byte(jsonString))
}
func likePost(w http.ResponseWriter, r *http.Request) {
var jsonString string
r.ParseForm()
w.Header().Set("Content-Type", "application/json")
username := r.Form.Get("username")
postId := r.Form.Get("postid")
if utils.LikePost(postId, username, Session) {
jsonString = `{ "result": "liked", "token": "` + utils.GenerateJWT(username, Session) + "\"}"
} else {
jsonString = `{ "error": "could not like", "token": "` + utils.GenerateJWT(username, Session) + "\" }"
}
w.Write([]byte(jsonString))
}
func unlikePost(w http.ResponseWriter, r *http.Request) {
var jsonString string
r.ParseForm()
w.Header().Set("Content-Type", "application/json")
username := r.Form.Get("username")
postId := r.Form.Get("postid")
if utils.UnlikePost(postId, username, Session) {
jsonString = `{ "result": "unliked", "token": "` + utils.GenerateJWT(username, Session) + "\"}"
} else {
jsonString = `{ "error": "could not unlike", "token": "` + utils.GenerateJWT(username, Session) + "\" }"
}
w.Write([]byte(jsonString))
}
func createTC(w http.ResponseWriter, r *http.Request) {
var jsonString string
r.ParseForm()
w.Header().Set("Content-Type", "application/json")
username := r.Form.Get("username")
title := r.Form.Get("title")
success := utils.CreateTC(title, username, Session)
if success != "" {
jsonString = `{ "result": "successfully created", "token": "` + utils.GenerateJWT(username, Session) + "\", \"travelcapsule\" : \"" + success + "\" }"
} else {
jsonString = `{ "error": "could not create tc", "token": "` + utils.GenerateJWT(username, Session) + "\" }"
}
w.Write([]byte(jsonString))
}
func addComment(w http.ResponseWriter, r *http.Request) {
var jsonString string
r.ParseForm()
w.Header().Set("Content-Type", "application/json")
username := r.Form.Get("username")
message := r.Form.Get("message")
postId := r.Form.Get("postid")
if utils.AddComment(postId, username, message, Session) {
jsonString = `{ "result": "comment added successfully", "token": "` + utils.GenerateJWT(username, Session) + "\"}"
} else {
jsonString = `{ "error": "could not add comment", "token": "` + utils.GenerateJWT(username, Session) + "\"}"
}
w.Write([]byte(jsonString))
}
// func getComments(w http.ResponseWriter, r *http.Request) {
// var comments interface{}
// r.ParseForm()
// w.Header().Set("Content-Type", "application/json")
// postId := r.Form.Get("postid")
// comments = utils.GetComments(postId, Session)
// resp, _ := json.Marshal(comments)
// w.Write(resp)
// }