Skip to content
This repository was archived by the owner on Oct 22, 2021. It is now read-only.

Swift 4 Update #26

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions rebekka-demo/Demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "net.fry.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand All @@ -358,6 +359,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "net.fry.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand Down
53 changes: 29 additions & 24 deletions rebekka-demo/Demo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,45 +18,50 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
var configuration = SessionConfiguration()
configuration.host = "ftp://speedtest.tele2.net"
self.session = Session(configuration: configuration)
testList()
//testDownload()
//testUpload()
//testCreate()
return true
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.

var configuration = SessionConfiguration()
configuration.host = "ftp://speedtest.tele2.net"
self.session = Session(configuration: configuration)

testList()
// testDownload()
//testUpload()
//testCreate()
return true
}

func testList() {
self.session.list("/") {
self.session.list("/") { [unowned self]
(resources, error) -> Void in
print("List directory with result:\n\(resources), error: \(error)\n\n")
print("List directory with result:\n\(String(describing: resources)), error: \(String(describing: error))\n\n")

}
}

func testUpload() {
if let URL = NSBundle.mainBundle().URLForResource("TestUpload", withExtension: "png") {
let path = "/upload/\(NSUUID().UUIDString).png"
if let URL = Bundle.main.url(forResource: "TestUpload", withExtension: "png") {
let path = "/upload/\(UUID().uuidString).png"
self.session.upload(URL, path: path) {
(result, error) -> Void in
print("Upload file with result:\n\(result), error: \(error)\n\n")
print("Upload file with result:\n\(result), error: \(String(describing: error))\n\n")
}
}
}

func testDownload() {
self.session.download("/1MB.zip") {
func testDownload(_ path: String) {
self.session.download(path, progressHandler: { progress in
DispatchQueue.main.async {
print("progress", progress)
}
}) {
(fileURL, error) -> Void in
print("Download file with result:\n\(fileURL), error: \(error)\n\n")
print("Download file with result:\n\(String(describing: fileURL)), error: \(String(describing: error))\n\n")
if let fileURL = fileURL {
do {
try NSFileManager.defaultManager().removeItemAtURL(fileURL)
// try FileManager.default.removeItem(at: fileURL)
} catch let error as NSError {
print("Error: \(error)")
}
Expand All @@ -66,10 +71,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}

func testCreate() {
let name = NSUUID().UUIDString
let name = UUID().uuidString
self.session.createDirectory("/upload/\(name)") {
(result, error) -> Void in
print("Create directory with result:\n\(result), error: \(error)")
print("Create directory with result:\n\(result), error: \(String(describing: error))")
}
}

Expand Down
2 changes: 2 additions & 0 deletions rebekka-source/Rebekka.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = "1,2";
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
Expand Down Expand Up @@ -388,6 +389,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
VERSIONING_SYSTEM = "apple-generic";
Expand Down
1 change: 1 addition & 0 deletions rebekka-source/Rebekka/DirectoryCreationOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ import Foundation
internal class DirectoryCreationOperation: WriteStreamOperation {

}

35 changes: 21 additions & 14 deletions rebekka-source/Rebekka/FileDownloadOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,52 +11,59 @@ import Foundation
/** Operation for downloading a file from FTP server. */
internal class FileDownloadOperation: ReadStreamOperation {

private var fileHandle: NSFileHandle?
var fileURL: NSURL?
private var fileHandle: FileHandle?
var fileURL: URL?
var progressHandler: DownloadProgressHandler?

override func start() {
let filePath = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent(NSUUID().UUIDString)
self.fileURL = NSURL(fileURLWithPath: filePath)
let filePath = (NSTemporaryDirectory() as NSString).appendingPathComponent(path ?? UUID().uuidString)
self.fileURL = URL(fileURLWithPath: filePath)
do {
try NSData().writeToURL(self.fileURL!, options: NSDataWritingOptions.DataWritingAtomic)
self.fileHandle = try NSFileHandle(forWritingToURL: self.fileURL!)
try Data().write(to: self.fileURL!, options: NSData.WritingOptions.atomic)
self.fileHandle = try FileHandle(forWritingTo: self.fileURL!)
self.startOperationWithStream(self.readStream)
} catch let error as NSError {
self.error = error
self.finishOperation()
}
}

override func streamEventEnd(aStream: NSStream) -> (Bool, NSError?) {
override func streamEventEnd(_ aStream: Stream) -> (Bool, NSError?) {
self.fileHandle?.closeFile()
return (true, nil)
}

override func streamEventError(aStream: NSStream) {
override func streamEventError(_ aStream: Stream) {
super.streamEventError(aStream)
self.fileHandle?.closeFile()
if self.fileURL != nil {
do {
try NSFileManager.defaultManager().removeItemAtURL(self.fileURL!)
try FileManager.default.removeItem(at: self.fileURL!)
} catch _ {
}
}
self.fileURL = nil
}

override func streamEventHasBytes(aStream: NSStream) -> (Bool, NSError?) {
if let inputStream = aStream as? NSInputStream {
override func streamEventHasBytes(_ aStream: Stream) -> (Bool, NSError?) {
let totalBytesSize = aStream.property(forKey: Stream.PropertyKey(rawValue: kCFStreamPropertyFTPResourceSize as String)) as! Int
var downloadedBytes: Int = 0

if let inputStream = aStream as? InputStream {
var parsetBytes: Int = 0
repeat {
parsetBytes = inputStream.read(self.temporaryBuffer, maxLength: 1024)
parsetBytes = inputStream.read(self.temporaryBuffer, maxLength: 65536)
downloadedBytes += parsetBytes
progressHandler?(Float(downloadedBytes) / Float(totalBytesSize))
if parsetBytes > 0 {
autoreleasepool {
let data = NSData(bytes: self.temporaryBuffer, length: parsetBytes)
self.fileHandle!.writeData(data)
let data = Data(bytes: UnsafePointer<UInt8>(self.temporaryBuffer), count: parsetBytes)
self.fileHandle!.write(data)
}
}
} while (parsetBytes > 0)
}
return (true, nil)
}
}

21 changes: 11 additions & 10 deletions rebekka-source/Rebekka/FileUploadOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import Foundation

/** Operation for file uploading. */
internal class FileUploadOperation: WriteStreamOperation {
private var fileHandle: NSFileHandle?
var fileURL: NSURL!
fileprivate var fileHandle: FileHandle?
var fileURL: URL!

override func start() {
do {
self.fileHandle = try NSFileHandle(forReadingFromURL: fileURL)
self.fileHandle = try FileHandle(forReadingFrom: fileURL)
self.startOperationWithStream(self.writeStream)
} catch let error as NSError {
self.error = error
Expand All @@ -24,23 +24,23 @@ internal class FileUploadOperation: WriteStreamOperation {
}
}

override func streamEventEnd(aStream: NSStream) -> (Bool, NSError?) {
override func streamEventEnd(_ aStream: Stream) -> (Bool, NSError?) {
self.fileHandle?.closeFile()
return (true, nil)
}

override func streamEventError(aStream: NSStream) {
override func streamEventError(_ aStream: Stream) {
super.streamEventError(aStream)
self.fileHandle?.closeFile()
}

override func streamEventHasSpace(aStream: NSStream) -> (Bool, NSError?) {
if let writeStream = aStream as? NSOutputStream {
override func streamEventHasSpace(_ aStream: Stream) -> (Bool, NSError?) {
if let writeStream = aStream as? OutputStream {
let offsetInFile = self.fileHandle!.offsetInFile
let data = self.fileHandle!.readDataOfLength(1024)
let writtenBytes = writeStream.write(UnsafePointer<UInt8>(data.bytes), maxLength: data.length)
let data = self.fileHandle!.readData(ofLength: 1024)
let writtenBytes = writeStream.write((data as NSData).bytes.bindMemory(to: UInt8.self, capacity: data.count), maxLength: data.count)
if writtenBytes > 0 {
self.fileHandle?.seekToFileOffset(offsetInFile + UInt64(writtenBytes))
self.fileHandle?.seek(toFileOffset: offsetInFile + UInt64(writtenBytes))
} else if writtenBytes == -1 {
self.finishOperation()
}
Expand All @@ -49,3 +49,4 @@ internal class FileUploadOperation: WriteStreamOperation {
}

}

35 changes: 18 additions & 17 deletions rebekka-source/Rebekka/Operation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,40 @@
import Foundation

internal enum OperationState {
case None
case Ready
case Executing
case Finished
case none
case ready
case executing
case finished
}

/** The base class for FTP operations used in framework. */
internal class Operation: NSOperation {

internal class Operation: Foundation.Operation {
var error: NSError?

internal let configuration: SessionConfiguration

internal var state = OperationState.Ready {
internal var state = OperationState.ready {
willSet {
self.willChangeValueForKey("isReady")
self.willChangeValueForKey("isExecuting")
self.willChangeValueForKey("isFinished")
self.willChangeValue(forKey: "isReady")
self.willChangeValue(forKey: "isExecuting")
self.willChangeValue(forKey: "isFinished")
}
didSet {
self.didChangeValueForKey("isReady")
self.didChangeValueForKey("isExecuting")
self.didChangeValueForKey("isFinished")
self.didChangeValue(forKey: "isReady")
self.didChangeValue(forKey: "isExecuting")
self.didChangeValue(forKey: "isFinished")
}
}

override var asynchronous: Bool { get { return true } }
override var isAsynchronous: Bool { get { return true } }

override var ready: Bool { get { return self.state == .Ready } }
override var executing: Bool { get { return self.state == .Executing } }
override var finished: Bool { get { return self.state == .Finished } }
override var isReady: Bool { get { return self.state == .ready } }
override var isExecuting: Bool { get { return self.state == .executing } }
override var isFinished: Bool { get { return self.state == .finished } }

init(configuration: SessionConfiguration) {
self.configuration = configuration
}
}

7 changes: 4 additions & 3 deletions rebekka-source/Rebekka/ReadStreamOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import Foundation
internal class ReadStreamOperation: StreamOperation {

internal lazy var temporaryBuffer: UnsafeMutablePointer<UInt8> = {
return UnsafeMutablePointer<UInt8>.alloc(1024)
return UnsafeMutablePointer<UInt8>.allocate(capacity: 65536)
}()

lazy var readStream: NSInputStream = {
let cfStream = CFReadStreamCreateWithFTPURL(nil, self.fullURL())
lazy var readStream: InputStream = {
let cfStream = CFReadStreamCreateWithFTPURL(nil, self.fullURL() as CFURL)
CFReadStreamSetDispatchQueue(cfStream.takeUnretainedValue(), self.queue)
return cfStream.takeRetainedValue()
}()
Expand All @@ -25,3 +25,4 @@ internal class ReadStreamOperation: StreamOperation {
self.startOperationWithStream(self.readStream)
}
}

Loading