-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftp.go
92 lines (74 loc) · 2.43 KB
/
ftp.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
package controllers
import (
"database/sql"
"net/http"
)
type FTPController struct{}
func (fc *FTPController) FTP(w http.ResponseWriter, r *http.Request) {
session, err := store.Get(r, "session")
if err!= nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
username, ok := session.Values["username"]
if!ok {
http.Error(w, "You are not logged in", http.StatusUnauthorized)
return
}
var ftpAccounts []struct {
Username string
Password string
}
err = db.QueryRows("SELECT username, password FROM ftp_accounts WHERE username = $1", username).Scan(&ftpAccounts)
if err!= nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data := map[string]interface{}{
"ftpAccounts": ftpAccounts,
}
err = tmpl.ExecuteTemplate(w, "ftp", data)
if err!= nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func (fc *FTPController) CreateFTP(w http.ResponseWriter, r *http.Request) {
session, err := store.Get(r, "session")
if err!= nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
username, ok := session.Values["username"]
if!ok {
http.Error(w, "You are not logged in", http.StatusUnauthorized)
return
}
ftpUsername := r.FormValue("ftp_username")
ftpPassword := r.FormValue("ftp_password")
_, err = db.Exec("INSERT INTO ftp_accounts (username, password) VALUES ($1, $2)", username, ftpUsername, ftpPassword)
if err!= nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/ftp", http.StatusFound)
}
func (fc *FTPController) DeleteFTP(w http.ResponseWriter, r *http.Request) {
session, err := store.Get(r, "session")
if err!= nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
username, ok := session.Values["username"]
if!ok {
http.Error(w, "You are not logged in", http.StatusUnauthorized)
return
}
ftpUsername := r.FormValue("ftp_username")
_, err = db.Exec("DELETE FROM ftp_accounts WHERE username = $1 AND ftp_username = $2", username, ftpUsername)
if err!= nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/ftp", http.StatusFound)
}