diff options
Diffstat (limited to 'src/Data/Torrent.hs')
-rw-r--r-- | src/Data/Torrent.hs | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/src/Data/Torrent.hs b/src/Data/Torrent.hs index e6ed8813..226126f2 100644 --- a/src/Data/Torrent.hs +++ b/src/Data/Torrent.hs | |||
@@ -11,7 +11,8 @@ | |||
11 | module Data.Torrent | 11 | module Data.Torrent |
12 | ( module Data.Torrent.InfoHash | 12 | ( module Data.Torrent.InfoHash |
13 | , Torrent(..), ContentInfo(..), FileInfo(..) | 13 | , Torrent(..), ContentInfo(..), FileInfo(..) |
14 | , contenLength, pieceCount | 14 | , contentLength, pieceCount |
15 | , contentLayout | ||
15 | , fromFile | 16 | , fromFile |
16 | ) where | 17 | ) where |
17 | 18 | ||
@@ -24,7 +25,7 @@ import Data.Text (Text) | |||
24 | import Data.BEncode | 25 | import Data.BEncode |
25 | import Data.Torrent.InfoHash | 26 | import Data.Torrent.InfoHash |
26 | import Network.URI | 27 | import Network.URI |
27 | 28 | import System.FilePath | |
28 | 29 | ||
29 | type Time = Text | 30 | type Time = Text |
30 | 31 | ||
@@ -229,6 +230,26 @@ contentLength MultiFile { ciFiles = tfs } = sum (map fiLength tfs) | |||
229 | pieceCount :: ContentInfo -> Int | 230 | pieceCount :: ContentInfo -> Int |
230 | pieceCount ti = contentLength ti `sizeInBase` ciPieceLength ti | 231 | pieceCount ti = contentLength ti `sizeInBase` ciPieceLength ti |
231 | 232 | ||
233 | -- | File layout specifies the order and the size of each file in the storage. | ||
234 | -- Note that order of files is highly important since we coalesce all | ||
235 | -- the files in the given order to get the linear block address space. | ||
236 | -- | ||
237 | type Layout = [(FilePath, Int)] | ||
238 | |||
239 | fileInfo :: ContentInfo -> [FileInfo] | ||
240 | fileInfo (SingleFile { ciName = name, ciLength = len, ciMD5sum = md5 }) | ||
241 | = [FileInfo len md5 [name]] | ||
242 | fileInfo (MultiFile { ciFiles = fs }) = fs | ||
243 | |||
244 | fileLayout :: FileInfo -> (FilePath, Int) | ||
245 | fileLayout (FileInfo { fiLength = len, fiPath = name }) = (path, fromIntegral len) | ||
246 | where -- WARN use utf8 encoding in name | ||
247 | path = joinPath (map BC.unpack name) | ||
248 | |||
249 | |||
250 | contentLayout :: ContentInfo -> Layout | ||
251 | contentLayout = map fileLayout . fileInfo | ||
252 | |||
232 | 253 | ||
233 | -- | Read and decode a .torrent file. | 254 | -- | Read and decode a .torrent file. |
234 | fromFile :: FilePath -> IO (Result Torrent) | 255 | fromFile :: FilePath -> IO (Result Torrent) |