Allocates file space on init, uses more sensible progress file location
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user