@@ -3,24 +3,26 @@ package main
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "image/color"
6
7
"io"
7
8
"log"
8
- "image/color"
9
9
"net/http"
10
10
"os"
11
11
"strings"
12
12
"sync"
13
13
"time"
14
- "github.com/disintegration/imaging"
14
+
15
15
"github.com/dgrijalva/jwt-go"
16
+ "github.com/disintegration/imaging"
16
17
"github.com/gin-gonic/gin"
17
18
"go.mongodb.org/mongo-driver/bson"
18
19
"go.mongodb.org/mongo-driver/mongo"
19
20
"go.mongodb.org/mongo-driver/mongo/options"
20
21
)
22
+
21
23
type User struct {
22
- Email string `json:"email"`
23
- Password string `json:"password"`
24
+ Email string `json:"email"`
25
+ Password string `json:"password"`
24
26
}
25
27
type userStack struct {
26
28
UndoStack []* Image
@@ -30,15 +32,16 @@ type userStack struct {
30
32
type Image struct {
31
33
Path string
32
34
}
35
+
33
36
var (
34
37
userStacks = make (map [string ]* userStack )
35
- mu sync.Mutex
36
- client1 * mongo.Client
38
+ mu sync.Mutex
39
+ client1 * mongo.Client
37
40
)
38
41
39
- func connectDb (){
42
+ func connectDb () {
40
43
clientOptions := options .Client ().ApplyURI (os .Getenv ("MONGO_URI" ))
41
- client ,err := mongo .Connect (context .Background (), clientOptions )
44
+ client , err := mongo .Connect (context .Background (), clientOptions )
42
45
if err != nil {
43
46
log .Fatal (err )
44
47
}
@@ -93,7 +96,7 @@ func signup(c *gin.Context) {
93
96
c .JSON (http .StatusBadRequest , gin.H {"error" : err .Error ()})
94
97
return
95
98
}
96
- insertRes ,err := client1 .Database ("users" ).Collection ("login_details" ).InsertOne (context .Background (),user )
99
+ insertRes , err := client1 .Database ("users" ).Collection ("login_details" ).InsertOne (context .Background (), user )
97
100
if err != nil {
98
101
log .Fatal (err )
99
102
}
@@ -115,13 +118,13 @@ func login(c *gin.Context) {
115
118
fmt .Println (user .Email )
116
119
fmt .Println (result .Email )
117
120
if result .Password == user .Password {
118
- tokenString ,err := createToken (& user );
121
+ tokenString , err := createToken (& user )
119
122
if err != nil {
120
123
fmt .Println ("Error creating token:" , err )
121
124
return
122
125
}
123
126
c .JSON (http .StatusOK , gin.H {"token" : tokenString })
124
- }else {
127
+ } else {
125
128
c .JSON (http .StatusBadRequest , gin.H {"error" : "password dont match" })
126
129
}
127
130
}
@@ -133,51 +136,50 @@ func upload(c *gin.Context) {
133
136
c .JSON (http .StatusInternalServerError , gin.H {"error" : "Failed to retrieve email from context" })
134
137
return
135
138
}
136
- _ ,exist := userStacks [email .(string )];
139
+ _ , exist := userStacks [email .(string )]
137
140
if ! exist {
138
141
userStacks [email .(string )] = & userStack {}
139
142
file , err := c .FormFile ("file" )
140
143
if err != nil {
141
144
c .JSON (http .StatusBadRequest , gin.H {"error" : err .Error ()})
142
145
return
143
- }
146
+ }
144
147
err = c .SaveUploadedFile (file , "tempupload/" + file .Filename )
145
- userStacks [email .(string )].CurrentImage = & Image {Path : "tempupload/" + file .Filename }
148
+ userStacks [email .(string )].CurrentImage = & Image {Path : "tempupload/" + file .Filename }
146
149
userStacks [email .(string )].UndoStack = []* Image {}
147
150
userStacks [email .(string )].RedoStack = []* Image {}
148
- if err != nil {
149
- c .JSON (http .StatusInternalServerError , gin.H {"error" : err .Error ()})
150
- return
151
- }
151
+ if err != nil {
152
+ c .JSON (http .StatusInternalServerError , gin.H {"error" : err .Error ()})
153
+ return
154
+ }
152
155
c .JSON (http .StatusOK , gin.H {"message" : "File uploaded successfully" })
153
- }else {
156
+ } else {
154
157
ImagePath := userStacks [email .(string )].CurrentImage .Path
155
- err := os .Remove (ImagePath )
156
- if err != nil {
157
- fmt .Printf ("Error removing %s" ,ImagePath )
158
- }else {
158
+ err := os .Remove (ImagePath )
159
+ if err != nil {
160
+ fmt .Printf ("Error removing %s" , ImagePath )
161
+ } else {
159
162
file , err := c .FormFile ("file" )
160
163
if err != nil {
161
164
c .JSON (http .StatusBadRequest , gin.H {"error" : err .Error ()})
162
165
return
163
- }
166
+ }
164
167
err = c .SaveUploadedFile (file , "tempupload/" + file .Filename )
165
- userStacks [email .(string )].CurrentImage = & Image {Path : "tempupload/" + file .Filename }
168
+ userStacks [email .(string )].CurrentImage = & Image {Path : "tempupload/" + file .Filename }
166
169
userStacks [email .(string )].UndoStack = []* Image {}
167
170
userStacks [email .(string )].RedoStack = []* Image {}
168
- if err != nil {
169
- c .JSON (http .StatusInternalServerError , gin.H {"error" : err .Error ()})
170
- return
171
- }
171
+ if err != nil {
172
+ c .JSON (http .StatusInternalServerError , gin.H {"error" : err .Error ()})
173
+ return
174
+ }
172
175
c .JSON (http .StatusOK , gin.H {"message" : "File uploaded successfully" })
173
176
}
174
177
}
175
178
defer mu .Unlock ()
176
179
}
177
180
178
-
179
181
func undo (c * gin.Context ) {
180
-
182
+
181
183
}
182
184
func redo (c * gin.Context ) {
183
185
@@ -189,26 +191,27 @@ func crop(c *gin.Context) {
189
191
c .JSON (http .StatusInternalServerError , gin.H {"error" : "Failed to retrieve email from context" })
190
192
return
191
193
}
192
- _ ,exist := userStacks [email .(string )];
193
- if exist {
194
- diff := & Image {Path : "awdawd" }
194
+ _ , exist := userStacks [email .(string )]
195
+ if exist {
196
+ diff := & Image {Path : "awdawd" }
195
197
userStacks [email .(string )].UndoStack = append (userStacks [email .(string )].UndoStack , diff )
196
- }else {
198
+ } else {
197
199
c .JSON (http .StatusInternalServerError , gin.H {"error" : "Upload a image first" })
198
200
}
199
201
}
200
- // func imageCenter(img gocv.Mat) image.Point {
201
- // height, width := img.Size()[0], img.Size()[1]
202
- // return image.Pt(width/2, height/2)
203
- // }
204
- func rotate (c * gin.Context ){
205
- email ,exists := c .Get ("email" )
202
+
203
+ // func imageCenter(img gocv.Mat) image.Point {
204
+ // height, width := img.Size()[0], img.Size()[1]
205
+ // return image.Pt(width/2, height/2)
206
+ // }
207
+ func rotate (c * gin.Context ) {
208
+ email , exists := c .Get ("email" )
206
209
if ! exists {
207
210
c .JSON (http .StatusInternalServerError , gin.H {"error" : "Failed to retrieve email from context" })
208
211
return
209
212
}
210
- _ ,exist := userStacks [email .(string )];
211
- if exist {
213
+ _ , exist := userStacks [email .(string )]
214
+ if exist {
212
215
imagePath := userStacks [email .(string )].CurrentImage .Path
213
216
file , err := os .Open (imagePath )
214
217
if err != nil {
@@ -223,21 +226,21 @@ func rotate(c *gin.Context){
223
226
if err != nil {
224
227
log .Fatal (err )
225
228
}
226
- c .JSON (http .StatusOK ,gin.H {"Success" :"True" })
227
- }else {
229
+ c .JSON (http .StatusOK , gin.H {"Success" : "True" })
230
+ } else {
228
231
c .JSON (http .StatusInternalServerError , gin.H {"error" : "Upload a image first" })
229
232
}
230
233
}
231
- func getImage (c * gin.Context ){
232
- email ,exists := c .Get ("email" )
234
+ func getImage (c * gin.Context ) {
235
+ email , exists := c .Get ("email" )
233
236
if ! exists {
234
237
c .JSON (http .StatusInternalServerError , gin.H {"error" : "Failed to retrieve email from context" })
235
238
return
236
239
}
237
- _ ,exist := userStacks [email .(string )];
238
- if exist {
240
+ _ , exist := userStacks [email .(string )]
241
+ if exist {
239
242
imageFile , err := os .Open (userStacks [email .(string )].CurrentImage .Path )
240
- extType := strings .Split (userStacks [email .(string )].CurrentImage .Path , "." )[1 ]
243
+ extType := strings .Split (userStacks [email .(string )].CurrentImage .Path , "." )[1 ]
241
244
if err != nil {
242
245
c .JSON (http .StatusInternalServerError , gin.H {"error" : "Error opening image file" })
243
246
return
@@ -250,7 +253,7 @@ func getImage(c *gin.Context){
250
253
c .JSON (http .StatusInternalServerError , gin.H {"error" : "Error copying image to response" })
251
254
return
252
255
}
253
- }else {
256
+ } else {
254
257
c .JSON (http .StatusInternalServerError , gin.H {"error" : "Upload a image first" })
255
258
}
256
- }
259
+ }
0 commit comments