Add client_read, client_write, client_edit and client_download

This commit is contained in:
Ian Gulliver
2026-10-18 00:13:37 -07:00
parent 2ed0adae90
commit e8599a7fce
7 changed files with 736 additions and 262 deletions
+29 -2
View File
@@ -4,9 +4,12 @@ import (
"bytes"
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"os"
"os/exec"
"path/filepath"
"time"
"github.com/coder/websocket"
@@ -38,8 +41,8 @@ func (s *Server) ConnectClient(w http.ResponseWriter, r *http.Request) {
func (s *Server) GetClientTransfer(w http.ResponseWriter, r *http.Request, transferID string) {
t, ok := s.clients.Transfer(transferID)
if !ok {
writeError(w, http.StatusNotFound, fmt.Errorf("no transfer %s", transferID))
if !ok || t.Receive {
writeError(w, http.StatusNotFound, fmt.Errorf("no transfer %s to send", transferID))
return
}
@@ -54,3 +57,27 @@ func (s *Server) GetClientTransfer(w http.ResponseWriter, r *http.Request, trans
panic(http.ErrAbortHandler)
}
}
func (s *Server) PutClientTransfer(w http.ResponseWriter, r *http.Request, transferID string) {
t, ok := s.clients.Transfer(transferID)
if !ok || !t.Receive {
writeError(w, http.StatusNotFound, fmt.Errorf("no transfer %s to receive", transferID))
return
}
f, err := os.OpenFile(filepath.Join(t.Dir, t.Name), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600)
if err != nil {
writeError(w, http.StatusNotFound, fmt.Errorf("transfer %s: %w", transferID, err))
return
}
_, err = io.Copy(f, r.Body)
cerr := f.Close()
if err == nil {
err = cerr
}
if err != nil {
writeError(w, http.StatusInternalServerError, fmt.Errorf("receive transfer %s: %w", transferID, err))
return
}
w.WriteHeader(http.StatusNoContent)
}