I am using below code to upload Image/Video to FTP. The complete image is not getting uploaded. Lowe part of the image is getting cut. At the end of the upload process, the complete data is said to be uploaded but the image is getting cut. I am able to upload small images and videos. The issue is for large images and videos. Kindly let me know the changes I need to do in the below code.
let buf = movieData?.bytes.assumingMemoryBound(to: UInt8.self)
let buf2 = movieData?.bytes.assumingMemoryBound(to: UInt8.self)
let buf3 = UnsafeMutablePointer<UInt8>.allocate(capacity: (movieData?.length)!)
var leftOverSize = movieData?.length
let bytesFile = movieData?.length
var totalBytesWritten = 0
var bytesWritten = 0
let ftpUrl = NSURL(string: "FTP_URL")
let stream = CFWriteStreamCreateWithFTPURL(nil,ftpUrl!).takeUnretainedValue()
let cfstatus = CFWriteStreamOpen(stream) as Bool
// connection fail
if !cfstatus {
print("Not connected")
}
repeat{
// Write the data to the write stream
bytesWritten = CFWriteStreamWrite(stream, buf, leftOverSize!)
print("bytesWritten: \(bytesWritten)")
if (bytesWritten > 0) {
totalBytesWritten += bytesWritten
// Store leftover data until kCFStreamEventCanAcceptBytes event occurs again
if (bytesWritten < bytesFile!) {
leftOverSize = bytesFile! - totalBytesWritten
memmove(buf3, buf2! + bytesWritten, leftOverSize!)
}else{
leftOverSize = 0
}
}else{
print("CFWriteStreamWrite returned \(bytesWritten)")
break
}
if !CFWriteStreamCanAcceptBytes(stream){
sleep(1)
}
}while((totalBytesWritten < bytesFile!))
CFWriteStreamClose(stream)