Skip to content

Commit 4fcc2bf

Browse files
committed
fix file uploading
1 parent 4dc442a commit 4fcc2bf

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

internal/app/server/files/files.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -295,36 +295,46 @@ func getFileFromClient(ctx context.Context, m anyMessage, readWriter io.ReadWrit
295295
return writeError(readWriter, fmt.Sprintf("File path \"%s\" not found", dir))
296296
}
297297
} else if err != nil {
298-
logger.Error(ctx, err)
298+
logger.Error(ctx, errors.WithMessagef(err, "failed to stat directory \"%s\"", dir))
299299
return writeError(readWriter, fmt.Sprintf("Directory \"%s\" error", dir))
300300
}
301301

302-
file, err := os.OpenFile(message.FilePath, os.O_CREATE|os.O_WRONLY, message.Perms)
302+
tmpFile, err := os.CreateTemp("", filepath.Base(message.FilePath))
303303
if err != nil {
304-
logger.Error(ctx, err)
305-
return writeError(readWriter, "Failed to open file")
304+
logger.Error(ctx, errors.WithMessage(err, "failed to create temp file"))
305+
return writeError(readWriter, "Failed to create temp file")
306306
}
307307
defer func(file *os.File) {
308-
err := file.Close()
308+
err = file.Close()
309309
if err != nil {
310-
logger.Error(ctx, err)
310+
logger.Error(ctx, errors.WithMessage(err, "failed to close temp file"))
311311
}
312-
}(file)
312+
err = os.Remove(file.Name())
313+
if err != nil {
314+
logger.Error(ctx, errors.WithMessage(err, "failed to remove temp file"))
315+
}
316+
}(tmpFile)
313317

314318
err = response.WriteResponse(readWriter, response.Response{
315319
Code: response.StatusReadyToTransfer,
316320
Info: "File is ready to transfer",
317321
})
318322
if err != nil {
319-
return err
323+
return errors.WithMessage(err, "failed to write ready to transfer response")
320324
}
321325

322-
_, err = io.CopyN(file, readWriter, int64(message.FileSize))
326+
_, err = io.CopyN(tmpFile, readWriter, int64(message.FileSize))
323327
if err != nil {
324328
logger.Error(ctx, err)
325329
return writeError(readWriter, "Failed to transfer file")
326330
}
327331

332+
err = copy.Copy(tmpFile.Name(), message.FilePath)
333+
if err != nil {
334+
logger.Error(ctx, errors.WithMessage(err, "failed to copy tmp file"))
335+
return writeError(readWriter, "Failed to copy tmp file")
336+
}
337+
328338
logger.Debug(ctx, "File successfully transferred")
329339

330340
return response.WriteResponse(readWriter, response.Response{

test/functional/servertest/files/upload_download_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,36 @@ func (suite *Suite) TestUploadSuccess() {
8282
suite.assertUploadedFileContents(fileContents)
8383
}
8484

85+
func (suite *Suite) TestUploadTwiceSuccess() {
86+
suite.Authenticate()
87+
fileContents := []byte{'f', 'i', 'l', 'e', 'c', 'o', 'n', 't', 'e', 'n', 't', 's'}
88+
msg := suite.givenUploadMessage(len(fileContents))
89+
r := suite.ClientWriteReadAndDecodeList(msg)
90+
suite.Equal(response.StatusReadyToTransfer, response.Code(r[0].(uint8)))
91+
92+
// Transfer the file
93+
suite.ClientFileContentsWrite(fileContents)
94+
r = suite.readMessageFromClient()
95+
96+
assert.Equal(suite.T(), response.StatusOK, response.Code(r[0].(uint8)))
97+
suite.assertUploadedFileSize(len(fileContents))
98+
suite.assertUploadedFileContents(fileContents)
99+
100+
// Upload file with the same name and smaller size
101+
fileContents = []byte{'s', 'm', 'a', 'l', 'l', 'e', 'r'}
102+
msg = suite.givenUploadMessage(len(fileContents))
103+
r = suite.ClientWriteReadAndDecodeList(msg)
104+
suite.Equal(response.StatusReadyToTransfer, response.Code(r[0].(uint8)))
105+
106+
// Transfer the file
107+
suite.ClientFileContentsWrite(fileContents)
108+
r = suite.readMessageFromClient()
109+
110+
assert.Equal(suite.T(), response.StatusOK, response.Code(r[0].(uint8)))
111+
suite.assertUploadedFileSize(len(fileContents))
112+
suite.assertUploadedFileContents(fileContents)
113+
}
114+
85115
func (suite *Suite) TestUploadBigFileSuccess() {
86116
suite.Authenticate()
87117
msg := suite.givenUploadMessage(1000000)

0 commit comments

Comments
 (0)