 
         if downloadState == MessageDownloadState.Downloaded.rawValue {
             reportProgress?(1.0)
+
+            return
         }
         
-        if !attachmentURLString.isEmpty {
+        if !attachmentURLString.isEmpty, let URL = NSURL(string: attachmentURLString) {
 
-            if let URL = NSURL(string: attachmentURLString) {
+            let downloadTask = sharedDownloader.session.downloadTaskWithURL(URL)
+
+            let progressReporter = ProgressReporter(downloadTask: downloadTask, reportProgressAction: reportProgress) { data in
+
+                let fileName = NSUUID().UUIDString
+
+                dispatch_async(dispatch_get_main_queue()) {
+                    let realm = Realm()
+
+                    if let message = messageWithMessageID(messageID, inRealm: realm) {
+
+                        switch mediaType {
+
+                        case MessageMediaType.Image.rawValue:
+
+                            if let fileURL = NSFileManager.saveMessageImageData(data, withName: fileName) {
+                                self.updateAttachmentOfMessage(message, withAttachmentFileName: fileName, inRealm: realm)
+                            }
+
+                        case MessageMediaType.Video.rawValue:
+
+                            if let fileURL = NSFileManager.saveMessageVideoData(data, withName: fileName) {
+                                self.updateAttachmentOfMessage(message, withAttachmentFileName: fileName, inRealm: realm)
+                            }
+
+                        case MessageMediaType.Audio.rawValue:
+
+                            if let fileURL = NSFileManager.saveMessageAudioData(data, withName: fileName) {
+                                self.updateAttachmentOfMessage(message, withAttachmentFileName: fileName, inRealm: realm)
+                            }
+                            
+                        default:
+                            break
+                        }
+                    }
+                }
+            }
+
+            sharedDownloader.progressReporters.append(progressReporter)
+
+            downloadTask.resume()
+        }
+
+        if mediaType == MessageMediaType.Video.rawValue {
+
+            let thumbnailURLString = message.thumbnailURLString
+
+            if !thumbnailURLString.isEmpty && message.localThumbnailName.isEmpty, let URL = NSURL(string: thumbnailURLString) {
 
                 let downloadTask = sharedDownloader.session.downloadTaskWithURL(URL, completionHandler: { location, response, error in
 
                     if let data = NSData(contentsOfURL: location) {
-
-                        reportProgress?(1.0)
 
                         let fileName = NSUUID().UUIDString
 


    struct ProgressReporter {

        let name: String

        typealias ReportAction = Double -> Void

        let reportAction: ReportAction
    }

    class func downloadAttachmentsOfMessage(message: Message, reportProgress: ProgressReporter.ReportAction?) {


                            let realm = Realm()

                            if let message = messageWithMessageID(messageID, inRealm: realm) {
                                if let fileURL = NSFileManager.saveMessageImageData(data, withName: fileName) {
                                    self.updateThumbnailOfMessage(message, withThumbnailFileName: fileName, inRealm: realm)
                                }
                            }
                        }
                    }
                })
                
                downloadTask.resume()
            }
        }
    }
}

extension YepDownloader: NSURLSessionDelegate {

}

extension YepDownloader: NSURLSessionDownloadDelegate {

    private func reportProgress(progress: Double, ofDownloadTask downloadTask: NSURLSessionDownloadTask) {
        progressReporters.filter({ $0.downloadTask == downloadTask }).map({ $0.reportProgressAction?(progress) })
    }

    private func handleData(data: NSData, ofDownloadTask downloadTask: NSURLSessionDownloadTask) {
        progressReporters.filter({ $0.downloadTask == downloadTask }).map({ $0.finishedAction(data) })
    }

    func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {

        if let data = NSData(contentsOfURL: location) {
            handleData(data, ofDownloadTask: downloadTask)
        }

        reportProgress(1.0, ofDownloadTask: downloadTask)

        println("didFinishDownloadingToURL")
    }

    func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {

        let progress: Double = Double(totalBytesWritten) / Double(totalBytesExpectedToWrite)

        reportProgress(progress, ofDownloadTask: downloadTask)

        println("downloadTask progress \(progress)")
    }
}

