Task Attachments #104
|
@ -87,6 +87,7 @@ const (
|
|||
RateLimitStore Key = `ratelimit.store`
|
||||
|
||||
FilesBasePath Key = `files.basepath`
|
||||
FilesMaxSize Key = `files.maxsize`
|
||||
)
|
||||
|
||||
// GetString returns a string config value
|
||||
|
@ -196,6 +197,7 @@ func InitDefaultConfig() {
|
|||
RateLimitStore.setDefault("memory")
|
||||
// Files
|
||||
FilesBasePath.setDefault("./files")
|
||||
FilesMaxSize.setDefault(21474836480) // 20 MB
|
||||
}
|
||||
|
||||
// InitConfig initializes the config, sets defaults etc.
|
||||
|
|
|
@ -34,3 +34,19 @@ func IsErrFileDoesNotExist(err error) bool {
|
|||
_, ok := err.(ErrFileDoesNotExist)
|
||||
return ok
|
||||
}
|
||||
|
||||
// ErrFileIsTooLarge defines an error where a file is larger than the configured limit
|
||||
type ErrFileIsTooLarge struct {
|
||||
Size int64
|
||||
}
|
||||
|
||||
// Error is the error implementaion of ErrFileIsTooLarge
|
||||
func (err ErrFileIsTooLarge) Error() string {
|
||||
return fmt.Sprintf("file is too large [Size: %d]", err.Size)
|
||||
}
|
||||
|
||||
//IsErrFileIsTooLarge checks if an error is ErrFileIsTooLarge
|
||||
func IsErrFileIsTooLarge(err error) bool {
|
||||
_, ok := err.(ErrFileIsTooLarge)
|
||||
return ok
|
||||
}
|
||||
|
|
|
@ -68,6 +68,10 @@ func (f *File) LoadFileMetaByID() (err error) {
|
|||
// Create creates a new file from an FileHeader
|
||||
func Create(f io.ReadCloser, realname string, realsize int64, a web.Auth) (file *File, err error) {
|
||||
|
||||
if realsize > config.FilesMaxSize.GetInt64() {
|
||||
return nil, ErrFileIsTooLarge{Size: realsize}
|
||||
}
|
||||
|
||||
// We first insert the file into the db to get it's ID
|
||||
file = &File{
|
||||
Name: realname,
|
||||
|
|
|
@ -51,21 +51,34 @@ func (a *testauth) GetID() int64 {
|
|||
}
|
||||
|
||||
func TestCreate(t *testing.T) {
|
||||
initFixtures(t)
|
||||
tf := &testfile{
|
||||
content: []byte("testfile"),
|
||||
}
|
||||
ta := &testauth{id: 1}
|
||||
_, err := Create(tf, "testfile", 100, ta)
|
||||
assert.NoError(t, err)
|
||||
t.Run("Normal", func(t *testing.T) {
|
||||
initFixtures(t)
|
||||
tf := &testfile{
|
||||
content: []byte("testfile"),
|
||||
}
|
||||
ta := &testauth{id: 1}
|
||||
_, err := Create(tf, "testfile", 100, ta)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Check the file was created correctly
|
||||
file := &File{ID: 2}
|
||||
err = file.LoadFileMetaByID()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, int64(1), file.CreatedByID)
|
||||
assert.Equal(t, "testfile", file.Name)
|
||||
assert.Equal(t, int64(100), file.Size)
|
||||
// Check the file was created correctly
|
||||
file := &File{ID: 2}
|
||||
err = file.LoadFileMetaByID()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, int64(1), file.CreatedByID)
|
||||
assert.Equal(t, "testfile", file.Name)
|
||||
assert.Equal(t, int64(100), file.Size)
|
||||
|
||||
})
|
||||
t.Run("Too Large", func(t *testing.T) {
|
||||
initFixtures(t)
|
||||
tf := &testfile{
|
||||
content: []byte("testfile"),
|
||||
}
|
||||
ta := &testauth{id: 1}
|
||||
_, err := Create(tf, "testfile", 99999999999, ta)
|
||||
assert.Error(t, err)
|
||||
assert.True(t, IsErrFileIsTooLarge(err))
|
||||
})
|
||||
}
|
||||
|
||||
func TestFile_Delete(t *testing.T) {
|
||||
|
|
Loading…
Reference in New Issue