summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2013-11-06 18:11:03 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2013-11-06 18:11:03 +0400
commit5ffd1432dc947175787a6b616a673c9c3d49ffb8 (patch)
tree1547ae15db2feda6e6e6dfe81a3889e5f96ad68c /src
parentc2a8c8338a63001e03e65dec1a5b81fe33bdba37 (diff)
Add smart constructors for magnets
Diffstat (limited to 'src')
-rw-r--r--src/Data/Torrent/Layout.hs9
-rw-r--r--src/Data/Torrent/Magnet.hs29
2 files changed, 36 insertions, 2 deletions
diff --git a/src/Data/Torrent/Layout.hs b/src/Data/Torrent/Layout.hs
index ea8fa894..4be108c2 100644
--- a/src/Data/Torrent/Layout.hs
+++ b/src/Data/Torrent/Layout.hs
@@ -39,7 +39,8 @@ module Data.Torrent.Layout
39 , isSingleFile 39 , isSingleFile
40 , isMultiFile 40 , isMultiFile
41 41
42 -- ** Folds 42 -- ** Query
43 , suggestedName
43 , contentLength 44 , contentLength
44 , fileCount 45 , fileCount
45 , blockCount 46 , blockCount
@@ -227,6 +228,12 @@ isMultiFile MultiFile {} = True
227isMultiFile _ = False 228isMultiFile _ = False
228{-# INLINE isMultiFile #-} 229{-# INLINE isMultiFile #-}
229 230
231-- | Get name of the torrent based on the root path piece.
232suggestedName :: LayoutInfo -> ByteString
233suggestedName (SingleFile FileInfo {..}) = fiName
234suggestedName MultiFile {..} = liDirName
235{-# INLINE suggestedName #-}
236
230-- | Find sum of sizes of the all torrent files. 237-- | Find sum of sizes of the all torrent files.
231contentLength :: LayoutInfo -> FileSize 238contentLength :: LayoutInfo -> FileSize
232contentLength SingleFile { liFile = FileInfo {..} } = fiLength 239contentLength SingleFile { liFile = FileInfo {..} } = fiLength
diff --git a/src/Data/Torrent/Magnet.hs b/src/Data/Torrent/Magnet.hs
index 34a7bbc5..01f28a76 100644
--- a/src/Data/Torrent/Magnet.hs
+++ b/src/Data/Torrent/Magnet.hs
@@ -13,10 +13,17 @@
13-- Bittorrent specific info: 13-- Bittorrent specific info:
14-- <http://www.bittorrent.org/beps/bep_0009.html> 14-- <http://www.bittorrent.org/beps/bep_0009.html>
15-- 15--
16{-# LANGUAGE NamedFieldPuns #-}
16module Data.Torrent.Magnet 17module Data.Torrent.Magnet
17 ( -- * Magnet 18 ( -- * Magnet
18 Magnet(..) 19 Magnet(..)
20
21 -- * Construction
19 , nullMagnet 22 , nullMagnet
23 , simpleMagnet
24 , detailedMagnet
25
26 -- * Conversion
20 , parseMagnet 27 , parseMagnet
21 , renderMagnet 28 , renderMagnet
22 29
@@ -33,11 +40,13 @@ import Data.List as L
33import Data.URLEncoded as URL 40import Data.URLEncoded as URL
34import Data.String 41import Data.String
35import Data.Text as T 42import Data.Text as T
43import Data.Text.Encoding as T
36import Network.URI 44import Network.URI
37import Text.Read 45import Text.Read
38 46
47import Data.Torrent
39import Data.Torrent.InfoHash 48import Data.Torrent.InfoHash
40 49import Data.Torrent.Layout
41 50
42{----------------------------------------------------------------------- 51{-----------------------------------------------------------------------
43-- URN 52-- URN
@@ -153,6 +162,24 @@ nullMagnet u = Magnet
153 , supplement = M.empty 162 , supplement = M.empty
154 } 163 }
155 164
165-- | A simple magnet link including infohash ('xt' param) and display
166-- name ('dn' param).
167--
168simpleMagnet :: Torrent -> Magnet
169simpleMagnet Torrent {tInfoDict = InfoDict {..}}
170 = (nullMagnet idInfoHash)
171 { displayName = Just $ T.decodeUtf8 $ suggestedName idLayoutInfo
172 }
173
174-- | Like 'simpleMagnet' but also include exactLength ('xl' param) and
175-- tracker ('tr' param).
176detailedMagnet :: Torrent -> Magnet
177detailedMagnet t @ Torrent {tInfoDict = InfoDict {..}, tAnnounce}
178 = (simpleMagnet t)
179 { exactLength = Just $ fromIntegral $ contentLength idLayoutInfo
180 , tracker = Just tAnnounce
181 }
182
156fromQuery :: URLEncoded -> Either String Magnet 183fromQuery :: URLEncoded -> Either String Magnet
157fromQuery q 184fromQuery q
158 | Just urnStr <- URL.lookup ("xt" :: String) q 185 | Just urnStr <- URL.lookup ("xt" :: String) q