-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix remote downloads being trimmed when downloads can be bigger than …
…uploads (#542)
- Loading branch information
Showing
5 changed files
with
56 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package readers | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/turt2live/matrix-media-repo/common" | ||
) | ||
|
||
func LimitReaderWithOverrunError(r io.ReadCloser, n int64) io.ReadCloser { | ||
return &limitedReader{r: r, n: n} | ||
} | ||
|
||
type limitedReader struct { | ||
r io.ReadCloser | ||
n int64 | ||
} | ||
|
||
func (r *limitedReader) Read(p []byte) (int, error) { | ||
if r.n <= 0 { | ||
// See if we can read one more byte, indicating the stream is too big | ||
b := make([]byte, 1) | ||
n, err := r.r.Read(b) | ||
p[0] = b[0] | ||
if err != nil { | ||
// ignore - we're at the end anyways | ||
return n, io.EOF | ||
} | ||
if n > 0 { | ||
return n, common.ErrMediaTooLarge | ||
} | ||
|
||
return n, io.EOF | ||
} | ||
|
||
n, err := r.r.Read(p) | ||
r.n -= int64(n) | ||
return n, err | ||
} | ||
|
||
func (r *limitedReader) Close() error { | ||
return r.r.Close() | ||
} |