added upload and download command
This commit is contained in:
parent
dbe33be2c4
commit
9fe1f78c7f
174
gommand.go
174
gommand.go
|
@ -8,12 +8,13 @@ import (
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"os/user"
|
"os/user"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PageData struct {
|
type PageData struct {
|
||||||
|
@ -299,41 +300,62 @@ func changeDirectory(command string, args []string, currentDir *string) CommandO
|
||||||
}
|
}
|
||||||
|
|
||||||
func fileUploadHandler(w http.ResponseWriter, r *http.Request) {
|
func fileUploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == http.MethodPost {
|
if r.Method != http.MethodPost {
|
||||||
file, header, err := r.FormFile("file")
|
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
|
||||||
if err != nil {
|
return
|
||||||
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)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
func fileDownloadHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
// if r.Method == http.MethodPost {
|
filePath := r.URL.Query().Get("filePath")
|
||||||
// file, header, err := r.FormFile("file")
|
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 {
|
func processCommand(command string, currentDir string) CommandOutput {
|
||||||
var errorMsg string
|
var errorMsg string
|
||||||
|
@ -422,6 +444,13 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
if command == "cd" {
|
if command == "cd" {
|
||||||
cmdOutput := changeDirectory(command, args, ¤tDir)
|
cmdOutput := changeDirectory(command, args, ¤tDir)
|
||||||
commandLog = append(commandLog, cmdOutput)
|
commandLog = append(commandLog, cmdOutput)
|
||||||
|
} else if command == "download" {
|
||||||
|
if len(args) < 1{
|
||||||
|
http.Error(w, "Usage: download <filePath>", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
http.Redirect(w, r, "/download?filePath="+url.QueryEscape(args[0]), http.StatusFound)
|
||||||
|
return
|
||||||
} else {
|
} else {
|
||||||
cmdOutput := processCommand(input, currentDir)
|
cmdOutput := processCommand(input, currentDir)
|
||||||
commandLog = append(commandLog, cmdOutput)
|
commandLog = append(commandLog, cmdOutput)
|
||||||
|
@ -572,40 +601,58 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
</script>
|
</script>
|
||||||
<script>
|
<script>
|
||||||
document.addEventListener("DOMContentLoaded", function() {
|
document.addEventListener("DOMContentLoaded", function() {
|
||||||
document.getElementById("command-input").addEventListener("keydown", function(event) {
|
const input = document.getElementById("command-input");
|
||||||
if (event.key === "Enter") {
|
const fileInput = document.getElementById("fileInput");
|
||||||
let inputVal = this.value.trim();
|
let isUploadTriggered = false;
|
||||||
if (inputVal === "upload") {
|
|
||||||
document.getElementById("inputFile").click();
|
document.querySelector("form").addEventListener("submit", function(event) {
|
||||||
|
const command = input.value.trim();
|
||||||
|
const parts = command.split(" ");
|
||||||
|
|
||||||
|
if (parts[0] === "upload") {
|
||||||
|
event.preventDefault();
|
||||||
|
if (parts.length === 1) {
|
||||||
|
fileInput.click();
|
||||||
|
isUploadTriggered = true;
|
||||||
|
} else {
|
||||||
|
const filePath = parts[1];
|
||||||
|
const targetPath = parts.length > 2 ? parts[2] : ".";
|
||||||
|
uploadFileFromBrowser(filePath, targetPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
function requestUpload() {
|
fileInput.addEventListener("change", function () {
|
||||||
let fileInput = document.getElementById("inputFile");
|
if (fileInput.files.length > 0 && isUploadTriggered) {
|
||||||
console.log("file is");
|
const file = fileInput.files[0];
|
||||||
console.log(fileInput);
|
input.value = 'upload "' + file.name + '"';
|
||||||
if (fileInput.files.length === 0) {
|
isUploadTriggered = false;
|
||||||
console.error("No file selected.");
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
let file = fileInput.files[0];
|
});
|
||||||
let formData = new FormData();
|
});
|
||||||
formData.append("file", file);
|
|
||||||
|
|
||||||
fetch("http://localhost:8080/upload", {
|
function uploadFileFromBrowser(filePath, targetPath) {
|
||||||
method: "POST",
|
const fileInput = document.getElementById("fileInput");
|
||||||
body: formData
|
if (fileInput.files.length === 0) {
|
||||||
})
|
console.error("No file selected");
|
||||||
.then(response => response.text())
|
alert("No file selected");
|
||||||
.then(data => {
|
return;
|
||||||
console.log("Upload succesful:", data);
|
}
|
||||||
|
const file = fileInput.files[0];
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.append("file", file);
|
||||||
|
fetch("/upload", {
|
||||||
|
method: "POST",
|
||||||
|
body: formData,
|
||||||
|
})
|
||||||
|
.then((response) => response.text())
|
||||||
|
.then((data) => {
|
||||||
|
console.log("Upload successful:", data);
|
||||||
document.getElementById("command-input").value = "";
|
document.getElementById("command-input").value = "";
|
||||||
})
|
})
|
||||||
.catch(error => console.error("Upload failed:", error));
|
.catch((error) => console.error("Upload failed:", error));
|
||||||
}
|
}
|
||||||
});
|
</script>
|
||||||
</script>
|
|
||||||
<style>
|
<style>
|
||||||
body {
|
body {
|
||||||
background-color: #222;
|
background-color: #222;
|
||||||
|
@ -669,13 +716,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<input type="file" id="fileInput" style="display: none;" onchange="setFilePath()">
|
||||||
<form id="fileUpload" action="/upload" method="POST" enctype="multipart/form-data">
|
|
||||||
<label for="inputFile">Select a file to upload:</label>
|
|
||||||
<input name="inputFile" id="inputFile" type="file">
|
|
||||||
<button id="fileUploadButton" onclick="document.getElementById('inputFile').click()">Upload</button>
|
|
||||||
</form>
|
|
||||||
</div>
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
`))
|
`))
|
||||||
|
@ -691,6 +732,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
func main() {
|
func main() {
|
||||||
http.HandleFunc("/", handler)
|
http.HandleFunc("/", handler)
|
||||||
http.HandleFunc("/upload", fileUploadHandler)
|
http.HandleFunc("/upload", fileUploadHandler)
|
||||||
|
http.HandleFunc("/download", fileDownloadHandler)
|
||||||
fmt.Println("Starting server on :8080")
|
fmt.Println("Starting server on :8080")
|
||||||
http.ListenAndServe(":8080", nil)
|
http.ListenAndServe(":8080", nil)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue