github.com/blend/go-sdk@v1.20220411.3/fileutil/parse_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 ( 11 "strconv" 12 "strings" 13 ) 14 15 // ParseFileSize parses a file size 16 func ParseFileSize(fileSizeValue string) (int64, error) { 17 if len(fileSizeValue) == 0 { 18 return 0, nil 19 } 20 21 if len(fileSizeValue) < 2 { 22 val, err := strconv.Atoi(fileSizeValue) 23 if err != nil { 24 return 0, err 25 } 26 return int64(val), nil 27 } 28 29 units := strings.ToLower(fileSizeValue[len(fileSizeValue)-2:]) 30 value, err := strconv.ParseInt(fileSizeValue[:len(fileSizeValue)-2], 10, 64) 31 if err != nil { 32 return 0, err 33 } 34 switch units { 35 case "tb": 36 return value * Terabyte, nil 37 case "gb": 38 return value * Gigabyte, nil 39 case "mb": 40 return value * Megabyte, nil 41 case "kb": 42 return value * Kilobyte, nil 43 } 44 fullValue, err := strconv.ParseInt(fileSizeValue, 10, 64) 45 if err != nil { 46 return 0, err 47 } 48 return fullValue, nil 49 }