2019-02-02 16:18:25 +08:00
|
|
|
// Copyright 2017-2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2018-10-22 13:19:06 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-10-22 13:19:06 +08:00
|
|
|
|
|
|
|
package gfile
|
|
|
|
|
|
|
|
import (
|
2019-06-19 09:06:52 +08:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2018-10-22 13:19:06 +08:00
|
|
|
)
|
|
|
|
|
2019-05-24 19:59:08 +08:00
|
|
|
// Size returns the size of file specified by <path> in byte.
|
2018-10-22 13:19:06 +08:00
|
|
|
func Size(path string) int64 {
|
2019-06-19 09:06:52 +08:00
|
|
|
s, e := os.Stat(path)
|
|
|
|
if e != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return s.Size()
|
2018-10-22 13:19:06 +08:00
|
|
|
}
|
|
|
|
|
2019-05-24 19:59:08 +08:00
|
|
|
// ReadableSize formats size of file given by <path>, for more human readable.
|
2018-10-22 13:19:06 +08:00
|
|
|
func ReadableSize(path string) string {
|
2019-07-24 22:48:43 +08:00
|
|
|
return FormatSize(Size(path))
|
2018-10-22 13:19:06 +08:00
|
|
|
}
|
|
|
|
|
2019-05-24 19:59:08 +08:00
|
|
|
// FormatSize formats size <raw> for more human readable.
|
2019-07-24 22:48:43 +08:00
|
|
|
func FormatSize(raw int64) string {
|
|
|
|
var r float64 = float64(raw)
|
2019-06-19 09:06:52 +08:00
|
|
|
var t float64 = 1024
|
|
|
|
var d float64 = 1
|
2018-10-22 13:19:06 +08:00
|
|
|
|
2019-07-24 22:48:43 +08:00
|
|
|
if r < t {
|
|
|
|
return fmt.Sprintf("%.2fB", r/d)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2018-10-22 13:19:06 +08:00
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
d *= 1024
|
|
|
|
t *= 1024
|
2018-10-22 13:19:06 +08:00
|
|
|
|
2019-07-24 22:48:43 +08:00
|
|
|
if r < t {
|
|
|
|
return fmt.Sprintf("%.2fK", r/d)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2018-10-22 13:19:06 +08:00
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
d *= 1024
|
|
|
|
t *= 1024
|
2018-10-22 13:19:06 +08:00
|
|
|
|
2019-07-24 22:48:43 +08:00
|
|
|
if r < t {
|
|
|
|
return fmt.Sprintf("%.2fM", r/d)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2018-10-22 13:19:06 +08:00
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
d *= 1024
|
|
|
|
t *= 1024
|
2018-10-22 13:19:06 +08:00
|
|
|
|
2019-07-24 22:48:43 +08:00
|
|
|
if r < t {
|
|
|
|
return fmt.Sprintf("%.2fG", r/d)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2018-10-22 13:19:06 +08:00
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
d *= 1024
|
|
|
|
t *= 1024
|
2018-10-22 13:19:06 +08:00
|
|
|
|
2019-07-24 22:48:43 +08:00
|
|
|
if r < t {
|
|
|
|
return fmt.Sprintf("%.2fT", r/d)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2018-10-22 13:19:06 +08:00
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
d *= 1024
|
|
|
|
t *= 1024
|
2018-10-22 13:19:06 +08:00
|
|
|
|
2019-07-24 22:48:43 +08:00
|
|
|
if r < t {
|
|
|
|
return fmt.Sprintf("%.2fP", r/d)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2018-10-22 13:19:06 +08:00
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
return "TooLarge"
|
|
|
|
}
|