} deriving (Eq, Show)
bvalToInfo :: BVal -> Maybe Info
-bvalToInfo (Bdict m) = let (Bint pieceLength') = m ! "piece length"
- (Bstr pieces') = m ! "pieces"
- private' = Nothing
- (Bstr name') = m ! "name"
- -- is the key "files" present? If so, it is a multi-file torrent
- -- if not, it is a single file torrent.
- filesIfMulti = lookup "files" m
- partialInfo = Info { pieceLength = pieceLength'
- , pieces = pieces'
- , private = private'
- , name = unpack name'
- , filemeta = []
- }
- in
- case filesIfMulti of
- Nothing -> let (Bint length') = m ! "length"
- filemeta' = FileMeta { lengthInBytes = length'
- , md5sum = Nothing
- , path = unpack name' }
- in Just (partialInfo { filemeta = [filemeta'] })
- Just (Blist files) -> mapM toFileMeta files >>=
- \filemeta' ->
- Just partialInfo { filemeta = filemeta' }
+bvalToInfo (Bdict minfo) = let (Bint pieceLength') = minfo ! "piece length"
+ (Bstr pieces') = minfo ! "pieces"
+ private' = Nothing
+ (Bstr name') = minfo ! "name"
+ -- is the key "files" present? If so, it is a multi-file torrent
+ -- if not, it is a single file torrent.
+ filesIfMulti = lookup "files" minfo
+ partialInfo = Info { pieceLength = pieceLength'
+ , pieces = pieces'
+ , private = private'
+ , name = unpack name'
+ , filemeta = []
+ }
+ in
+ case filesIfMulti of
+ Nothing -> let (Bint length') = minfo ! "length"
+ filemeta' = FileMeta { lengthInBytes = length'
+ , md5sum = Nothing
+ , path = unpack name' }
+ in Just (partialInfo { filemeta = [filemeta'] })
+ Just (Blist files) -> mapM toFileMeta files >>=
+ \filemeta' ->
+ Just partialInfo { filemeta = filemeta' }
+ Just _ -> Nothing
bvalToInfo _ = Nothing
toFileMeta :: BVal -> Maybe FileMeta
-toFileMeta (Bdict fm) = let (Bint length) = fm ! "length"
+toFileMeta (Bdict fm) = let (Bint length') = fm ! "length"
(Blist pathElems) = fm ! "path"
pathStrings = fmap bstrToString pathElems
in
sequence pathStrings >>=
\pathList -> let path' = concat $ intersperse "/" pathList
- in Just (FileMeta { lengthInBytes = length
+ in Just (FileMeta { lengthInBytes = length'
, md5sum = Nothing
, path = path' })
toFileMeta _ = Nothing
mkMetaInfo :: BVal -> Either String Metainfo
-mkMetaInfo (Bdict m) =
- let info' = bvalToInfo $ m ! "info"
- announce' = lookup "announce" m
- announceList' = lookup "announce-list" m
- creationDate' = lookup "creation date" m
- comment' = lookup "comment" m
- createdBy' = lookup "created by" m
- encoding' = lookup "encoding" m
+mkMetaInfo (Bdict minfo) =
+ let info' = bvalToInfo $ minfo ! "info"
+ announce' = lookup "announce" minfo
+ announceList' = lookup "announce-list" minfo
+ creationDate' = lookup "creation date" minfo
+ comment' = lookup "comment" minfo
+ createdBy' = lookup "created by" minfo
+ encoding' = lookup "encoding" minfo
in Right Metainfo {
info = info'
, announceList = maybeToList (announce' >>= bstrToString)
, comment = bstrToString =<< comment'
, createdBy = bstrToString =<< createdBy'
, encoding = bstrToString =<< encoding'
- , infoHash = hash . encode $ (m ! "info")
+ , infoHash = hash . encode $ (minfo ! "info")
}
mkMetaInfo _ = Left "mkMetaInfo: expect an input dict"