diff --git a/modules/setting/common_mime.go b/modules/setting/common_mime.go new file mode 100644 index 0000000000000..99602bc784907 --- /dev/null +++ b/modules/setting/common_mime.go @@ -0,0 +1,92 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package setting + +import ( + "strings" +) + +// https://s-randomfiles.s3.amazonaws.com/mime/allMimeTypes.txt + +var commonMimeTypes = []struct { + names []string + types []string +}{ + {[]string{ + "zipped", + "zip", + "compressed", + "packed", + }, []string{ + "application/zip", + "application/x-zip-compressed", + "multipart/x-zip", + "application/gzip", + "application/x-gzip", + "application/x-gzip-compressed", + "multipart/gzip", + "application/x-compressed-tar", + "application/x-gtar", + "application/x-tgz", + "application/rar", + "application/x-rar-compressed", + "application/x-7z-compressed", + "application/x-compress", + }}, + {[]string{ + "image", + "picture", + }, []string{ + "image/jpeg", + "image/png", + "image/apng", + "image/bmp", + "image/gif", + }}, + {[]string{ + "txt", + "text", + }, []string{ + "text/plain", + }}, + {[]string{ + "pdf", + }, []string{ + "application/pdf", + }}, +} + +func expandCommonMimeTypesString(typeList string) string { + return strings.Join(expandCommonMimeTypes(strings.Split(typeList, ",")), ",") +} + +func expandCommonMimeTypes(types []string) []string { + list := make([]string, 0, len(types)) +individuals: + for _, name := range types { + if !strings.Contains(name, "/") { + normalized := strings.TrimSpace(strings.ToLower(name)) + for _, candidate := range commonMimeTypes { + if isStringInSlice(normalized, candidate.names) { + list = append(list, candidate.types...) + continue individuals + } + } + } else { + list = append(list, name) + } + } + return list +} + +// Can't import 'util' because it's a circular reference +func isStringInSlice(target string, slice []string) bool { + for i := 0; i < len(slice); i++ { + if slice[i] == target { + return true + } + } + return false +} diff --git a/modules/setting/repository.go b/modules/setting/repository.go index 8af3eaaf46933..89951921170ef 100644 --- a/modules/setting/repository.go +++ b/modules/setting/repository.go @@ -222,6 +222,8 @@ func newRepository() { log.Fatal("Failed to map Repository.PullRequest settings: %v", err) } + Repository.Upload.AllowedTypes = expandCommonMimeTypes(Repository.Upload.AllowedTypes) + if !filepath.IsAbs(Repository.Upload.TempPath) { Repository.Upload.TempPath = path.Join(AppWorkPath, Repository.Upload.TempPath) } diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 714015c47589d..a7f7d96a8e4e7 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -827,7 +827,7 @@ func NewContext() { if !filepath.IsAbs(AttachmentPath) { AttachmentPath = path.Join(AppWorkPath, AttachmentPath) } - AttachmentAllowedTypes = strings.Replace(sec.Key("ALLOWED_TYPES").MustString("image/jpeg,image/png,application/zip,application/gzip"), "|", ",", -1) + AttachmentAllowedTypes = expandCommonMimeTypesString(strings.Replace(sec.Key("ALLOWED_TYPES").MustString("image/jpeg,image/png,application/zip,application/gzip"), "|", ",", -1)) AttachmentMaxSize = sec.Key("MAX_SIZE").MustInt64(4) AttachmentMaxFiles = sec.Key("MAX_FILES").MustInt(5) AttachmentEnabled = sec.Key("ENABLED").MustBool(true)