-
Notifications
You must be signed in to change notification settings - Fork 47
/
parse_file_size.go
49 lines (42 loc) · 993 Bytes
/
parse_file_size.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
Copyright (c) 2021 - Present. Blend Labs, Inc. All rights reserved
Use of this source code is governed by a MIT license that can be found in the LICENSE file.
*/
package fileutil
import (
"strconv"
"strings"
)
// ParseFileSize parses a file size
func ParseFileSize(fileSizeValue string) (int64, error) {
if len(fileSizeValue) == 0 {
return 0, nil
}
if len(fileSizeValue) < 2 {
val, err := strconv.Atoi(fileSizeValue)
if err != nil {
return 0, err
}
return int64(val), nil
}
units := strings.ToLower(fileSizeValue[len(fileSizeValue)-2:])
value, err := strconv.ParseInt(fileSizeValue[:len(fileSizeValue)-2], 10, 64)
if err != nil {
return 0, err
}
switch units {
case "tb":
return value * Terrabyte, nil
case "gb":
return value * Gigabyte, nil
case "mb":
return value * Megabyte, nil
case "kb":
return value * Kilobyte, nil
}
fullValue, err := strconv.ParseInt(fileSizeValue, 10, 64)
if err != nil {
return 0, err
}
return fullValue, nil
}