generated from mattermost/mattermost-plugin-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MM-60340 Enforce max file size when exporting to file via slash comma…
…nd (#61) * add config for max file size; enforce max file size for slash command
- Loading branch information
Showing
10 changed files
with
343 additions
and
15 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,62 @@ | ||
package util | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"sync" | ||
) | ||
|
||
type ErrLimitExceeded struct { | ||
limit uint64 | ||
} | ||
|
||
func (e ErrLimitExceeded) Error() string { | ||
return fmt.Sprintf("limit (%d bytes) exceeded", e.limit) | ||
} | ||
|
||
// LimitPipeWriter wraps an io.PipeWriter and provides a limit to the number of bytes that can be written. | ||
// Exceeding the limit will cause the current Write call to error, and the underlying PipeWriter will be | ||
// closed, causing subsequent reads on the pipe to error. | ||
type LimitPipeWriter struct { | ||
pw *io.PipeWriter | ||
limit uint64 | ||
count uint64 | ||
mux sync.Mutex // protects limit, count | ||
} | ||
|
||
// NewLimitPipeWriter creates a new LimitPipeWriter with the specified limit. | ||
func NewLimitPipeWriter(pw *io.PipeWriter, limit uint64) *LimitPipeWriter { | ||
return &LimitPipeWriter{ | ||
pw: pw, | ||
limit: limit, | ||
} | ||
} | ||
|
||
// Write implements io.Writer | ||
func (lpw *LimitPipeWriter) Write(p []byte) (int, error) { | ||
lpw.mux.Lock() | ||
defer lpw.mux.Unlock() | ||
|
||
count := uint64(len(p)) | ||
if lpw.count+count > lpw.limit { | ||
err := ErrLimitExceeded{lpw.limit} | ||
lpw.pw.CloseWithError(err) // ok to call multiple times | ||
return 0, err | ||
} | ||
|
||
n, err := lpw.pw.Write(p) | ||
lpw.count += uint64(n) | ||
return n, err | ||
} | ||
|
||
// CloseWithError closes the writer. Future reads from the underlying pipe will return the specified error. | ||
// It is safe to call this multiple times - subsequent calls to CloseWithError will be a no-op. | ||
func (lpw *LimitPipeWriter) CloseWithError(err error) error { | ||
return lpw.pw.CloseWithError(err) | ||
} | ||
|
||
// Close closes the writer; subsequent reads from the | ||
// read half of the pipe will return no bytes and io.EOF. | ||
func (lpw *LimitPipeWriter) Close() error { | ||
return lpw.pw.Close() | ||
} |
Oops, something went wrong.