Advertisement
Guest User

Untitled

a guest
Sep 7th, 2017
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. let ftpup = FTPUpload(baseUrl: "mysite.com", userName: "user@mysite.com", password: "password", directoryPath: "")
  2.  
  3. let image = UIImage(named: "medium")
  4. let imagedata = UIImageJPEGRepresentation(image!, 1)
  5.  
  6. ftpup.send(data: imagedata!, with: "cloudpowa.jpg", success: {(success) -> Void in
  7. if !success {
  8. print("Failed upload!")
  9. }
  10. else {
  11. print("image uploaded!")
  12. }
  13. })
  14.  
  15. import Foundation
  16. import CFNetwork
  17.  
  18. public class FTPUpload {
  19. fileprivate let ftpBaseUrl: String
  20. fileprivate let directoryPath: String
  21. fileprivate let username: String
  22. fileprivate let password: String
  23.  
  24. public init(baseUrl: String, userName: String, password: String, directoryPath: String) {
  25. self.ftpBaseUrl = baseUrl
  26. self.username = userName
  27. self.password = password
  28. self.directoryPath = directoryPath
  29. }
  30. }
  31.  
  32.  
  33. // MARK: - Steam Setup
  34. extension FTPUpload {
  35. private func setFtpUserName(for ftpWriteStream: CFWriteStream, userName: CFString) {
  36. let propertyKey = CFStreamPropertyKey(rawValue: kCFStreamPropertyFTPUserName)
  37. CFWriteStreamSetProperty(ftpWriteStream, propertyKey, userName)
  38. }
  39.  
  40. private func setFtpPassword(for ftpWriteStream: CFWriteStream, password: CFString) {
  41. let propertyKey = CFStreamPropertyKey(rawValue: kCFStreamPropertyFTPPassword)
  42. CFWriteStreamSetProperty(ftpWriteStream, propertyKey, password)
  43. }
  44.  
  45. fileprivate func ftpWriteStream(forFileName fileName: String) -> CFWriteStream? {
  46. let fullyQualifiedPath = "ftp://(ftpBaseUrl)/(directoryPath)/(fileName)"
  47.  
  48. guard let ftpUrl = CFURLCreateWithString(kCFAllocatorDefault, fullyQualifiedPath as CFString, nil) else { return nil }
  49. let ftpStream = CFWriteStreamCreateWithFTPURL(kCFAllocatorDefault, ftpUrl)
  50. let ftpWriteStream = ftpStream.takeRetainedValue()
  51. setFtpUserName(for: ftpWriteStream, userName: username as CFString)
  52. setFtpPassword(for: ftpWriteStream, password: password as CFString)
  53. return ftpWriteStream
  54. }
  55. }
  56.  
  57.  
  58. // MARK: - FTP Write
  59. extension FTPUpload {
  60. public func send(data: Data, with fileName: String, success: @escaping ((Bool)->Void)) {
  61.  
  62. guard let ftpWriteStream = ftpWriteStream(forFileName: fileName) else {
  63. success(false)
  64. return
  65. }
  66.  
  67. if CFWriteStreamOpen(ftpWriteStream) == false {
  68. print("Could not open stream")
  69. success(false)
  70. return
  71. }
  72.  
  73. let fileSize = data.count
  74. let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: fileSize)
  75. data.copyBytes(to: buffer, count: fileSize)
  76.  
  77. defer {
  78. CFWriteStreamClose(ftpWriteStream)
  79. buffer.deallocate(capacity: fileSize)
  80. }
  81.  
  82. var offset: Int = 0
  83. var dataToSendSize: Int = fileSize
  84.  
  85. repeat {
  86. let bytesWritten = CFWriteStreamWrite(ftpWriteStream, &buffer[offset], dataToSendSize)
  87. if bytesWritten > 0 {
  88. offset += bytesWritten.littleEndian
  89. dataToSendSize -= bytesWritten
  90. continue
  91. } else if bytesWritten < 0 {
  92. // ERROR
  93. print("FTPUpload - ERROR")
  94. break
  95. } else if bytesWritten == 0 {
  96. // SUCCESS
  97. print("FTPUpload - Completed!!")
  98. break
  99. }
  100. } while CFWriteStreamCanAcceptBytes(ftpWriteStream)
  101.  
  102. success(true)
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement