@@ -579,98 +579,100 @@ class ConnectionManager {
579
579
}
580
580
581
581
//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
+ }
587
589
}
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
617
612
}
618
- }
619
-
620
- //Read the file
621
- var fileResponseIndex : Int32 = 0
622
- do {
623
- let compressionPipe = try CompressionPipeDeflate ( chunkSize: Int ( CommConst . defaultFileChunkSize) )
624
613
625
- let fileHandle = try FileHandle ( forReadingFrom: fileURL)
614
+ //Clean up
615
+ defer {
616
+ if fileNeedsCleanup {
617
+ try ? FileManager . default. removeItem ( at: fileURL)
618
+ }
619
+ }
626
620
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
643
634
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 )
646
637
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 }
649
640
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 )
655
664
}
656
665
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
661
667
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
663
670
}
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
+ }
674
676
}
675
677
}
676
678
0 commit comments