summaryrefslogtreecommitdiff
path: root/src/Data
diff options
context:
space:
mode:
authorSam T <sta.cs.vsu@gmail.com>2013-04-22 17:19:01 +0400
committerSam T <sta.cs.vsu@gmail.com>2013-04-22 17:19:01 +0400
commit6b50fcd1540053694827119b5207cdc33c9f3ef9 (patch)
tree7bf4cec210c02ad6f12b3c0b43c88484018405b2 /src/Data
parent98275c68cdc6762e1d00cf26b724d5b68fe42ee4 (diff)
~ Many cosmetic fixes, including:
* Fix ty of length * Fix names after name changes. * Rename dataty names to more suitable. TorrentInfo -> ContentInfo, TorrentFile -> FileInfo
Diffstat (limited to 'src/Data')
-rw-r--r--src/Data/Torrent.hs78
1 files changed, 40 insertions, 38 deletions
diff --git a/src/Data/Torrent.hs b/src/Data/Torrent.hs
index 5955dfcc..41b78922 100644
--- a/src/Data/Torrent.hs
+++ b/src/Data/Torrent.hs
@@ -10,12 +10,11 @@
10-- | This module provides torrent metainfo serialization. 10-- | This module provides torrent metainfo serialization.
11module Data.Torrent 11module Data.Torrent
12 ( module Data.Torrent.InfoHash 12 ( module Data.Torrent.InfoHash
13 , Torrent(..), TorrentInfo(..), TorrentFile(..) 13 , Torrent(..), ContentInfo(..), FileInfo(..)
14 , fromFile 14 , fromFile
15 ) where 15 ) where
16 16
17import Control.Applicative 17import Control.Applicative
18import Control.Monad
19import qualified Data.Map as M 18import qualified Data.Map as M
20import Data.ByteString (ByteString) 19import Data.ByteString (ByteString)
21import qualified Data.ByteString as B 20import qualified Data.ByteString as B
@@ -25,6 +24,7 @@ import Data.BEncode
25import Data.Torrent.InfoHash 24import Data.Torrent.InfoHash
26import Network.URI 25import Network.URI
27 26
27
28type Time = Text 28type Time = Text
29 29
30-- TODO more convenient form of torrent info. 30-- TODO more convenient form of torrent info.
@@ -54,7 +54,7 @@ data Torrent = Torrent {
54 -- ^ String encoding format used to generate the pieces part of 54 -- ^ String encoding format used to generate the pieces part of
55 -- the info dictionary in the .torrent metafile. 55 -- the info dictionary in the .torrent metafile.
56 56
57 , tInfo :: TorrentInfo 57 , tInfo :: ContentInfo
58 -- ^ Info about each content file. 58 -- ^ Info about each content file.
59 59
60 , tPublisher :: Maybe URI 60 , tPublisher :: Maybe URI
@@ -69,27 +69,27 @@ data Torrent = Torrent {
69 } deriving Show 69 } deriving Show
70 70
71-- | Info part of the .torrent file contain info about each content file. 71-- | Info part of the .torrent file contain info about each content file.
72data TorrentInfo = 72data ContentInfo =
73 SingleFile { 73 SingleFile {
74 tiLength :: Integer 74 ciLength :: Integer
75 -- ^ Length of the file in bytes. 75 -- ^ Length of the file in bytes.
76 76
77 , tiMD5sum :: Maybe ByteString 77 , ciMD5sum :: Maybe ByteString
78 -- ^ 32 character long MD5 sum of the file. 78 -- ^ 32 character long MD5 sum of the file.
79 -- Used by third-party tools, not by bittorrent protocol itself. 79 -- Used by third-party tools, not by bittorrent protocol itself.
80 80
81 , tiName :: ByteString 81 , ciName :: ByteString
82 -- ^ Suggested name of the file single file. 82 -- ^ Suggested name of the file single file.
83 83
84 84
85 85
86 , tiPieceLength :: Int 86 , ciPieceLength :: Int
87 -- ^ Number of bytes in each piece. 87 -- ^ Number of bytes in each piece.
88 88
89 , tiPieces :: ByteString 89 , ciPieces :: ByteString
90 -- ^ Concatenation of all 20-byte SHA1 hash values. 90 -- ^ Concatenation of all 20-byte SHA1 hash values.
91 91
92 , tiPrivate :: Maybe Bool 92 , ciPrivate :: Maybe Bool
93 -- ^ If set the client MUST publish its presence to get other peers ONLY 93 -- ^ If set the client MUST publish its presence to get other peers ONLY
94 -- via the trackers explicity described in the metainfo file. 94 -- via the trackers explicity described in the metainfo file.
95 -- 95 --
@@ -97,27 +97,28 @@ data TorrentInfo =
97 } 97 }
98 98
99 | MultiFile { 99 | MultiFile {
100 tiFiles :: [TorrentFile] 100 ciFiles :: [FileInfo]
101 -- ^ List of the all files that torrent contains. 101 -- ^ List of the all files that torrent contains.
102 102
103 , tiName :: ByteString 103 , ciName :: ByteString
104 -- | The file path of the directory in which to store all the files. 104 -- | The file path of the directory in which to store all the files.
105 105
106 , tiPieceLength :: Int 106 , ciPieceLength :: Int
107 , tiPieces :: ByteString 107 , ciPieces :: ByteString
108 , tiPrivate :: Maybe Bool 108 , ciPrivate :: Maybe Bool
109 } deriving (Show, Read, Eq) 109 } deriving (Show, Read, Eq)
110 110
111
111-- | Contain info about one single file. 112-- | Contain info about one single file.
112data TorrentFile = TorrentFile { 113data FileInfo = FileInfo {
113 tfLength :: Int 114 fiLength :: Integer
114 -- ^ Length of the file in bytes. 115 -- ^ Length of the file in bytes.
115 116
116 , tfMD5sum :: Maybe ByteString 117 , fiMD5sum :: Maybe ByteString
117 -- ^ 32 character long MD5 sum of the file. 118 -- ^ 32 character long MD5 sum of the file.
118 -- Used by third-party tools, not by bittorrent protocol itself. 119 -- Used by third-party tools, not by bittorrent protocol itself.
119 120
120 , tfPath :: [ByteString] 121 , fiPath :: [ByteString]
121 -- ^ One or more string elements that together represent the path and 122 -- ^ One or more string elements that together represent the path and
122 -- filename. Each element in the list corresponds to either a directory 123 -- filename. Each element in the list corresponds to either a directory
123 -- name or (in the case of the last element) the filename. 124 -- name or (in the case of the last element) the filename.
@@ -125,6 +126,7 @@ data TorrentFile = TorrentFile {
125 -- string elements ["dir1", "dir2", "file.ext"] 126 -- string elements ["dir1", "dir2", "file.ext"]
126 } deriving (Show, Read, Eq) 127 } deriving (Show, Read, Eq)
127 128
129
128instance BEncodable URI where 130instance BEncodable URI where
129 toBEncode uri = toBEncode (BC.pack (uriToString id uri "")) 131 toBEncode uri = toBEncode (BC.pack (uriToString id uri ""))
130 {-# INLINE toBEncode #-} 132 {-# INLINE toBEncode #-}
@@ -163,24 +165,24 @@ instance BEncodable Torrent where
163 fromBEncode _ = decodingError "Torrent" 165 fromBEncode _ = decodingError "Torrent"
164 166
165 167
166instance BEncodable TorrentInfo where 168instance BEncodable ContentInfo where
167 toBEncode ti@(SingleFile { }) = fromAscAssocs 169 toBEncode ti@(SingleFile { }) = fromAscAssocs
168 [ "length" --> tLength ti 170 [ "length" --> ciLength ti
169 , "md5sum" -->? tMD5sum ti 171 , "md5sum" -->? ciMD5sum ti
170 , "name" --> tName ti 172 , "name" --> ciName ti
171 173
172 , "piece length" --> tPieceLength ti 174 , "piece length" --> ciPieceLength ti
173 , "pieces" --> tPieces ti 175 , "pieces" --> ciPieces ti
174 , "private" -->? tPrivate ti 176 , "private" -->? ciPrivate ti
175 ] 177 ]
176 178
177 toBEncode ti@(MultiFile {}) = fromAscAssocs 179 toBEncode ti@(MultiFile {}) = fromAscAssocs
178 [ "files" --> tFiles ti 180 [ "files" --> ciFiles ti
179 , "name" --> tName ti 181 , "name" --> ciName ti
180 182
181 , "piece length" --> tPieceLength ti 183 , "piece length" --> ciPieceLength ti
182 , "pieces" --> tPieces ti 184 , "pieces" --> ciPieces ti
183 , "private" -->? tPrivate ti 185 , "private" -->? ciPrivate ti
184 ] 186 ]
185 187
186 fromBEncode (BDict d) 188 fromBEncode (BDict d)
@@ -197,22 +199,22 @@ instance BEncodable TorrentInfo where
197 <*> d >-- "piece length" 199 <*> d >-- "piece length"
198 <*> d >-- "pieces" 200 <*> d >-- "pieces"
199 <*> d >--? "private" 201 <*> d >--? "private"
200 fromBEncode _ = decodingError "TorrentInfo" 202 fromBEncode _ = decodingError "ContentInfo"
201 203
202 204
203instance BEncodable TorrentFile where 205instance BEncodable FileInfo where
204 toBEncode tf = fromAssocs 206 toBEncode tf = fromAssocs
205 [ "length" --> tfLength tf 207 [ "length" --> fiLength tf
206 , "md5sum" -->? tfMD5sum tf 208 , "md5sum" -->? fiMD5sum tf
207 , "path" --> tfPath tf 209 , "path" --> fiPath tf
208 ] 210 ]
209 211
210 fromBEncode (BDict d) = 212 fromBEncode (BDict d) =
211 TorrentFile <$> d >-- "length" 213 FileInfo <$> d >-- "length"
212 <*> d >--? "md5sum" 214 <*> d >--? "md5sum"
213 <*> d >-- "path" 215 <*> d >-- "path"
214 216
215 fromBEncode _ = decodingError "TorrentFile" 217 fromBEncode _ = decodingError "FileInfo"
216 218
217 219
218-- | Read and decode a .torrent file. 220-- | Read and decode a .torrent file.