forked from kgretzky/pwndrop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubfiles.go
146 lines (122 loc) · 3.35 KB
/
subfiles.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
package api
import (
"net/http"
"os"
"path/filepath"
"strconv"
"time"
"github.com/gorilla/mux"
"github.com/kgretzky/pwndrop/log"
"github.com/kgretzky/pwndrop/storage"
"github.com/kgretzky/pwndrop/utils"
)
func SubFileCreateHandler(w http.ResponseWriter, r *http.Request) {
// #### CHECK IF AUTHENTICATED ####
_, err := AuthSession(r)
if err != nil {
DumpResponse(w, "unauthorized", http.StatusUnauthorized, API_ERROR_BAD_AUTHENTICATION, nil)
return
}
vars := mux.Vars(r)
data_dir := Cfg.GetDataDir()
user_id := 1
file, fhead, err := r.FormFile("file")
if err != nil {
DumpResponse(w, err.Error(), http.StatusBadRequest, API_ERROR_BAD_REQUEST, nil)
return
}
defer file.Close()
fid, err := strconv.Atoi(vars["id"])
if err != nil {
DumpResponse(w, err.Error(), http.StatusBadRequest, API_ERROR_BAD_REQUEST, nil)
return
}
parent_file, err := storage.FileGet(fid)
if err != nil {
DumpResponse(w, err.Error(), http.StatusBadRequest, API_ERROR_FILE_NOT_FOUND, nil)
return
}
name := fhead.Filename
fname := utils.GenRandomHash()
os.Mkdir(filepath.Join(data_dir, "files"), 0700)
save_path := filepath.Join(data_dir, "files", fname)
if err := SaveUploadedFile(file, fhead, save_path); err != nil {
DumpResponse(w, err.Error(), http.StatusInternalServerError, API_ERROR_FILE_SAVE_FAILED, nil)
return
}
var fi os.FileInfo
if fi, err = os.Stat(save_path); err != nil {
os.Remove(save_path)
DumpResponse(w, err.Error(), http.StatusInternalServerError, API_ERROR_FILE_NOT_FOUND, nil)
return
}
o := &storage.DbSubFile{
Fid: fid,
Uid: user_id,
Name: name,
Filename: fname,
FileSize: fi.Size(),
CreateTime: time.Now().Unix(),
}
f, err := storage.SubFileCreate(o)
if err != nil {
os.Remove(save_path)
DumpResponse(w, err.Error(), http.StatusInternalServerError, API_ERROR_FILE_DATABASE_FAILED, nil)
return
}
parent_file.SubName = f.Name
parent_file.RefSubFile = f.ID
log.Debug("ref_sub_file: %d", parent_file.RefSubFile)
_, err = storage.FileUpdate(fid, parent_file)
if err != nil {
DumpResponse(w, err.Error(), http.StatusInternalServerError, API_ERROR_FILE_SAVE_FAILED, nil)
return
}
DumpResponse(w, "ok", http.StatusOK, 0, f)
}
func SubFileDeleteHandler(w http.ResponseWriter, r *http.Request) {
// #### CHECK IF AUTHENTICATED ####
_, err := AuthSession(r)
if err != nil {
DumpResponse(w, "unauthorized", http.StatusUnauthorized, API_ERROR_BAD_AUTHENTICATION, nil)
return
}
vars := mux.Vars(r)
sub_id, err := strconv.Atoi(vars["sub_id"])
if err != nil {
DumpResponse(w, err.Error(), http.StatusBadRequest, API_ERROR_BAD_REQUEST, nil)
return
}
err = DeleteSubFile(sub_id)
if err != nil {
DumpResponse(w, err.Error(), http.StatusInternalServerError, API_ERROR_FILE_DATABASE_FAILED, nil)
return
}
files, err := storage.FileList()
if err == nil {
for _, f := range files {
if f.RefSubFile == sub_id {
storage.FilePause(f.ID, false)
}
}
}
DumpResponse(w, "ok", http.StatusOK, 0, nil)
}
func DeleteSubFile(sub_id int) error {
data_dir := Cfg.GetDataDir()
f, err := storage.SubFileGet(sub_id)
if err != nil {
return err
}
_, err = storage.FileResetSubFile(f.Fid)
if err != nil {
return err
}
err = storage.SubFileDelete(sub_id)
if err != nil {
return err
}
save_path := filepath.Join(data_dir, "files", f.Filename)
os.Remove(save_path)
return nil
}