mirror of
				https://github.com/go-gitea/gitea
				synced 2025-11-03 21:08:25 +00:00 
			
		
		
		
	UI: Detect and restore encoding and BOM in content (#6727)
* detect and remove a decoded BOM Signed-off-by: Andrew Thornton <art27@cantab.net> * Restore the previous encoding and BOM * On error keep as UTF-8 Signed-off-by: Andrew Thornton <art27@cantab.net> * create remove BOM function * Deal with LFSed content * Update modules/repofiles/update.go * Fix final LFS bug * Keep LFS sections referring to opts.Content
This commit is contained in:
		@@ -5,6 +5,7 @@
 | 
			
		||||
package base
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"crypto/md5"
 | 
			
		||||
	"crypto/rand"
 | 
			
		||||
	"crypto/sha1"
 | 
			
		||||
@@ -36,6 +37,9 @@ import (
 | 
			
		||||
	"github.com/gogits/chardet"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// UTF8BOM is the utf-8 byte-order marker
 | 
			
		||||
var UTF8BOM = []byte{'\xef', '\xbb', '\xbf'}
 | 
			
		||||
 | 
			
		||||
// EncodeMD5 encodes string to md5 hex value.
 | 
			
		||||
func EncodeMD5(str string) string {
 | 
			
		||||
	m := md5.New()
 | 
			
		||||
@@ -91,6 +95,14 @@ func DetectEncoding(content []byte) (string, error) {
 | 
			
		||||
	return result.Charset, err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// RemoveBOMIfPresent removes a UTF-8 BOM from a []byte
 | 
			
		||||
func RemoveBOMIfPresent(content []byte) []byte {
 | 
			
		||||
	if len(content) > 2 && bytes.Equal(content[0:3], UTF8BOM) {
 | 
			
		||||
		return content[3:]
 | 
			
		||||
	}
 | 
			
		||||
	return content
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// BasicAuthDecode decode basic auth string
 | 
			
		||||
func BasicAuthDecode(encoded string) (string, string, error) {
 | 
			
		||||
	s, err := base64.StdEncoding.DecodeString(encoded)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user