1
0

Fixed error when setting max file size on 32-Bit systems

This commit is contained in:
kolaente
2019-10-18 17:30:25 +02:00
parent 2169464983
commit b81cd6128a
19 changed files with 369 additions and 16 deletions

View File

@ -197,7 +197,7 @@ func InitDefaultConfig() {
RateLimitStore.setDefault("memory")
// Files
FilesBasePath.setDefault("files")
FilesMaxSize.setDefault(21474836480) // 20 MB
FilesMaxSize.setDefault("20MB")
}
// InitConfig initializes the config, sets defaults etc.

View File

@ -37,7 +37,7 @@ func IsErrFileDoesNotExist(err error) bool {
// ErrFileIsTooLarge defines an error where a file is larger than the configured limit
type ErrFileIsTooLarge struct {
Size int64
Size uint64
}
// Error is the error implementation of ErrFileIsTooLarge

View File

@ -19,6 +19,7 @@ package files
import (
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/web"
"github.com/c2h5oh/datasize"
"github.com/spf13/afero"
"io"
"strconv"
@ -30,7 +31,7 @@ type File struct {
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"`
Name string `xorm:"text not null" json:"name"`
Mime string `xorm:"text null" json:"mime"`
Size int64 `xorm:"int(11) not null" json:"size"`
Size uint64 `xorm:"int(11) not null" json:"size"`
Created time.Time `xorm:"-" json:"created"`
@ -66,9 +67,15 @@ 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) {
func Create(f io.ReadCloser, realname string, realsize uint64, a web.Auth) (file *File, err error) {
if realsize > config.FilesMaxSize.GetInt64() {
// Get and parse the configured file size
var maxSize datasize.ByteSize
err = maxSize.UnmarshalText([]byte(config.FilesMaxSize.GetString()))
if err != nil {
return nil, err
}
if realsize > maxSize.Bytes() {
return nil, ErrFileIsTooLarge{Size: realsize}
}

View File

@ -66,7 +66,7 @@ func TestCreate(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, int64(1), file.CreatedByID)
assert.Equal(t, "testfile", file.Name)
assert.Equal(t, int64(100), file.Size)
assert.Equal(t, uint64(100), file.Size)
})
t.Run("Too Large", func(t *testing.T) {

View File

@ -763,7 +763,7 @@ func (err ErrTaskAttachmentDoesNotExist) HTTPError() web.HTTPError {
// ErrTaskAttachmentIsTooLarge represents an error where the user tries to relate a task with itself
type ErrTaskAttachmentIsTooLarge struct {
Size int64
Size uint64
}
// IsErrTaskAttachmentIsTooLarge checks if an error is ErrTaskAttachmentIsTooLarge.

View File

@ -47,7 +47,7 @@ func (TaskAttachment) TableName() string {
// NewAttachment creates a new task attachment
// Note: I'm not sure if only accepting an io.ReadCloser and not an afero.File or os.File instead is a good way of doing things.
func (ta *TaskAttachment) NewAttachment(f io.ReadCloser, realname string, realsize int64, a web.Auth) error {
func (ta *TaskAttachment) NewAttachment(f io.ReadCloser, realname string, realsize uint64, a web.Auth) error {
// Store the file
file, err := files.Create(f, realname, realsize, a)

View File

@ -111,7 +111,7 @@ func TestTaskAttachment_NewAttachment(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, testuser.ID, ta.File.CreatedByID)
assert.Equal(t, "testfile", ta.File.Name)
assert.Equal(t, int64(100), ta.File.Size)
assert.Equal(t, uint64(100), ta.File.Size)
// Extra test for max size test
}

View File

@ -28,7 +28,7 @@ type vikunjaInfos struct {
FrontendURL string `json:"frontend_url"`
Motd string `json:"motd"`
LinkSharingEnabled bool `json:"link_sharing_enabled"`
MaxFileSize int64 `json:"max_file_size"`
MaxFileSize string `json:"max_file_size"`
}
// Info is the handler to get infos about this vikunja instance
@ -44,6 +44,6 @@ func Info(c echo.Context) error {
FrontendURL: config.ServiceFrontendurl.GetString(),
Motd: config.ServiceMotd.GetString(),
LinkSharingEnabled: config.ServiceEnableLinkSharing.GetBool(),
MaxFileSize: config.FilesMaxSize.GetInt64(),
MaxFileSize: config.FilesMaxSize.GetString(),
})
}

View File

@ -82,7 +82,7 @@ func UploadTaskAttachment(c echo.Context) error {
}
defer f.Close()
err = ta.NewAttachment(f, file.Filename, file.Size, user)
err = ta.NewAttachment(f, file.Filename, uint64(file.Size), user)
if err != nil {
r.Errors = append(r.Errors, handler.HandleHTTPError(err, c))
continue