github.com/blend/go-sdk@v1.20220411.3/fileutil/format_file_size.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 fileutil
     9  
    10  import "strconv"
    11  
    12  const (
    13  	// Kilobyte represents the bytes in a kilobyte.
    14  	Kilobyte int64 = 1 << 10
    15  	// Megabyte represents the bytes in a megabyte.
    16  	Megabyte int64 = Kilobyte << 10
    17  	// Gigabyte represents the bytes in a gigabyte.
    18  	Gigabyte int64 = Megabyte << 10
    19  	// Terabyte represents the bytes in a terabyte.
    20  	Terabyte int64 = Gigabyte << 10
    21  )
    22  
    23  // FormatFileSize returns a string representation of a file size in bytes.
    24  func FormatFileSize(sizeBytes int64) string {
    25  	switch {
    26  	case sizeBytes >= 1<<40:
    27  		return strconv.FormatInt(sizeBytes/Terabyte, 10) + "tb"
    28  	case sizeBytes >= 1<<30:
    29  		return strconv.FormatInt(sizeBytes/Gigabyte, 10) + "gb"
    30  	case sizeBytes >= 1<<20:
    31  		return strconv.FormatInt(sizeBytes/Megabyte, 10) + "mb"
    32  	case sizeBytes >= 1<<10:
    33  		return strconv.FormatInt(sizeBytes/Kilobyte, 10) + "kb"
    34  	}
    35  	return strconv.FormatInt(sizeBytes, 10)
    36  }