Allocates file space on init, uses more sensible progress file location

This commit is contained in:
Ben Davis
2017-11-03 18:00:12 +08:00
parent c22417f1e2
commit dffbaa6cf3
3 changed files with 18 additions and 11 deletions
@@ -120,23 +120,30 @@ extension TorrentFileManager {
// Save/Load progress
extension TorrentFileManager {
private static func sanitizedFileName(from infoHash: Data) -> String {
let base64EncodedString = String(asciiData: infoHash.base64EncodedData())!
let sanitizedString = base64EncodedString
.replacingOccurrences(of: "/", with: "_")
return sanitizedString + ".torrentprogress"
}
static func saveProgressBitfield(_ bitfield: BitField, infoHash: Data) {
let fileName = String(asciiData: infoHash.base64EncodedData())!
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory,
let fileName = sanitizedFileName(from: infoHash)
let documentsPath = NSSearchPathForDirectoriesInDomains(.cachesDirectory,
.userDomainMask,
true)[0] as String
let documentsUrl = URL(fileURLWithPath: documentsPath, isDirectory: true)
let fileURL = documentsUrl.appendingPathComponent("torrent_progress.bin", isDirectory: false)
let fileURL = documentsUrl.appendingPathComponent(fileName, isDirectory: false)
try? bitfield.toData().write(to: fileURL)
}
static func loadSavedProgressBitfield(infoHash: Data) -> BitField? {
let fileName = String(asciiData: infoHash.base64EncodedData())!
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory,
let fileName = sanitizedFileName(from: infoHash)
let documentsPath = NSSearchPathForDirectoriesInDomains(.cachesDirectory,
.userDomainMask,
true)[0] as String
let documentsUrl = URL(fileURLWithPath: documentsPath, isDirectory: true)
let fileURL = documentsUrl.appendingPathComponent("torrent_progress.bin", isDirectory: false)
let fileURL = documentsUrl.appendingPathComponent(fileName, isDirectory: false)
if let data = try? Data(contentsOf: fileURL) {
return BitField(data: data)
}
@@ -45,6 +45,10 @@ public class TorrentClient {
let clientId = TorrentPeer.makePeerId()
public init(metaInfo: TorrentMetaInfo, rootDirectory: String) {
let downloadDirectory = rootDirectory + "/" + metaInfo.sensibleDownloadDirectoryName()
try! TorrentFileManager.prepareRootDirectory(downloadDirectory, forTorrentMetaInfo: metaInfo)
self.metaInfo = metaInfo
self.torrentServer = TorrentServer(infoHash: metaInfo.infoHash, clientId: clientId)
self.progressManager = TorrentProgressManager(metaInfo: metaInfo, rootDirectory: rootDirectory)
@@ -52,7 +56,6 @@ public class TorrentClient {
infoHash: metaInfo.infoHash,
bitFieldSize: metaInfo.info.pieces.count)
// TODO: listen on this port
trackerManager = TorrentTrackerManager(metaInfo: metaInfo, clientId: clientId, port: torrentServer.port)
trackerManager.delegate = self
+1 -4
View File
@@ -23,10 +23,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
pathRoot = pathRoot + "/Torrent Downloads/"
print(pathRoot)
let downloadDirectory = pathRoot + "/" + metaInfo.sensibleDownloadDirectoryName()
try! TorrentFileManager.prepareRootDirectory(downloadDirectory, forTorrentMetaInfo: metaInfo)
torrentClient = TorrentClient(metaInfo: metaInfo, rootDirectory: pathRoot)
window = UIWindow(frame: UIScreen.main.bounds)