module System.HrfSize
( determineFileSize,
FileSize(..),
convertSize
) where
import System.IO (FilePath, IOMode (ReadMode), hFileSize,
withBinaryFile)
data FileSize = Bytes Double | KiB Double | MiB Double
| GiB Double | TiB Double
deriving (Show, Eq, Ord)
convertSize :: Double -> FileSize
convertSize size | size < 1024.0 = Bytes $ trunc size
| size < 1024.0 ^ (2 :: Int) =
KiB $ trunc $ size / 1024.0
| size < 1024.0 ^ (3 :: Int) =
MiB $ trunc $ size / 1024.0 ^ (2 :: Int)
| size < 1024.0 ^ (4 :: Int) =
GiB $ trunc $ size / 1024.0 ^ (3 :: Int)
| otherwise =
TiB $ trunc $ size / 1024.0 ^ (4 :: Int)
where
trunc n | length (dropWhile (/= '.') $ show n) > 3 =
read parsePrecision :: Double
| otherwise = n
where
parsePrecision = let (a, b) = span (/= '.') $ show n in
a ++ take 3 b
determineFileSize :: FilePath -> IO FileSize
determineFileSize pathToFile = do
fileInt <- withBinaryFile pathToFile ReadMode hFileSize
let file = fromInteger fileInt :: Double
return $ convertSize file