diff --git a/backend/localcommand/factory.go b/backend/localcommand/factory.go index be6da37..b811ab7 100644 --- a/backend/localcommand/factory.go +++ b/backend/localcommand/factory.go @@ -4,7 +4,7 @@ import ( "syscall" "time" - "github.com/sorenisanerd/gotty/server" + "github.com/unskript/gotty/server" ) type Options struct { diff --git a/go.mod b/go.mod index b124aca..4041fca 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/sorenisanerd/gotty +module github.com/unskript/gotty go 1.20 diff --git a/main.go b/main.go index 2212b8f..2c249a2 100644 --- a/main.go +++ b/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() { diff --git a/server/handlers.go b/server/handlers.go index 3393b22..a69795f 100644 --- a/server/handlers.go +++ b/server/handlers.go @@ -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 + } +} diff --git a/server/server.go b/server/server.go index c2c1a86..5a6500c 100644 --- a/server/server.go +++ b/server/server.go @@ -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 } diff --git a/server/slave.go b/server/slave.go index db9d731..05951e1 100644 --- a/server/slave.go +++ b/server/slave.go @@ -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. diff --git a/utils/flags.go b/utils/flags.go index f33fec4..08ab605 100644 --- a/utils/flags.go +++ b/utils/flags.go @@ -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) {