mirror of
https://github.com/sorenisanerd/gotty.git
synced 2025-04-01 16:40:28 +00:00
EN-5280 : Add upload/download endpoint in gotty
This commit is contained in:
parent
bf3eae1d9a
commit
be688bfb6f
@ -4,7 +4,7 @@ import (
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/sorenisanerd/gotty/server"
|
||||
"github.com/unskript/gotty/server"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
|
2
go.mod
2
go.mod
@ -1,4 +1,4 @@
|
||||
module github.com/sorenisanerd/gotty
|
||||
module github.com/unskript/gotty
|
||||
|
||||
go 1.20
|
||||
|
||||
|
8
main.go
8
main.go
@ -12,10 +12,10 @@ import (
|
||||
|
||||
cli "github.com/urfave/cli/v2"
|
||||
|
||||
"github.com/sorenisanerd/gotty/backend/localcommand"
|
||||
"github.com/sorenisanerd/gotty/pkg/homedir"
|
||||
"github.com/sorenisanerd/gotty/server"
|
||||
"github.com/sorenisanerd/gotty/utils"
|
||||
"github.com/unskript/gotty/backend/localcommand"
|
||||
"github.com/unskript/gotty/pkg/homedir"
|
||||
"github.com/unskript/gotty/server"
|
||||
"github.com/unskript/gotty/utils"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -9,11 +9,14 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sync/atomic"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/sorenisanerd/gotty/webtty"
|
||||
"github.com/unskript/gotty/webtty"
|
||||
)
|
||||
|
||||
func (server *Server) generateHandleWS(ctx context.Context, cancel context.CancelFunc, counter *counter) http.HandlerFunc {
|
||||
@ -258,3 +261,70 @@ func (server *Server) titleVariables(order []string, varUnits map[string]map[str
|
||||
|
||||
return titleVars
|
||||
}
|
||||
|
||||
// Add a new method to handle file download to gotty
|
||||
func (server *Server) handleDownload(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
// Get the file from the form data
|
||||
file, handler, err := r.FormFile("file")
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Error retrieving file: %s", err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// Save the file to a location (you might want to customize this)
|
||||
filepath := filepath.Join("/unskript/downloads/", handler.Filename)
|
||||
outFile, err := os.Create(filepath)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Error creating file: %s", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer outFile.Close()
|
||||
|
||||
// Copy the file content to the destination file
|
||||
_, err = io.Copy(outFile, file)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Error copying file: %s", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("File %s Downloaded successfully", handler.Filename)
|
||||
}
|
||||
|
||||
// Add a new method to handle file uploads from gotty
|
||||
func (server *Server) handleUpload(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
// Retrieve the file path from the request parameters
|
||||
filePath := r.FormValue("path")
|
||||
if filePath == "" {
|
||||
http.Error(w, "File path parameter 'path' is missing", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Open the file
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Error opening file: %s", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// Set the appropriate headers for download
|
||||
w.Header().Set("Content-Disposition", "attachment; filename="+filepath.Base(filePath))
|
||||
w.Header().Set("Content-Type", "application/octet-stream")
|
||||
|
||||
// Copy the file content to the response writer
|
||||
_, err = io.Copy(w, file)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Error copying file to response: %s", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -19,10 +19,10 @@ import (
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/sorenisanerd/gotty/bindata"
|
||||
"github.com/sorenisanerd/gotty/pkg/homedir"
|
||||
"github.com/sorenisanerd/gotty/pkg/randomstring"
|
||||
"github.com/sorenisanerd/gotty/webtty"
|
||||
"github.com/unskript/gotty/bindata"
|
||||
"github.com/unskript/gotty/pkg/homedir"
|
||||
"github.com/unskript/gotty/pkg/randomstring"
|
||||
"github.com/unskript/gotty/webtty"
|
||||
)
|
||||
|
||||
// Server provides a webtty HTTP endpoint.
|
||||
@ -232,6 +232,10 @@ func (server *Server) setupHandlers(ctx context.Context, cancel context.CancelFu
|
||||
wsMux.HandleFunc(pathPrefix+"ws", server.generateHandleWS(ctx, cancel, counter))
|
||||
siteHandler = http.Handler(wsMux)
|
||||
|
||||
siteMux.HandleFunc(pathPrefix+"upload", server.handleUpload)
|
||||
siteMux.HandleFunc(pathPrefix+"download", server.handleDownload)
|
||||
|
||||
|
||||
return siteHandler
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"github.com/sorenisanerd/gotty/webtty"
|
||||
"github.com/unskript/gotty/webtty"
|
||||
)
|
||||
|
||||
// Slave is webtty.Slave with some additional methods.
|
||||
|
@ -11,7 +11,7 @@ import (
|
||||
"github.com/urfave/cli/v2"
|
||||
"github.com/yudai/hcl"
|
||||
|
||||
"github.com/sorenisanerd/gotty/pkg/homedir"
|
||||
"github.com/unskript/gotty/pkg/homedir"
|
||||
)
|
||||
|
||||
func GenerateFlags(options ...interface{}) (flags []cli.Flag, mappings map[string]string, err error) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user