-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
159 lines (134 loc) · 3.69 KB
/
api.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
package main
import (
"errors"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/jackc/pgx/v4"
"io"
"net/http"
"os"
)
// Api for deleting your own account
func (app *Application) accountDeleteAPI(c *gin.Context) {
userID, err := app.db.idByToken(c.GetString("token"))
if errors.Is(err, pgx.ErrNoRows) {
c.AbortWithStatus(http.StatusUnauthorized)
return
} else if err != nil {
app.logError.Println(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
if err = app.deleteAccountWithImages(userID); err != nil {
app.logError.Println(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
}
func (app *Application) deleteAccountWithImages(userID int) (err error) {
images, err := app.db.getAllImagesFromAccount(userID)
if err != nil {
return
}
for _, image := range images {
if err = app.deleteFile(image.FileName); err != nil {
app.logError.Println(err)
}
}
if err = app.db.deleteImagesFromAccount(userID); err != nil {
return
}
if err = app.db.deleteAccount(userID); err != nil {
return
}
return
}
// Api for deleting an image from your account
type deleteImageAPIInput struct {
FileName string `form:"file_name"`
}
func (app *Application) deleteImageAPI(c *gin.Context) {
var input deleteImageAPIInput
var err error
if err = c.MustBindWith(&input, binding.FormPost); err != nil {
c.String(http.StatusBadRequest, err.Error())
c.Abort()
return
}
// Makes sure the image exists
var exists bool
if exists, err = app.db.fileExists(input.FileName); err != nil {
app.logError.Println(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
} else if !exists {
c.AbortWithStatus(http.StatusBadRequest)
return
}
if err = app.deleteFile(input.FileName); err != nil { // Deletes file
app.logError.Println(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
if err = app.db.deleteImageUploadToken(input.FileName, c.GetString("uploadToken")); err != nil { // Deletes file entry from database
app.logError.Println(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.String(http.StatusOK, "Successfully deleted the image")
}
// Api for uploading image
func (app *Application) uploadImageAPI(c *gin.Context) {
fileRaw, _, err := c.Request.FormFile("file")
defer fileRaw.Close()
if err != nil {
c.String(http.StatusBadRequest, "No file provided")
c.Abort()
return
}
file, err := io.ReadAll(fileRaw)
if err != nil {
app.logError.Println(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
fullFileName, err := app.generateFullFileName(file)
if err != nil {
app.logError.Println(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
switch app.config.fileStorageMethod {
case fileStorageS3:
err = app.uploadFileS3(file, fullFileName)
case fileStorageLocal:
err = os.WriteFile(app.config.DataFolder+fullFileName, file, 0600)
default:
err = ErrUnknownStorageMethod
}
if err != nil {
app.logError.Println(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
if err = app.db.insertNewImageUploadToken(fullFileName, c.GetString("uploadToken")); err != nil {
app.logError.Println(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.Redirect(http.StatusTemporaryRedirect, "/"+fullFileName)
}
// Api for changing your upload token
func (app *Application) newUploadTokenAPI(c *gin.Context) {
uploadToken, err := app.db.replaceUploadToken(c.GetString("token"))
if err != nil {
if !errors.Is(err, pgx.ErrNoRows) {
app.logError.Println(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.AbortWithStatus(http.StatusUnauthorized)
return
}
c.String(http.StatusOK, uploadToken)
}