Skip to content

Commit 0d46a22

Browse files
committed
Fix attachment download check
1 parent 3716635 commit 0d46a22

File tree

1 file changed

+83
-81
lines changed

1 file changed

+83
-81
lines changed

AirMessage/Connection/ConnectionManager.swift

+83-81
Original file line numberDiff line numberDiff line change
@@ -579,98 +579,100 @@ class ConnectionManager {
579579
}
580580

581581
//Assemble and filter attachments
582-
let messageAttachments = conversationItems
583-
.compactMap { $0 as? MessageInfo }
584-
.flatMap { message in
585-
message.attachments.filter { attachment in
586-
attachmentsFilter?.apply(to: attachment, ofDate: message.date) ?? true
582+
if let attachmentsFilter = attachmentsFilter {
583+
let messageAttachments = conversationItems
584+
.compactMap { $0 as? MessageInfo }
585+
.flatMap { message in
586+
message.attachments.filter { attachment in
587+
attachmentsFilter.apply(to: attachment, ofDate: message.date)
588+
}
587589
}
588-
}
589-
for attachment in messageAttachments {
590-
//Make sure the file exists
591-
guard let attachmentURL = attachment.localURL,
592-
FileManager.default.fileExists(atPath: attachmentURL.path) else {
593-
continue
594-
}
595-
596-
//Try to normalize the file
597-
let fileURL: URL
598-
let fileType: String?
599-
let fileName: String
600-
let fileNeedsCleanup: Bool
601-
if let normalizedDetails = normalizeFile(url: attachmentURL, ext: attachmentURL.pathExtension) {
602-
fileURL = normalizedDetails.url
603-
fileType = normalizedDetails.type
604-
fileName = normalizedDetails.name
605-
fileNeedsCleanup = true
606-
} else {
607-
fileURL = attachmentURL
608-
fileType = attachment.type
609-
fileName = attachment.name
610-
fileNeedsCleanup = false
611-
}
612-
613-
//Clean up
614-
defer {
615-
if fileNeedsCleanup {
616-
try? FileManager.default.removeItem(at: fileURL)
590+
for attachment in messageAttachments {
591+
//Make sure the file exists
592+
guard let attachmentURL = attachment.localURL,
593+
FileManager.default.fileExists(atPath: attachmentURL.path) else {
594+
continue
595+
}
596+
597+
//Try to normalize the file
598+
let fileURL: URL
599+
let fileType: String?
600+
let fileName: String
601+
let fileNeedsCleanup: Bool
602+
if let normalizedDetails = normalizeFile(url: attachmentURL, ext: attachmentURL.pathExtension) {
603+
fileURL = normalizedDetails.url
604+
fileType = normalizedDetails.type
605+
fileName = normalizedDetails.name
606+
fileNeedsCleanup = true
607+
} else {
608+
fileURL = attachmentURL
609+
fileType = attachment.type
610+
fileName = attachment.name
611+
fileNeedsCleanup = false
617612
}
618-
}
619-
620-
//Read the file
621-
var fileResponseIndex: Int32 = 0
622-
do {
623-
let compressionPipe = try CompressionPipeDeflate(chunkSize: Int(CommConst.defaultFileChunkSize))
624613

625-
let fileHandle = try FileHandle(forReadingFrom: fileURL)
614+
//Clean up
615+
defer {
616+
if fileNeedsCleanup {
617+
try? FileManager.default.removeItem(at: fileURL)
618+
}
619+
}
626620

627-
var doBreak: Bool
628-
repeat {
629-
doBreak = try autoreleasepool {
630-
//Read data
631-
var data = try fileHandle.readCompat(upToCount: Int(CommConst.defaultFileChunkSize))
632-
let isEOF = data.count == 0
633-
634-
//Compress the data
635-
let dataOut = try compressionPipe.pipe(data: &data, isLast: isEOF)
636-
637-
//Make sure the client is still connected
638-
guard client.isConnected.value else { return true }
639-
640-
//Build and send the request
641-
do {
642-
guard let dataProxy = dataProxy else { return true }
621+
//Read the file
622+
var fileResponseIndex: Int32 = 0
623+
do {
624+
let compressionPipe = try CompressionPipeDeflate(chunkSize: Int(CommConst.defaultFileChunkSize))
625+
626+
let fileHandle = try FileHandle(forReadingFrom: fileURL)
627+
628+
var doBreak: Bool
629+
repeat {
630+
doBreak = try autoreleasepool {
631+
//Read data
632+
var data = try fileHandle.readCompat(upToCount: Int(CommConst.defaultFileChunkSize))
633+
let isEOF = data.count == 0
643634

644-
var packer = AirPacker()
645-
packer.pack(int: NHT.massRetrievalFile.rawValue)
635+
//Compress the data
636+
let dataOut = try compressionPipe.pipe(data: &data, isLast: isEOF)
646637

647-
packer.pack(short: requestID)
648-
packer.pack(int: fileResponseIndex)
638+
//Make sure the client is still connected
639+
guard client.isConnected.value else { return true }
649640

650-
//Include extra information with the initial response
651-
if fileResponseIndex == 0 {
652-
packer.pack(string: attachment.name) //Original file name
653-
packer.pack(optionalString: fileName) //Converted file name
654-
packer.pack(optionalString: fileType) //Converted file type
641+
//Build and send the request
642+
do {
643+
guard let dataProxy = dataProxy else { return true }
644+
645+
var packer = AirPacker()
646+
packer.pack(int: NHT.massRetrievalFile.rawValue)
647+
648+
packer.pack(short: requestID)
649+
packer.pack(int: fileResponseIndex)
650+
651+
//Include extra information with the initial response
652+
if fileResponseIndex == 0 {
653+
packer.pack(string: attachment.name) //Original file name
654+
packer.pack(optionalString: fileName) //Converted file name
655+
packer.pack(optionalString: fileType) //Converted file type
656+
}
657+
658+
packer.pack(bool: isEOF) //Is last message
659+
660+
packer.pack(string: attachment.guid) //Attachment GUID
661+
packer.pack(payload: dataOut) //Data
662+
663+
dataProxy.send(message: packer.data, to: client, encrypt: true, onSent: nil)
655664
}
656665

657-
packer.pack(bool: isEOF) //Is last message
658-
659-
packer.pack(string: attachment.guid) //Attachment GUID
660-
packer.pack(payload: dataOut) //Data
666+
fileResponseIndex += 1
661667

662-
dataProxy.send(message: packer.data, to: client, encrypt: true, onSent: nil)
668+
//Break if we reached the end of the file
669+
return isEOF
663670
}
664-
665-
fileResponseIndex += 1
666-
667-
//Break if we reached the end of the file
668-
return isEOF
669-
}
670-
} while !doBreak
671-
} catch {
672-
LogManager.log("Failed to read / compress data for mass retrieval attachment file \(fileURL.path) (\(attachment.guid)): \(error)", level: .notice)
673-
return
671+
} while !doBreak
672+
} catch {
673+
LogManager.log("Failed to read / compress data for mass retrieval attachment file \(fileURL.path) (\(attachment.guid)): \(error)", level: .notice)
674+
return
675+
}
674676
}
675677
}
676678

0 commit comments

Comments
 (0)