Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "bufio"
- "fmt"
- "github.com/jlaffaye/ftp"
- "io/ioutil"
- "log"
- "net/http"
- "os"
- )
- var (
- Server = "185.20.227.83:9212"
- Login = "admin"
- Password = "123456"
- BasePath = "/home/iu9_33_03/GoProjects/src/Laba1"
- client *ftp.ServerConn
- file *os.File
- err error
- cur1, _ = ioutil.ReadFile("web/Home.html")
- homeScreenForm = string(cur1)
- cur2, _ = ioutil.ReadFile("web/Menu.html")
- menuScreenFormStart = string(cur2)
- cur3, _ = ioutil.ReadFile("web/Options.html")
- options = string(cur3)
- cur4, _ = ioutil.ReadFile("web/End.html")
- menuScreenFormEnd = string(cur4)
- )
- func HomeScreenHandler(w http.ResponseWriter, r *http.Request) {
- fmt.Fprintf(w, homeScreenForm)
- }
- func MenuScreenHandler(w http.ResponseWriter, r *http.Request) {
- /*
- if r.URL.Query().Get("server") != "" {
- Server = r.URL.Query().Get("server")
- }
- if r.URL.Query().Get("login") != "" {
- Login = r.URL.Query().Get("login")
- }
- if r.URL.Query().Get("pass") != "" {
- Password = r.URL.Query().Get("pass")
- }
- */
- res := menuScreenFormStart
- res += "<center><p><h2>" + Server + "</h2></p>" + "<p><h3> Connected </h3></p>"
- res += options
- option := r.URL.Query().Get("options")
- switch option {
- case "delete":
- err = removeDirectory(r.URL.Query().Get("f1"))
- if err != nil {
- log.Fatal(err)
- }
- case "make":
- err = makeDirectory(r.URL.Query().Get("f1"))
- if err != nil {
- log.Fatal(err)
- }
- case "list":
- entries, err := listOfDirectories(r.URL.Query().Get("f1"))
- if err != nil {
- log.Fatal(err)
- }
- res += "<p><b>" + r.URL.Query().Get("f1") + ": </b></p>"
- for _, e := range entries {
- res += "<p>" + e.Name + "</p>"
- }
- case "upload":
- err = uploadFile(r.URL.Query().Get("f1"), r.URL.Query().Get("f2"))
- if err != nil {
- log.Fatal(err)
- }
- case "read":
- str, err := readFile(r.URL.Query().Get("f1"))
- if err != nil {
- log.Fatal(err)
- }
- res += "<p>" + str + "</p>"
- case "currentDir":
- str, err := currentDirectory()
- if err != nil {
- log.Fatal(err)
- }
- res += "<p>" + str + "</p>"
- case "change":
- err := changeDirectory(r.URL.Query().Get("f1"))
- if err != nil {
- log.Fatal(err)
- }
- }
- res += menuScreenFormEnd
- fmt.Fprint(w, res)
- }
- func uploadFile(from string, to string) error {
- file, err = os.Open(BasePath + from)
- if err != nil {
- return err
- }
- reader := bufio.NewReader(file)
- err = client.Stor(to, reader)
- if err != nil {
- return err
- }
- return nil
- }
- func removeDirectory(path string) error {
- err = client.Delete(path)
- if err != nil {
- return err
- }
- return nil
- }
- func makeDirectory(dir string) error {
- err = client.MakeDir(dir)
- if err != nil {
- return err
- }
- return nil
- }
- func listOfDirectories(path string) ([]*ftp.Entry, error) {
- entries, err := client.List(path)
- if err != nil {
- return nil, err
- }
- return entries, nil
- }
- func readFile(path string) (string, error) {
- file, err := client.Retr(path)
- if err != nil {
- return "", err
- }
- buffer, err := ioutil.ReadAll(file)
- if err != nil {
- return "", err
- }
- return string(buffer), nil
- }
- func currentDirectory() (string, error) {
- name, err := client.CurrentDir()
- if err != nil {
- return "", err
- }
- return name, nil
- }
- func changeDirectory(path string) error {
- err = client.ChangeDir(path)
- if err != nil {
- return err
- }
- return nil
- }
- func main() {
- client, err = ftp.Connect(Server)
- if err != nil {
- log.Fatal(err)
- }
- err = client.Login(Login, Password)
- if err != nil {
- log.Fatal(err)
- }
- http.HandleFunc("/", HomeScreenHandler)
- http.HandleFunc("/menu", MenuScreenHandler)
- err = http.ListenAndServe(":9212", nil)
- if err != nil {
- log.Fatal("ListenAndServe: ", err)
- }
- err = client.Quit()
- if err != nil {
- log.Fatal(err)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement