diff --git a/BitTorrent/File Manager/TorrentFileManager.swift b/BitTorrent/File Manager/TorrentFileManager.swift index 0d88096..4f96764 100644 --- a/BitTorrent/File Manager/TorrentFileManager.swift +++ b/BitTorrent/File Manager/TorrentFileManager.swift @@ -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) } diff --git a/BitTorrent/Torrent Client/TorrentClient.swift b/BitTorrent/Torrent Client/TorrentClient.swift index f0311c4..5b87647 100644 --- a/BitTorrent/Torrent Client/TorrentClient.swift +++ b/BitTorrent/Torrent Client/TorrentClient.swift @@ -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 diff --git a/BitTorrentExample/AppDelegate.swift b/BitTorrentExample/AppDelegate.swift index 6ac82ea..3e68bac 100644 --- a/BitTorrentExample/AppDelegate.swift +++ b/BitTorrentExample/AppDelegate.swift @@ -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)