Skip to content

Commit 672a172

Browse files
committed
.
1 parent 9456606 commit 672a172

File tree

1 file changed

+55
-52
lines changed

1 file changed

+55
-52
lines changed

backend/services.go

Lines changed: 55 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,26 @@ package main
33
import (
44
"context"
55
"fmt"
6+
"image/color"
67
"io"
78
"log"
8-
"image/color"
99
"net/http"
1010
"os"
1111
"strings"
1212
"sync"
1313
"time"
14-
"github.com/disintegration/imaging"
14+
1515
"github.com/dgrijalva/jwt-go"
16+
"github.com/disintegration/imaging"
1617
"github.com/gin-gonic/gin"
1718
"go.mongodb.org/mongo-driver/bson"
1819
"go.mongodb.org/mongo-driver/mongo"
1920
"go.mongodb.org/mongo-driver/mongo/options"
2021
)
22+
2123
type User struct {
22-
Email string `json:"email"`
23-
Password string `json:"password"`
24+
Email string `json:"email"`
25+
Password string `json:"password"`
2426
}
2527
type userStack struct {
2628
UndoStack []*Image
@@ -30,15 +32,16 @@ type userStack struct {
3032
type Image struct {
3133
Path string
3234
}
35+
3336
var (
3437
userStacks = make(map[string]*userStack)
35-
mu sync.Mutex
36-
client1 *mongo.Client
38+
mu sync.Mutex
39+
client1 *mongo.Client
3740
)
3841

39-
func connectDb(){
42+
func connectDb() {
4043
clientOptions := options.Client().ApplyURI(os.Getenv("MONGO_URI"))
41-
client,err := mongo.Connect(context.Background(), clientOptions)
44+
client, err := mongo.Connect(context.Background(), clientOptions)
4245
if err != nil {
4346
log.Fatal(err)
4447
}
@@ -93,7 +96,7 @@ func signup(c *gin.Context) {
9396
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
9497
return
9598
}
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)
97100
if err != nil {
98101
log.Fatal(err)
99102
}
@@ -115,13 +118,13 @@ func login(c *gin.Context) {
115118
fmt.Println(user.Email)
116119
fmt.Println(result.Email)
117120
if result.Password == user.Password {
118-
tokenString,err:= createToken(&user);
121+
tokenString, err := createToken(&user)
119122
if err != nil {
120123
fmt.Println("Error creating token:", err)
121124
return
122125
}
123126
c.JSON(http.StatusOK, gin.H{"token": tokenString})
124-
}else{
127+
} else {
125128
c.JSON(http.StatusBadRequest, gin.H{"error": "password dont match"})
126129
}
127130
}
@@ -133,51 +136,50 @@ func upload(c *gin.Context) {
133136
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve email from context"})
134137
return
135138
}
136-
_,exist:= userStacks[email.(string)];
139+
_, exist := userStacks[email.(string)]
137140
if !exist {
138141
userStacks[email.(string)] = &userStack{}
139142
file, err := c.FormFile("file")
140143
if err != nil {
141144
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
142145
return
143-
}
146+
}
144147
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}
146149
userStacks[email.(string)].UndoStack = []*Image{}
147150
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+
}
152155
c.JSON(http.StatusOK, gin.H{"message": "File uploaded successfully"})
153-
}else{
156+
} else {
154157
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 {
159162
file, err := c.FormFile("file")
160163
if err != nil {
161164
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
162165
return
163-
}
166+
}
164167
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}
166169
userStacks[email.(string)].UndoStack = []*Image{}
167170
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+
}
172175
c.JSON(http.StatusOK, gin.H{"message": "File uploaded successfully"})
173176
}
174177
}
175178
defer mu.Unlock()
176179
}
177180

178-
179181
func undo(c *gin.Context) {
180-
182+
181183
}
182184
func redo(c *gin.Context) {
183185

@@ -189,26 +191,27 @@ func crop(c *gin.Context) {
189191
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve email from context"})
190192
return
191193
}
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"}
195197
userStacks[email.(string)].UndoStack = append(userStacks[email.(string)].UndoStack, diff)
196-
}else{
198+
} else {
197199
c.JSON(http.StatusInternalServerError, gin.H{"error": "Upload a image first"})
198200
}
199201
}
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")
206209
if !exists {
207210
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve email from context"})
208211
return
209212
}
210-
_,exist:= userStacks[email.(string)];
211-
if exist{
213+
_, exist := userStacks[email.(string)]
214+
if exist {
212215
imagePath := userStacks[email.(string)].CurrentImage.Path
213216
file, err := os.Open(imagePath)
214217
if err != nil {
@@ -223,21 +226,21 @@ func rotate(c *gin.Context){
223226
if err != nil {
224227
log.Fatal(err)
225228
}
226-
c.JSON(http.StatusOK,gin.H{"Success":"True"})
227-
}else{
229+
c.JSON(http.StatusOK, gin.H{"Success": "True"})
230+
} else {
228231
c.JSON(http.StatusInternalServerError, gin.H{"error": "Upload a image first"})
229232
}
230233
}
231-
func getImage(c *gin.Context){
232-
email,exists := c.Get("email")
234+
func getImage(c *gin.Context) {
235+
email, exists := c.Get("email")
233236
if !exists {
234237
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve email from context"})
235238
return
236239
}
237-
_,exist:= userStacks[email.(string)];
238-
if exist{
240+
_, exist := userStacks[email.(string)]
241+
if exist {
239242
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]
241244
if err != nil {
242245
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error opening image file"})
243246
return
@@ -250,7 +253,7 @@ func getImage(c *gin.Context){
250253
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error copying image to response"})
251254
return
252255
}
253-
}else{
256+
} else {
254257
c.JSON(http.StatusInternalServerError, gin.H{"error": "Upload a image first"})
255258
}
256-
}
259+
}

0 commit comments

Comments
 (0)