mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-28 01:03:10 -06:00
Let LimitReader read MaxFileSizeBytes + 1
This commit is contained in:
parent
a28af5b750
commit
d27d4c9f2e
|
|
@ -147,7 +147,7 @@ func (r *uploadRequest) doUpload(
|
||||||
// r.storeFileAndMetadata(ctx, tmpDir, ...)
|
// r.storeFileAndMetadata(ctx, tmpDir, ...)
|
||||||
// before you return from doUpload else we will leak a temp file. We could make this nicer with a `WithTransaction` style of
|
// before you return from doUpload else we will leak a temp file. We could make this nicer with a `WithTransaction` style of
|
||||||
// nested function to guarantee either storage or cleanup.
|
// nested function to guarantee either storage or cleanup.
|
||||||
lr := io.LimitReader(reqReader, int64(*cfg.MaxFileSizeBytes))
|
lr := io.LimitReader(reqReader, int64(*cfg.MaxFileSizeBytes)+1)
|
||||||
hash, bytesWritten, tmpDir, err := fileutils.WriteTempFile(ctx, lr, cfg.AbsBasePath)
|
hash, bytesWritten, tmpDir, err := fileutils.WriteTempFile(ctx, lr, cfg.AbsBasePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.Logger.WithError(err).WithFields(log.Fields{
|
r.Logger.WithError(err).WithFields(log.Fields{
|
||||||
|
|
@ -159,9 +159,8 @@ func (r *uploadRequest) doUpload(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if temp file size is greater (should not happen, LimitReader stops when the defined size is reached.)
|
// Check if temp file size exceeds max file size configuration
|
||||||
// or equal to the max file size configuration.
|
if bytesWritten > types.FileSizeBytes(*cfg.MaxFileSizeBytes) {
|
||||||
if bytesWritten >= types.FileSizeBytes(*cfg.MaxFileSizeBytes) {
|
|
||||||
fileutils.RemoveDir(tmpDir, r.Logger) // delete temp file
|
fileutils.RemoveDir(tmpDir, r.Logger) // delete temp file
|
||||||
return requestEntityTooLargeJSONResponse(*cfg.MaxFileSizeBytes)
|
return requestEntityTooLargeJSONResponse(*cfg.MaxFileSizeBytes)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -83,6 +83,23 @@ func Test_uploadRequest_doUpload(t *testing.T) {
|
||||||
},
|
},
|
||||||
want: nil,
|
want: nil,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "upload ok (exact size)",
|
||||||
|
args: args{
|
||||||
|
ctx: context.Background(),
|
||||||
|
reqReader: strings.NewReader("testtest"),
|
||||||
|
cfg: cfg,
|
||||||
|
db: db,
|
||||||
|
},
|
||||||
|
fields: fields{
|
||||||
|
Logger: logger,
|
||||||
|
MediaMetadata: &types.MediaMetadata{
|
||||||
|
MediaID: "1338",
|
||||||
|
UploadName: "test ok (exact size)",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
want: nil,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "upload not ok",
|
name: "upload not ok",
|
||||||
args: args{
|
args: args{
|
||||||
|
|
@ -94,7 +111,7 @@ func Test_uploadRequest_doUpload(t *testing.T) {
|
||||||
fields: fields{
|
fields: fields{
|
||||||
Logger: logger,
|
Logger: logger,
|
||||||
MediaMetadata: &types.MediaMetadata{
|
MediaMetadata: &types.MediaMetadata{
|
||||||
MediaID: "1337",
|
MediaID: "1339",
|
||||||
UploadName: "test fail",
|
UploadName: "test fail",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue