diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2013-11-06 18:11:03 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2013-11-06 18:11:03 +0400 |
commit | 5ffd1432dc947175787a6b616a673c9c3d49ffb8 (patch) | |
tree | 1547ae15db2feda6e6e6dfe81a3889e5f96ad68c | |
parent | c2a8c8338a63001e03e65dec1a5b81fe33bdba37 (diff) |
Add smart constructors for magnets
-rw-r--r-- | src/Data/Torrent/Layout.hs | 9 | ||||
-rw-r--r-- | src/Data/Torrent/Magnet.hs | 29 |
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 | |||
227 | isMultiFile _ = False | 228 | isMultiFile _ = False |
228 | {-# INLINE isMultiFile #-} | 229 | {-# INLINE isMultiFile #-} |
229 | 230 | ||
231 | -- | Get name of the torrent based on the root path piece. | ||
232 | suggestedName :: LayoutInfo -> ByteString | ||
233 | suggestedName (SingleFile FileInfo {..}) = fiName | ||
234 | suggestedName 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. |
231 | contentLength :: LayoutInfo -> FileSize | 238 | contentLength :: LayoutInfo -> FileSize |
232 | contentLength SingleFile { liFile = FileInfo {..} } = fiLength | 239 | contentLength 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 #-} | ||
16 | module Data.Torrent.Magnet | 17 | module 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 | |||
33 | import Data.URLEncoded as URL | 40 | import Data.URLEncoded as URL |
34 | import Data.String | 41 | import Data.String |
35 | import Data.Text as T | 42 | import Data.Text as T |
43 | import Data.Text.Encoding as T | ||
36 | import Network.URI | 44 | import Network.URI |
37 | import Text.Read | 45 | import Text.Read |
38 | 46 | ||
47 | import Data.Torrent | ||
39 | import Data.Torrent.InfoHash | 48 | import Data.Torrent.InfoHash |
40 | 49 | import 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 | -- | ||
168 | simpleMagnet :: Torrent -> Magnet | ||
169 | simpleMagnet 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). | ||
176 | detailedMagnet :: Torrent -> Magnet | ||
177 | detailedMagnet t @ Torrent {tInfoDict = InfoDict {..}, tAnnounce} | ||
178 | = (simpleMagnet t) | ||
179 | { exactLength = Just $ fromIntegral $ contentLength idLayoutInfo | ||
180 | , tracker = Just tAnnounce | ||
181 | } | ||
182 | |||
156 | fromQuery :: URLEncoded -> Either String Magnet | 183 | fromQuery :: URLEncoded -> Either String Magnet |
157 | fromQuery q | 184 | fromQuery q |
158 | | Just urnStr <- URL.lookup ("xt" :: String) q | 185 | | Just urnStr <- URL.lookup ("xt" :: String) q |