Removing filename return from UploadFile

This commit is contained in:
Denis-Cosmin Nutiu 2017-12-01 00:17:35 +02:00
parent 88229fed6d
commit c06c11e96e
2 changed files with 9 additions and 16 deletions

View file

@ -24,16 +24,16 @@ func randSeq(n int) string {
} }
// UploadFile uploads a file to the server // UploadFile uploads a file to the server
func UploadFile(c Client, filename string) (string, error) { func UploadFile(c Client, filename string) error {
f, err := os.Create(MakePathFromStringStack(c.Stack()) + filename) f, err := os.Create(MakePathFromStringStack(c.Stack()) + filename)
if err != nil { if err != nil {
return filename, err return err
} }
defer f.Close() defer f.Close()
io.Copy(f, c.Connection()) io.Copy(f, c.Connection())
return filename, nil return nil
} }
// SendAsciiPic sends an image as ascii text to the client. // SendAsciiPic sends an image as ascii text to the client.

View file

@ -38,11 +38,6 @@ type Client interface {
Stack() *StringStack // Returns the underlying String Stack. Stack() *StringStack // Returns the underlying String Stack.
} }
type UploadResult struct {
Filename string // Filename represents the file which was randomly created by the server
Err error // Err is the error that occurred while uploading the file.
}
// FTPClient represents a FTPClient connection, it holds a root cage and the underlying connection. // FTPClient represents a FTPClient connection, it holds a root cage and the underlying connection.
type FTPClient struct { type FTPClient struct {
rootCage *StringStack // rootCage is a StringStack that is used to represent the current directory the client is in. rootCage *StringStack // rootCage is a StringStack that is used to represent the current directory the client is in.
@ -163,31 +158,29 @@ func HandleUpload(conn net.Conn) {
} }
// This channel will be used to store the uploadResult // This channel will be used to store the uploadResult
c1 := make(chan UploadResult, 1) c1 := make(chan error, 1)
log.Println(conn.RemoteAddr().String() + " is uploading something.") log.Println(conn.RemoteAddr().String() + " is uploading something.")
// Create a new Go routine for uploading // Create a new Go routine for uploading
go func() { go func() {
fname, err := UploadFile(&client, filename) err := UploadFile(&client, filename)
c1 <- UploadResult{fname, err} c1 <- err
}() }()
// Wait for either UploadResult or Timeout // Wait for either UploadResult or Timeout
select { select {
case result := <-c1: case result := <-c1:
{ {
filename, err := result.Filename, result.Err if result == nil {
if err == nil {
io.WriteString(conn, filename) io.WriteString(conn, filename)
log.Println(conn.RemoteAddr().String() + "'s upload finished.") log.Println(conn.RemoteAddr().String() + "'s upload finished.")
} else { } else {
log.Println(fmt.Sprintf("%s: %s %s", "HandleUpload", conn.RemoteAddr().String(), err.Error())) log.Println(fmt.Sprintf("%s: %s %s", "HandleUpload", conn.RemoteAddr().String(), result.Error()))
client.Stack().Push(filename) client.Stack().Push(filename)
os.Remove(MakePathFromStringStack(client.Stack())) os.Remove(MakePathFromStringStack(client.Stack()))
io.WriteString(conn, err.Error()) io.WriteString(conn, result.Error())
} }
conn.Close() conn.Close()