summaryrefslogtreecommitdiff
path: root/src/Data/Torrent.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Data/Torrent.hs')
-rw-r--r--src/Data/Torrent.hs25
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 @@
11module Data.Torrent 11module 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)
24import Data.BEncode 25import Data.BEncode
25import Data.Torrent.InfoHash 26import Data.Torrent.InfoHash
26import Network.URI 27import Network.URI
27 28import System.FilePath
28 29
29type Time = Text 30type Time = Text
30 31
@@ -229,6 +230,26 @@ contentLength MultiFile { ciFiles = tfs } = sum (map fiLength tfs)
229pieceCount :: ContentInfo -> Int 230pieceCount :: ContentInfo -> Int
230pieceCount ti = contentLength ti `sizeInBase` ciPieceLength ti 231pieceCount 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--
237type Layout = [(FilePath, Int)]
238
239fileInfo :: ContentInfo -> [FileInfo]
240fileInfo (SingleFile { ciName = name, ciLength = len, ciMD5sum = md5 })
241 = [FileInfo len md5 [name]]
242fileInfo (MultiFile { ciFiles = fs }) = fs
243
244fileLayout :: FileInfo -> (FilePath, Int)
245fileLayout (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
250contentLayout :: ContentInfo -> Layout
251contentLayout = map fileLayout . fileInfo
252
232 253
233-- | Read and decode a .torrent file. 254-- | Read and decode a .torrent file.
234fromFile :: FilePath -> IO (Result Torrent) 255fromFile :: FilePath -> IO (Result Torrent)