github.com/blend/go-sdk@v1.20220411.3/stringutil/filesize.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package stringutil
     9  
    10  import "fmt"
    11  
    12  // FileSize returns a string representation of a file size in bytes.
    13  func FileSize(sizeBytes int) string {
    14  	if sizeBytes >= 1<<30 {
    15  		return fmt.Sprintf("%dgB", sizeBytes/(1<<30))
    16  	} else if sizeBytes >= 1<<20 {
    17  		return fmt.Sprintf("%dmB", sizeBytes/(1<<20))
    18  	} else if sizeBytes >= 1<<10 {
    19  		return fmt.Sprintf("%dkB", sizeBytes/(1<<10))
    20  	}
    21  	return fmt.Sprintf("%dB", sizeBytes)
    22  }