Revision 8d76e2e3 ChatSecure/Classes/Controllers/FileTransferManager.swift
| ChatSecure/Classes/Controllers/FileTransferManager.swift | ||
|---|---|---|
| 510 | 510 |
extension FileTransferManager {
|
| 511 | 511 |
|
| 512 | 512 |
/** creates downloadmessages and then downloads if needed. parent message should already be saved! @warn Do not call from within an existing db transaction! */ |
| 513 |
@objc public func createAndDownloadItemsIfNeeded(message: OTRMessageProtocol, readConnection: YapDatabaseConnection, force: Bool) {
|
|
| 514 |
DispatchQueue.global(qos: .default).async {
|
|
| 515 |
if message.messageMediaItemKey != nil || message.messageText?.count == 0 || message.downloadableURLs.count == 0 {
|
|
| 516 |
//DDLogVerbose("Download of message not needed \(message.messageKey)")
|
|
| 517 |
return |
|
| 518 |
} |
|
| 519 |
var downloads: [OTRDownloadMessage] = [] |
|
| 520 |
var disableAutomaticURLFetching = false |
|
| 521 |
if !force {
|
|
| 522 |
readConnection.read { (transaction) in
|
|
| 523 |
downloads = message.existingDownloads(with: transaction) |
|
| 524 |
if let thread = message.threadOwner(with: transaction), let account = OTRAccount.fetchObject(withUniqueID: thread.threadAccountIdentifier, transaction: transaction) {
|
|
| 525 |
disableAutomaticURLFetching = account.disableAutomaticURLFetching |
|
| 526 |
} |
|
| 527 |
} |
|
| 513 |
@objc public func createAndDownloadItemsIfNeeded(message: OTRMessageProtocol, force: Bool, transaction: YapDatabaseReadWriteTransaction) {
|
|
| 514 |
if message.messageMediaItemKey != nil || message.messageText?.count == 0 || message.downloadableURLs.count == 0 {
|
|
| 515 |
//DDLogVerbose("Download of message not needed \(message.messageKey)")
|
|
| 516 |
return |
|
| 517 |
} |
|
| 518 |
var downloads: [OTRDownloadMessage] = [] |
|
| 519 |
var disableAutomaticURLFetching = false |
|
| 520 |
if !force {
|
|
| 521 |
downloads = message.existingDownloads(with: transaction) |
|
| 522 |
if let thread = message.threadOwner(with: transaction), let account = OTRAccount.fetchObject(withUniqueID: thread.threadAccountIdentifier, transaction: transaction) {
|
|
| 523 |
disableAutomaticURLFetching = account.disableAutomaticURLFetching |
|
| 528 | 524 |
} |
| 525 |
} |
|
| 526 |
if downloads.count == 0 {
|
|
| 527 |
downloads = message.downloads() |
|
| 529 | 528 |
if downloads.count == 0 {
|
| 530 |
downloads = message.downloads() |
|
| 531 |
if downloads.count == 0 {
|
|
| 532 |
return |
|
| 533 |
} |
|
| 534 |
self.connection.readWrite({ (transaction) in
|
|
| 535 |
for download in downloads {
|
|
| 536 |
if disableAutomaticURLFetching, |
|
| 537 |
let filename = download.downloadableURL?.absoluteString {
|
|
| 538 |
let media = OTRMediaItem.incomingItem(withFilename: filename, mimeType: nil) |
|
| 539 |
media.parentObjectKey = download.uniqueId |
|
| 540 |
media.parentObjectCollection = download.messageCollection |
|
| 541 |
media.save(with: transaction) |
|
| 542 |
download.messageMediaItemKey = media.uniqueId |
|
| 543 |
download.messageError = FileTransferError.automaticDownloadsDisabled |
|
| 544 |
} |
|
| 545 |
download.save(with: transaction) |
|
| 546 |
} |
|
| 547 |
message.touch(with: transaction) |
|
| 548 |
}) |
|
| 549 |
} |
|
| 550 |
if disableAutomaticURLFetching {
|
|
| 551 |
DDLogVerbose("Automatic URL fetching disabled \(message.messageKey)")
|
|
| 552 | 529 |
return |
| 553 | 530 |
} |
| 554 | 531 |
for download in downloads {
|
| 555 |
self.downloadMediaIfNeeded(download) |
|
| 532 |
if disableAutomaticURLFetching, |
|
| 533 |
let filename = download.downloadableURL?.absoluteString {
|
|
| 534 |
let media = OTRMediaItem.incomingItem(withFilename: filename, mimeType: nil) |
|
| 535 |
media.parentObjectKey = download.uniqueId |
|
| 536 |
media.parentObjectCollection = download.messageCollection |
|
| 537 |
media.save(with: transaction) |
|
| 538 |
download.messageMediaItemKey = media.uniqueId |
|
| 539 |
download.messageError = FileTransferError.automaticDownloadsDisabled |
|
| 540 |
} |
|
| 541 |
download.save(with: transaction) |
|
| 556 | 542 |
} |
| 543 |
message.touch(with: transaction) |
|
| 544 |
} |
|
| 545 |
if disableAutomaticURLFetching {
|
|
| 546 |
DDLogVerbose("Automatic URL fetching disabled \(message.messageKey)")
|
|
| 547 |
return |
|
| 548 |
} |
|
| 549 |
for download in downloads {
|
|
| 550 |
self.downloadMediaIfNeeded(download) |
|
| 557 | 551 |
} |
| 558 | 552 |
} |
| 559 | 553 |
|
| ... | ... | |
| 694 | 688 |
} else {
|
| 695 | 689 |
DDLogError("Failed to refetch download message WTF \(downloadMessage)")
|
| 696 | 690 |
} |
| 697 |
}, completionQueue: DispatchQueue.main, |
|
| 698 |
completionBlock: {
|
|
| 699 |
UIApplication.shared.showLocalNotification(downloadMessage) |
|
| 691 |
UIApplication.shared.showLocalNotification(downloadMessage, transaction: transaction) |
|
| 700 | 692 |
}) |
| 701 | 693 |
}, completionQueue: nil) |
| 702 | 694 |
} |
Also available in: Unified diff