mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-07-06 19:40:41 +00:00
Added support for thumbnails in standalone media file upload
This commit is contained in:
parent
a94cce354b
commit
0144957e82
@ -367,7 +367,7 @@ func enqueueMessages(transaction: Transaction, account: Account, peerId: PeerId,
|
||||
}
|
||||
|
||||
let authorId: PeerId?
|
||||
if let peer = peer as? TelegramChannel, case let .broadcast(info) = peer.info {
|
||||
if let peer = peer as? TelegramChannel, case .broadcast = peer.info {
|
||||
authorId = peer.id
|
||||
} else {
|
||||
authorId = account.peerId
|
||||
@ -388,12 +388,9 @@ func enqueueMessages(transaction: Transaction, account: Account, peerId: PeerId,
|
||||
if let sourceMessage = sourceMessage, let author = sourceMessage.author ?? sourceMessage.peers[sourceMessage.id.peerId] {
|
||||
if let peer = peer as? TelegramSecretChat {
|
||||
var isAction = false
|
||||
var mediaDuration: Int32?
|
||||
for media in sourceMessage.media {
|
||||
if let _ = media as? TelegramMediaAction {
|
||||
isAction = true
|
||||
} else if let file = media as? TelegramMediaFile, let duration = file.duration {
|
||||
mediaDuration = duration
|
||||
}
|
||||
}
|
||||
if !disableAutoremove, let messageAutoremoveTimeout = peer.messageAutoremoveTimeout, !isAction {
|
||||
|
@ -21,6 +21,20 @@ public struct StandaloneUploadSecretFile {
|
||||
let key: SecretFileEncryptionKey
|
||||
}
|
||||
|
||||
public enum StandaloneUploadMediaThumbnailResult {
|
||||
case pending
|
||||
case file(Api.InputFile)
|
||||
case none
|
||||
|
||||
var file: Api.InputFile? {
|
||||
if case let .file(file) = self {
|
||||
return file
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum StandaloneUploadMediaResult {
|
||||
case media(AnyMediaReference)
|
||||
}
|
||||
@ -30,7 +44,22 @@ public enum StandaloneUploadMediaEvent {
|
||||
case result(StandaloneUploadMediaResult)
|
||||
}
|
||||
|
||||
public func standaloneUploadedImage(account: Account, peerId: PeerId, text: String, data: Data, dimensions: CGSize) -> Signal<StandaloneUploadMediaEvent, StandaloneUploadMediaError> {
|
||||
private func uploadedThumbnail(network: Network, postbox: Postbox, data: Data) -> Signal<Api.InputFile?, StandaloneUploadMediaError> {
|
||||
return multipartUpload(network: network, postbox: postbox, source: .data(data), encrypt: false, tag: TelegramMediaResourceFetchTag(statsCategory: .image), hintFileSize: nil, hintFileIsLarge: false)
|
||||
|> mapError { _ -> StandaloneUploadMediaError in return .generic }
|
||||
|> mapToSignal { result -> Signal<Api.InputFile?, StandaloneUploadMediaError> in
|
||||
switch result {
|
||||
case .progress:
|
||||
return .complete()
|
||||
case let .inputFile(inputFile):
|
||||
return .single(inputFile)
|
||||
case .inputSecretFile:
|
||||
return .single(nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public func standaloneUploadedImage(account: Account, peerId: PeerId, text: String, data: Data, thumbnailData: Data? = nil, dimensions: CGSize) -> Signal<StandaloneUploadMediaEvent, StandaloneUploadMediaError> {
|
||||
return multipartUpload(network: account.network, postbox: account.postbox, source: .data(data), encrypt: peerId.namespace == Namespaces.Peer.SecretChat, tag: TelegramMediaResourceFetchTag(statsCategory: .image), hintFileSize: nil, hintFileIsLarge: false)
|
||||
|> mapError { _ -> StandaloneUploadMediaError in return .generic }
|
||||
|> mapToSignal { next -> Signal<StandaloneUploadMediaEvent, StandaloneUploadMediaError> in
|
||||
@ -91,11 +120,39 @@ public func standaloneUploadedImage(account: Account, peerId: PeerId, text: Stri
|
||||
}
|
||||
}
|
||||
|
||||
public func standaloneUploadedFile(account: Account, peerId: PeerId, text: String, source: MultipartUploadSource, mimeType: String, attributes: [TelegramMediaFileAttribute], hintFileIsLarge: Bool) -> Signal<StandaloneUploadMediaEvent, StandaloneUploadMediaError> {
|
||||
return multipartUpload(network: account.network, postbox: account.postbox, source: source, encrypt: peerId.namespace == Namespaces.Peer.SecretChat, tag: TelegramMediaResourceFetchTag(statsCategory: statsCategoryForFileWithAttributes(attributes)), hintFileSize: nil, hintFileIsLarge: hintFileIsLarge)
|
||||
public func standaloneUploadedFile(account: Account, peerId: PeerId, text: String, source: MultipartUploadSource, thumbnailData: Data? = nil, mimeType: String, attributes: [TelegramMediaFileAttribute], hintFileIsLarge: Bool) -> Signal<StandaloneUploadMediaEvent, StandaloneUploadMediaError> {
|
||||
let upload = multipartUpload(network: account.network, postbox: account.postbox, source: source, encrypt: peerId.namespace == Namespaces.Peer.SecretChat, tag: TelegramMediaResourceFetchTag(statsCategory: statsCategoryForFileWithAttributes(attributes)), hintFileSize: nil, hintFileIsLarge: hintFileIsLarge)
|
||||
|> mapError { _ -> StandaloneUploadMediaError in return .generic }
|
||||
|> mapToSignal { next -> Signal<StandaloneUploadMediaEvent, StandaloneUploadMediaError> in
|
||||
switch next {
|
||||
|
||||
let uploadThumbnail: Signal<StandaloneUploadMediaThumbnailResult, StandaloneUploadMediaError>
|
||||
if let thumbnailData = thumbnailData {
|
||||
uploadThumbnail = .single(.pending)
|
||||
|> then(
|
||||
uploadedThumbnail(network: account.network, postbox: account.postbox, data: thumbnailData)
|
||||
|> mapError { _ -> StandaloneUploadMediaError in return .generic }
|
||||
|> map { result in
|
||||
if let result = result {
|
||||
return .file(result)
|
||||
} else {
|
||||
return .none
|
||||
}
|
||||
}
|
||||
)
|
||||
} else {
|
||||
uploadThumbnail = .single(.none)
|
||||
}
|
||||
|
||||
return combineLatest(upload, uploadThumbnail)
|
||||
|> mapToSignal { result, thumbnail in
|
||||
switch result {
|
||||
case let .progress(progress):
|
||||
return .single(.progress(progress))
|
||||
default:
|
||||
switch thumbnail {
|
||||
case .pending:
|
||||
return .complete()
|
||||
default:
|
||||
switch result {
|
||||
case let .inputFile(inputFile):
|
||||
return account.postbox.transaction { transaction -> Api.InputPeer? in
|
||||
return transaction.getPeer(peerId).flatMap(apiInputPeer)
|
||||
@ -103,7 +160,12 @@ public func standaloneUploadedFile(account: Account, peerId: PeerId, text: Strin
|
||||
|> mapError { _ -> StandaloneUploadMediaError in return .generic }
|
||||
|> mapToSignal { inputPeer -> Signal<StandaloneUploadMediaEvent, StandaloneUploadMediaError> in
|
||||
if let inputPeer = inputPeer {
|
||||
return account.network.request(Api.functions.messages.uploadMedia(peer: inputPeer, media: Api.InputMedia.inputMediaUploadedDocument(flags: 0, file: inputFile, thumb: nil, mimeType: mimeType, attributes: inputDocumentAttributesFromFileAttributes(attributes), stickers: nil, ttlSeconds: nil)))
|
||||
var flags: Int32 = 0
|
||||
let thumbnailFile = thumbnail.file
|
||||
if let _ = thumbnailFile {
|
||||
flags |= 1 << 2
|
||||
}
|
||||
return account.network.request(Api.functions.messages.uploadMedia(peer: inputPeer, media: Api.InputMedia.inputMediaUploadedDocument(flags: flags, file: inputFile, thumb: thumbnailFile, mimeType: mimeType, attributes: inputDocumentAttributesFromFileAttributes(attributes), stickers: nil, ttlSeconds: nil)))
|
||||
|> mapError { _ -> StandaloneUploadMediaError in return .generic }
|
||||
|> mapToSignal { media -> Signal<StandaloneUploadMediaEvent, StandaloneUploadMediaError> in
|
||||
switch media {
|
||||
@ -122,7 +184,7 @@ public func standaloneUploadedFile(account: Account, peerId: PeerId, text: Strin
|
||||
return .fail(.generic)
|
||||
}
|
||||
}
|
||||
case let .inputSecretFile(file, size, key):
|
||||
case let .inputSecretFile(file, _, key):
|
||||
return account.postbox.transaction { transaction -> Api.InputEncryptedChat? in
|
||||
if let peer = transaction.getPeer(peerId) as? TelegramSecretChat {
|
||||
return Api.InputEncryptedChat.inputEncryptedChat(chatId: peer.id.id, accessHash: peer.accessHash)
|
||||
@ -135,8 +197,7 @@ public func standaloneUploadedFile(account: Account, peerId: PeerId, text: Strin
|
||||
return .fail(.generic)
|
||||
}
|
||||
return account.network.request(Api.functions.messages.uploadEncryptedFile(peer: inputChat, file: file))
|
||||
|> mapError { _ -> StandaloneUploadMediaError in return .generic
|
||||
}
|
||||
|> mapError { _ -> StandaloneUploadMediaError in return .generic }
|
||||
|> mapToSignal { result -> Signal<StandaloneUploadMediaEvent, StandaloneUploadMediaError> in
|
||||
switch result {
|
||||
case let .encryptedFile(id, accessHash, size, dcId, _):
|
||||
@ -148,8 +209,10 @@ public func standaloneUploadedFile(account: Account, peerId: PeerId, text: Strin
|
||||
}
|
||||
}
|
||||
}
|
||||
case let .progress(progress):
|
||||
return .single(.progress(progress))
|
||||
case .progress:
|
||||
return .never()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user