From 32027c2d4392fb6964bafe3aaf6d427e5d827d21 Mon Sep 17 00:00:00 2001 From: Sam Truzjan Date: Thu, 28 Nov 2013 06:42:10 +0400 Subject: Hide InfoHash constructor --- src/Data/Torrent.hs | 2 +- src/Data/Torrent/InfoHash.hs | 15 ++++++--------- src/Data/Torrent/Layout.hs | 4 ++-- 3 files changed, 9 insertions(+), 12 deletions(-) (limited to 'src/Data') diff --git a/src/Data/Torrent.hs b/src/Data/Torrent.hs index 59f11814..8411dc97 100644 --- a/src/Data/Torrent.hs +++ b/src/Data/Torrent.hs @@ -142,7 +142,7 @@ infoDictionary :: LayoutInfo -> PieceInfo -> Bool -> InfoDict infoDictionary li pinfo private = InfoDict ih li pinfo private where ih = IH.hashlazy $ encode $ InfoDict fake_ih li pinfo private - fake_ih = InfoHash "" + fake_ih = "0123456789012345678901234567890123456789" getPrivate :: Get Bool getPrivate = (Just True ==) <$>? "private" diff --git a/src/Data/Torrent/InfoHash.hs b/src/Data/Torrent/InfoHash.hs index a4d6a02e..fd57700a 100644 --- a/src/Data/Torrent/InfoHash.hs +++ b/src/Data/Torrent/InfoHash.hs @@ -10,8 +10,7 @@ {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} module Data.Torrent.InfoHash - ( -- * Info hash - InfoHash(..) + ( InfoHash -- * Parsing , byteStringToInfoHash @@ -42,6 +41,7 @@ import qualified Data.ByteString.Lazy.Builder as B import qualified Data.ByteString.Lazy.Builder.ASCII as B import Data.Char import Data.List as L +import Data.Maybe import Data.Hashable as Hashable import Data.URLEncoded as URL import Data.Serialize @@ -85,14 +85,11 @@ instance Read InfoHash where pair (a : b : xs) = (a, b) : pair xs pair _ = [] --- | for base16 (hex) encoded strings +-- | for base16/base32 encoded strings instance IsString InfoHash where - fromString str - | L.length str == 40 - , (ihStr, inv) <- Base16.decode $ BC.pack str - = if BS.length inv == 0 then InfoHash ihStr - else error "fromString: invalid infohash string" - | otherwise = error "fromString: invalid infohash string length" + fromString str = fromMaybe err $ textToInfoHash $ T.pack str + where + err = error $ "fromString: invalid infohash string" ++ str instance Hashable InfoHash where hash = Hashable.hash . getInfoHash diff --git a/src/Data/Torrent/Layout.hs b/src/Data/Torrent/Layout.hs index 70908c4e..8f35772d 100644 --- a/src/Data/Torrent/Layout.hs +++ b/src/Data/Torrent/Layout.hs @@ -68,6 +68,7 @@ import Data.Aeson.Types (FromJSON, ToJSON) import Data.BEncode import Data.BEncode.Types import Data.ByteString as BS +import Data.ByteString.Base16 as Base16 import Data.ByteString.Char8 as BC import Data.Char as Char import Data.Foldable as F @@ -81,7 +82,6 @@ import System.FilePath import System.Posix.Types import Data.Torrent.Block -import Data.Torrent.InfoHash {----------------------------------------------------------------------- @@ -180,7 +180,7 @@ instance Pretty (FileInfo BS.ByteString) where $$ "Size: " <> text (show fiLength) $$ maybe PP.empty ppMD5 fiMD5Sum where - ppMD5 md5 = "MD5 : " <> text (show (InfoHash md5)) + ppMD5 md5 = "MD5 : " <> text (show (Base16.encode md5)) -- | Join file path. joinFilePath :: FileInfo [ByteString] -> FileInfo ByteString -- cgit v1.2.3