From 9fe1f78c7fe206e459932da40c06580723e9e7aa Mon Sep 17 00:00:00 2001 From: Stefan Friese Date: Tue, 4 Feb 2025 15:29:05 +0000 Subject: [PATCH] added upload and download command --- gommand.go | 174 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 108 insertions(+), 66 deletions(-) diff --git a/gommand.go b/gommand.go index 07f491c..906f2e8 100644 --- a/gommand.go +++ b/gommand.go @@ -8,12 +8,13 @@ import ( "html/template" "io" "net/http" + "net/url" "os" "os/exec" "os/user" "path/filepath" - "strings" "regexp" + "strings" ) type PageData struct { @@ -299,41 +300,62 @@ func changeDirectory(command string, args []string, currentDir *string) CommandO } func fileUploadHandler(w http.ResponseWriter, r *http.Request) { - if r.Method == http.MethodPost { - file, header, err := r.FormFile("file") - if err != nil { - http.Error(w, "Failed to upload file", http.StatusInternalServerError) - return - } - defer file.Close() - - uploadDir := r.FormValue("uploadDir") - if uploadDir == "" { - uploadDir = "./" - } - - out, err := os.Create(filepath.Join(uploadDir, header.Filename)) - if err != nil { - http.Error(w, "Failed to save file", http.StatusInternalServerError) - return - } - defer out.Close() - - _, err = io.Copy(out, file) - if err != nil { - http.Error(w, "Failed to write file", http.StatusInternalServerError) - return - } - cmdOutput := CommandOutput{Command: "upload", Output: "File saved successfully", Error: ""} - // fmt.Fprintf(w, "File %s uploaded successfully to %s", header.Filename, uploadDir) - commandLog = append(commandLog, cmdOutput) - + if r.Method != http.MethodPost { + http.Error(w, "Invalid request method", http.StatusMethodNotAllowed) + return } + + file, header, err := r.FormFile("file") + if err != nil { + http.Error(w, "Failed to upload file", http.StatusInternalServerError) + return + } + defer file.Close() + + uploadDir := r.FormValue("uploadDir") + if uploadDir == "" { + uploadDir = "./" + } + + os.MkdirAll(uploadDir, os.ModePerm) + destPath := filepath.Join(uploadDir, header.Filename) + out, err := os.Create(destPath) + if err != nil { + http.Error(w, "Failed to save file", http.StatusInternalServerError) + return + } + defer out.Close() + + _, err = io.Copy(out, file) + if err != nil { + http.Error(w, "Failed to write file", http.StatusInternalServerError) + return + } + + w.Write([]byte("File uploaded successfully")) } -// func fileDownloadHandler(w http.ResponseWriter, r *http.Request) { -// if r.Method == http.MethodPost { -// file, header, err := r.FormFile("file") +func fileDownloadHandler(w http.ResponseWriter, r *http.Request) { + filePath := r.URL.Query().Get("filePath") + if filePath == ""{ + http.Error(w, "Filepath is required", http.StatusBadRequest) + return + } + absPath, err := filepath.Abs(filePath) + if err != nil || !fileExists(absPath) { + http.Error(w, "File not found", http.StatusNotFound) + return + } + + w.Header().Set("Content-Disposition", "attachement; filename=\""+filepath.Base(absPath)+"\"") + w.Header().Set("Content-Type", "application/octet-stream") + http.ServeFile(w, r, absPath) +} + +func fileExists(filePath string) bool { + _, err := os.Stat(filePath) + return !os.IsNotExist(err) +} func processCommand(command string, currentDir string) CommandOutput { var errorMsg string @@ -422,6 +444,13 @@ func handler(w http.ResponseWriter, r *http.Request) { if command == "cd" { cmdOutput := changeDirectory(command, args, ¤tDir) commandLog = append(commandLog, cmdOutput) + } else if command == "download" { + if len(args) < 1{ + http.Error(w, "Usage: download ", http.StatusBadRequest) + return + } + http.Redirect(w, r, "/download?filePath="+url.QueryEscape(args[0]), http.StatusFound) + return } else { cmdOutput := processCommand(input, currentDir) commandLog = append(commandLog, cmdOutput) @@ -572,40 +601,58 @@ func handler(w http.ResponseWriter, r *http.Request) { + .catch((error) => console.error("Upload failed:", error)); + } +