forked from billy4479/telegram-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
folders.go
165 lines (135 loc) · 3.61 KB
/
folders.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
package api
import (
"errors"
"net/http"
"strconv"
"github.com/qiangweihewu/telegram-yunpan/backend/db"
"github.com/labstack/echo/v4"
"gorm.io/gorm"
)
func GetFolder(c echo.Context) error {
idStr := c.QueryParam("id")
userid, err := getUserIDFromContext(c)
if err != nil {
return returnErrorJSON(c, http.StatusUnauthorized, err)
}
if idStr == "" {
path := c.QueryParam("path")
if path == "" {
return returnErrorJSON(c, http.StatusBadRequest, errors.New("An id or a path is expected"))
}
folder, err := db.GetFolderByPath(path, userid)
if err != nil {
if err == gorm.ErrRecordNotFound {
return returnErrorJSON(c, http.StatusNotFound, err)
}
return returnErrorJSON(c, http.StatusInternalServerError, err)
}
return c.JSON(http.StatusOK, folder)
}
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
return returnErrorJSON(c, http.StatusBadRequest, err)
}
folder, err := db.GetFolderByID(id, userid)
if err != nil {
if err == gorm.ErrRecordNotFound {
return returnErrorJSON(c, http.StatusNotFound, err)
}
return returnErrorJSON(c, http.StatusInternalServerError, err)
}
return c.JSON(http.StatusOK, folder)
}
type createFolderData struct {
Path string `json:"path"`
}
func CreateFolder(c echo.Context) error {
// TODO: accept also parent id + name
data := createFolderData{}
err := c.Bind(&data)
if err != nil {
return returnErrorJSON(c, http.StatusBadRequest, err)
}
id, err := getUserIDFromContext(c)
if err != nil {
return returnErrorJSON(c, http.StatusUnauthorized, err)
}
folder := &db.Folder{
Path: data.Path,
Owner: id,
}
err = db.CreateFolder(folder)
if err = handleDBErr(c, err); err != nil {
return err
}
return c.JSON(http.StatusOK, folder)
}
func DeleteFolder(c echo.Context) error {
recursive := c.QueryParam("recursive")
id, userid, err := getIDAndUserIDFromContext(c)
if err != nil {
return err
}
if recursive == "yes" {
err = db.DeleteFolderRecursive(id, userid)
} else {
err = db.DeleteFolder(id, userid)
}
if err = handleDBErr(c, err); err != nil {
return err
}
return c.NoContent(http.StatusOK)
}
func ListContent(c echo.Context) error {
id, userid, err := getIDAndUserIDFromContext(c)
if err != nil {
return err
}
result, err := db.GetChildrenInFolder(&db.Folder{ID: id, Owner: userid})
if err = handleDBErr(c, err); err != nil {
return err
}
return c.JSON(http.StatusOK, result)
}
func GetTree(c echo.Context) error {
id, userid, err := getIDAndUserIDFromContext(c)
if err != nil {
return err
}
folder, err := db.GetFolderByID(id, userid)
if err = handleDBErr(c, err); err != nil {
return err
}
result, err := db.GetChildrenRecursive(folder)
if err = handleDBErr(c, err); err != nil {
return err
}
return c.JSON(http.StatusOK, result)
}
func GetRoot(c echo.Context) error {
id, err := getUserIDFromContext(c)
if err != nil {
return returnErrorJSON(c, http.StatusUnauthorized, err)
}
folder, err := db.GetRootOf(id)
if err = handleDBErr(c, err); err != nil {
return err
}
return c.JSON(http.StatusOK, folder)
}
func UpdateFolder(c echo.Context) error {
userid, err := getUserIDFromContext(c)
if err != nil {
return returnErrorJSON(c, http.StatusUnauthorized, err)
}
var folder db.Folder
err = c.Bind(&folder)
if err != nil {
return returnErrorJSON(c, http.StatusBadRequest, err)
}
err = db.EditFolder(&folder, userid)
if err = handleDBErr(c, err); err != nil {
return err
}
return c.JSON(http.StatusOK, folder)
}