-
Notifications
You must be signed in to change notification settings - Fork 1
/
trash.go
174 lines (146 loc) · 4.58 KB
/
trash.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
package main
import (
"net/http"
"path"
"strconv"
"github.com/go-chi/chi"
"github.com/jmoiron/sqlx"
db "github.com/xbsoftware/wfs-db"
)
func addTrashRoutes(r chi.Router) {
r.Post("/delete", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
id := r.Form.Get("id")
if id == "" || id[0:2] == "./" {
panic("path not provided")
}
obj, err := drive.Info(id)
if err != nil {
format.JSON(w, 500, Response{Invalid: true, Error: err.Error()})
return
}
did := dbID(id)
_, err = conn.Exec("update entity set path = ?, folder = -1 where path = ? AND tree = ?", "./"+strconv.Itoa(did)+id, id, User.Root)
if err != nil {
format.JSON(w, 500, Response{Invalid: true, Error: err.Error()})
return
}
// mark all files in deleted folder
if obj.Type == "folder" {
_, err = conn.Exec("update entity set path = concat(\".\", path) where path LIKE ? AND tree = ?", id+"/%", User.Root)
if err != nil {
format.JSON(w, 500, Response{Invalid: true, Error: err.Error()})
return
}
}
format.JSON(w, 200, Response{})
})
r.Put("/delete", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
id := r.Form.Get("id")
if id[0:2] != "./" {
panic("wrong id provided")
}
obj := db.DBFile{}
conn.Get(&obj, "select * from entity where path=? and tree=?", id, User.Root)
if obj.ID == 0 {
panic("wrong id provided")
}
// all involved files
ids := make([]int, 0)
if obj.Type == 2 {
ids = append(ids, selectIdRec(obj.ID)...)
}
prefixLen := len("./" + strconv.Itoa(obj.ID))
deletedPath := obj.Path[prefixLen:]
restorePath := r.Form.Get("target")
if restorePath == "" {
restorePath = path.Dir(deletedPath)
}
// check restore folder
newRoot := 0
conn.Get(&newRoot, "SELECT id FROM entity WHERE path = ? AND tree = ?", restorePath, User.Root)
if newRoot == 0 {
conn.Get(&newRoot, "SELECT id FROM entity WHERE path = \"/\" AND tree = ?", User.Root)
}
targetName := obj.FileName
targetPath := path.Join(restorePath, path.Base(deletedPath))
// ensure that file name is not occupied
for {
fileUsed := 0
conn.Get(&fileUsed, "SELECT id FROM entity WHERE path = ? AND tree = ?", targetPath, User.Root)
if fileUsed == 0 {
break
}
ext := path.Ext(targetName)
targetName = makeUnique(targetName, ext, obj.Type)
targetPath = makeUnique(targetPath, ext, obj.Type)
}
// restore the object
_, err := conn.Exec("UPDATE entity set name = ?, path = ?, folder = ? where path = ? AND tree = ?", targetName, targetPath, newRoot, id, User.Root)
if err != nil {
format.JSON(w, 500, Response{Invalid: true, Error: err.Error()})
return
}
// and all inner objects
if len(ids) > 0 {
sql, args, _ := sqlx.In("UPDATE entity set path = REPLACE(path, ?, ?) where id IN (?) AND tree = ?", "."+deletedPath+"/", targetPath+"/", ids, User.Root)
_, err = conn.Exec(sql, args...)
if err != nil {
format.JSON(w, 500, Response{Invalid: true, Error: err.Error()})
return
}
}
info, err := drive.Info(targetPath)
format.JSON(w, 200, info)
})
r.Delete("/delete", func(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get("id")
if id[0:2] != "./" {
panic("wrong id provided")
}
obj := db.DBFile{}
conn.Get(&obj, "select * from entity where path=? and tree=?", id, User.Root)
if obj.ID == 0 {
panic("wrong id provided")
}
// all involved files
ids := []int{obj.ID}
if obj.Type == 2 {
ids = append(ids, selectIdRec(obj.ID)...)
}
// delete related markers
idStr, args, _ := sqlx.In("entity_id IN(?) AND user_id = ? ", ids, User.Root)
conn.Exec("DELETE FROM favorite WHERE "+idStr, args...)
idStr, args, _ = sqlx.In("entity_id IN(?)", ids)
conn.Exec("DELETE FROM entity_tag WHERE "+idStr, args...)
conn.Exec("DELETE FROM entity_user WHERE "+idStr, args...)
// delete file itself
idStr, args, _ = sqlx.In("DELETE FROM entity where id in (?)", ids)
_, err := conn.Exec(idStr, args...)
if err != nil {
format.JSON(w, 500, Response{Invalid: true, Error: err.Error()})
return
}
format.JSON(w, 200, Response{})
})
}
func selectIdRec(folder int) []int {
var ids = make([]db.DBFile, 0)
conn.Select(&ids, "select id,type FROM entity where folder = ? AND tree = ?", folder, User.Root)
out := make([]int, 0, len(ids))
for i := range ids {
out = append(out, ids[i].ID)
if ids[i].Type == 2 {
out = append(out, selectIdRec(ids[i].ID)...)
}
}
return out
}
func makeUnique(name, ext string, ftype int) string {
if ftype == 2 || ext == "" {
return name + ".restored"
}
index := len(name) - len(ext)
return name[:index] + ".restored" + name[index:]
}