cmd/htpacker: don't try to compress tiny files
If we have some really tiny files, it's not worth compressing them. Among other things, this will work around a bug in go-zopfli for 0- or 1-byte files.
This commit is contained in:
		
							parent
							
								
									2b280de481
								
							
						
					
					
						commit
						439bf2422b
					
				| 
						 | 
					@ -64,6 +64,11 @@ func (ignoreProgress) Begin(_, _ string) {}
 | 
				
			||||||
func (ignoreProgress) End(_, _ string)   {}
 | 
					func (ignoreProgress) End(_, _ string)   {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const (
 | 
					const (
 | 
				
			||||||
 | 
						// minCompressionFileSize is the minimum filesize we need before
 | 
				
			||||||
 | 
						// considering compression. Note this must be at least 2, to avoid
 | 
				
			||||||
 | 
						// known bugs in go-zopfli.
 | 
				
			||||||
 | 
						minCompressionFileSize = 128
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// minCompressionSaving means we'll only use the compressed version of
 | 
						// minCompressionSaving means we'll only use the compressed version of
 | 
				
			||||||
	// the file if it's at least this many bytes smaller than the original.
 | 
						// the file if it's at least this many bytes smaller than the original.
 | 
				
			||||||
	// Chosen somewhat arbitrarily; we have to add an HTTP header, and the
 | 
						// Chosen somewhat arbitrarily; we have to add an HTTP header, and the
 | 
				
			||||||
| 
						 | 
					@ -433,6 +438,10 @@ func (p *packer) Uncompressed(srcPath string, dir *packed.File) error {
 | 
				
			||||||
// Gzip will gzip input data to a temporary file, and then append that to the
 | 
					// Gzip will gzip input data to a temporary file, and then append that to the
 | 
				
			||||||
// output file.
 | 
					// output file.
 | 
				
			||||||
func (p *packer) Gzip(data []byte, dir *packed.File) error {
 | 
					func (p *packer) Gzip(data []byte, dir *packed.File) error {
 | 
				
			||||||
 | 
						if len(data) < minCompressionFileSize {
 | 
				
			||||||
 | 
							return nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// write via temporary file
 | 
						// write via temporary file
 | 
				
			||||||
	tmpfile, err := ioutil.TempFile("", "")
 | 
						tmpfile, err := ioutil.TempFile("", "")
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
| 
						 | 
					@ -474,6 +483,9 @@ func (p *packer) Gzip(data []byte, dir *packed.File) error {
 | 
				
			||||||
// Brotli will compress input data to a temporary file, and then append that to
 | 
					// Brotli will compress input data to a temporary file, and then append that to
 | 
				
			||||||
// the output file.
 | 
					// the output file.
 | 
				
			||||||
func (p *packer) Brotli(data []byte, dir *packed.File) error {
 | 
					func (p *packer) Brotli(data []byte, dir *packed.File) error {
 | 
				
			||||||
 | 
						if len(data) < minCompressionFileSize {
 | 
				
			||||||
 | 
							return nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	// write via temporary file
 | 
						// write via temporary file
 | 
				
			||||||
	tmpfile, err := ioutil.TempFile("", "")
 | 
						tmpfile, err := ioutil.TempFile("", "")
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue