mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-06-16 05:55:20 +00:00
113 lines
3.5 KiB
Swift
113 lines
3.5 KiB
Swift
import Foundation
|
|
import UIKit
|
|
import Postbox
|
|
import TelegramCore
|
|
import SyncCore
|
|
import LegacyComponents
|
|
import SwiftSignalKit
|
|
|
|
public class VideoConversionWatcher: TGMediaVideoFileWatcher {
|
|
private let update: (String, Int) -> Void
|
|
private var path: String?
|
|
|
|
public init(update: @escaping (String, Int) -> Void) {
|
|
self.update = update
|
|
|
|
super.init()
|
|
}
|
|
|
|
override public func setup(withFileURL fileURL: URL!) {
|
|
self.path = fileURL?.path
|
|
super.setup(withFileURL: fileURL)
|
|
}
|
|
|
|
override public func fileUpdated(_ completed: Bool) -> Any! {
|
|
if let path = self.path {
|
|
var value = stat()
|
|
if stat(path, &value) == 0 {
|
|
self.update(path, Int(value.st_size))
|
|
}
|
|
}
|
|
|
|
return super.fileUpdated(completed)
|
|
}
|
|
}
|
|
|
|
public final class LegacyLiveUploadInterfaceResult: NSObject {
|
|
public let id: Int64
|
|
|
|
init(id: Int64) {
|
|
self.id = id
|
|
|
|
super.init()
|
|
}
|
|
}
|
|
|
|
public final class LegacyLiveUploadInterface: VideoConversionWatcher, TGLiveUploadInterface {
|
|
private let account: Account
|
|
private let id: Int64
|
|
private var path: String?
|
|
private var size: Int?
|
|
|
|
private let data = Promise<MediaResourceData>()
|
|
private let dataValue = Atomic<MediaResourceData?>(value: nil)
|
|
|
|
public init(account: Account) {
|
|
self.account = account
|
|
self.id = Int64.random(in: Int64.min ... Int64.max)
|
|
|
|
var updateImpl: ((String, Int) -> Void)?
|
|
super.init(update: { path, size in
|
|
updateImpl?(path, size)
|
|
})
|
|
|
|
updateImpl = { [weak self] path, size in
|
|
if let strongSelf = self {
|
|
if strongSelf.path == nil {
|
|
strongSelf.path = path
|
|
strongSelf.account.messageMediaPreuploadManager.add(network: strongSelf.account.network, postbox: strongSelf.account.postbox, id: strongSelf.id, encrypt: false, tag: nil, source: strongSelf.data.get())
|
|
}
|
|
strongSelf.size = size
|
|
|
|
let result = strongSelf.dataValue.modify { dataValue in
|
|
if let dataValue = dataValue, dataValue.complete {
|
|
return MediaResourceData(path: path, offset: 0, size: size, complete: true)
|
|
} else {
|
|
return MediaResourceData(path: path, offset: 0, size: size, complete: false)
|
|
}
|
|
}
|
|
if let result = result {
|
|
print("**set1 \(result) \(result.complete)")
|
|
strongSelf.data.set(.single(result))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
deinit {
|
|
}
|
|
|
|
override public func fileUpdated(_ completed: Bool) -> Any! {
|
|
let _ = super.fileUpdated(completed)
|
|
print("**fileUpdated \(completed)")
|
|
if completed {
|
|
let result = self.dataValue.modify { dataValue in
|
|
if let dataValue = dataValue {
|
|
return MediaResourceData(path: dataValue.path, offset: dataValue.offset, size: dataValue.size, complete: true)
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
if let result = result {
|
|
print("**set2 \(result) \(completed)")
|
|
self.data.set(.single(result))
|
|
return LegacyLiveUploadInterfaceResult(id: self.id)
|
|
} else {
|
|
return nil
|
|
}
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
}
|