mirror of
				https://github.com/go-gitea/gitea
				synced 2025-10-31 03:18:24 +00:00 
			
		
		
		
	Detect charset and convert non UTF-8 files for display (#4950)
* Detect charset and convert non UTF-8 files for display * Refactor and move function to correct module * Revert unrelated changes * More unrelated changes * Duplicate content for small text to have better encoding detection * Check if original content is valid before duplicating it
This commit is contained in:
		| @@ -59,7 +59,22 @@ func DetectEncoding(content []byte) (string, error) { | ||||
| 		return "UTF-8", nil | ||||
| 	} | ||||
|  | ||||
| 	result, err := chardet.NewTextDetector().DetectBest(content) | ||||
| 	textDetector := chardet.NewTextDetector() | ||||
| 	var detectContent []byte | ||||
| 	if len(content) < 1024 { | ||||
| 		// Check if original content is valid | ||||
| 		if _, err := textDetector.DetectBest(content); err != nil { | ||||
| 			return "", err | ||||
| 		} | ||||
| 		times := 1024 / len(content) | ||||
| 		detectContent = make([]byte, 0, times*len(content)) | ||||
| 		for i := 0; i < times; i++ { | ||||
| 			detectContent = append(detectContent, content...) | ||||
| 		} | ||||
| 	} else { | ||||
| 		detectContent = content | ||||
| 	} | ||||
| 	result, err := textDetector.DetectBest(detectContent) | ||||
| 	if err != nil { | ||||
| 		return "", err | ||||
| 	} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user