Move media file loading logic to the SDK. (#702)

* Adopt getMediaFile for media previewing.
pull/709/head
Doug 2023-03-20 14:51:33 +00:00 committed by GitHub
parent bbd64092e7
commit efdb47a98a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
42 changed files with 427 additions and 519 deletions

View File

@ -0,0 +1,35 @@
//
// Copyright 2023 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import UniformTypeIdentifiers
extension UTType {
/// Creates a type based on an optional mime type, falling back to a filename when this type is missing or unknown.
init?(mimeType: String?, fallbackFilename: String) {
guard let mimeType, let type = UTType(mimeType: mimeType) else {
self.init(filename: fallbackFilename)
return
}
self = type
}
/// Creates a type based on a filename.
private init?(filename: String) {
let components = filename.split(separator: ".")
guard components.count > 1, let filenameExtension = components.last else { return nil }
self.init(filenameExtension: String(filenameExtension))
}
}

View File

@ -56,7 +56,7 @@ struct LoadableImage<TransformerView: View, PlaceholderView: View>: View {
imageProvider: ImageProviderProtocol?,
transformer: @escaping (Image) -> TransformerView = { $0 },
placeholder: @escaping () -> PlaceholderView) {
let mediaSource = url.map(MediaSourceProxy.init)
let mediaSource = url.map { MediaSourceProxy(url: $0, mimeType: nil) }
self.init(mediaSource: mediaSource,
blurhash: blurhash,

View File

@ -17,7 +17,7 @@
import SwiftUI
struct FilePreviewCoordinatorParameters {
let fileURL: URL
let mediaFile: MediaFileHandleProxy
let title: String?
}
@ -34,7 +34,7 @@ final class FilePreviewCoordinator: CoordinatorProtocol {
init(parameters: FilePreviewCoordinatorParameters) {
self.parameters = parameters
viewModel = FilePreviewViewModel(fileURL: parameters.fileURL, title: parameters.title)
viewModel = FilePreviewViewModel(mediaFile: parameters.mediaFile, title: parameters.title)
}
// MARK: - Public

View File

@ -21,7 +21,7 @@ enum FilePreviewViewModelAction {
}
struct FilePreviewViewState: BindableState {
let fileURL: URL
let mediaFile: MediaFileHandleProxy
let title: String?
}

View File

@ -21,8 +21,8 @@ typealias FilePreviewViewModelType = StateStoreViewModel<FilePreviewViewState, F
class FilePreviewViewModel: FilePreviewViewModelType, FilePreviewViewModelProtocol {
var callback: ((FilePreviewViewModelAction) -> Void)?
init(fileURL: URL, title: String? = nil) {
super.init(initialViewState: FilePreviewViewState(fileURL: fileURL, title: title))
init(mediaFile: MediaFileHandleProxy, title: String? = nil) {
super.init(initialViewState: FilePreviewViewState(mediaFile: mediaFile, title: title))
}
override func process(viewAction: FilePreviewViewAction) async {

View File

@ -24,7 +24,7 @@ struct FilePreviewScreen: View {
var body: some View {
ZStack(alignment: .leading) {
PreviewView(context: context,
fileURL: context.viewState.fileURL,
fileURL: context.viewState.mediaFile.url,
title: context.viewState.title)
.ignoresSafeArea(edges: .bottom)
@ -93,10 +93,9 @@ private class PreviewItem: NSObject, QLPreviewItem {
// MARK: - Previews
struct FilePreview_Previews: PreviewProvider {
static let viewModel = FilePreviewViewModel(mediaFile: .unmanaged(url: URL(staticString: "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf")))
static var previews: some View {
Group {
let upgradeViewModel = FilePreviewViewModel(fileURL: URL(staticString: "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"))
FilePreviewScreen(context: upgradeViewModel.context)
}
FilePreviewScreen(context: viewModel.context)
}
}

View File

@ -56,8 +56,8 @@ final class RoomScreenCoordinator: CoordinatorProtocol {
switch action {
case .displayRoomDetails:
self.displayRoomDetails()
case .displayVideo(let fileURL, let title), .displayFile(let fileURL, let title):
self.displayFile(for: fileURL, with: title)
case .displayMediaFile(let file, let title):
self.displayFilePreview(for: file, with: title)
case .displayEmojiPicker(let itemId):
self.displayEmojiPickerScreen(for: itemId)
case .displayReportContent(let itemId):
@ -77,8 +77,8 @@ final class RoomScreenCoordinator: CoordinatorProtocol {
// MARK: - Private
private func displayFile(for fileURL: URL, with title: String?) {
let params = FilePreviewCoordinatorParameters(fileURL: fileURL, title: title)
private func displayFilePreview(for file: MediaFileHandleProxy, with title: String?) {
let params = FilePreviewCoordinatorParameters(mediaFile: file, title: title)
let coordinator = FilePreviewCoordinator(parameters: params)
coordinator.callback = { [weak self] _ in
self?.navigationStackCoordinator.pop()

View File

@ -20,8 +20,7 @@ import UIKit
enum RoomScreenViewModelAction {
case displayRoomDetails
case displayVideo(videoURL: URL, title: String?)
case displayFile(fileURL: URL, title: String?)
case displayMediaFile(file: MediaFileHandleProxy, title: String?)
case displayEmojiPicker(itemId: String)
case displayReportContent(itemId: String)
}

View File

@ -134,10 +134,8 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol
let action = await timelineController.processItemTap(itemId)
switch action {
case .displayVideo(let videoURL, let title):
callback?(.displayVideo(videoURL: videoURL, title: title))
case .displayFile(let fileURL, let title):
callback?(.displayFile(fileURL: fileURL, title: title))
case .displayMediaFile(let file, let title):
callback?(.displayMediaFile(file: file, title: title))
case .none:
break
}

View File

@ -48,7 +48,7 @@ struct ImageRoomTimelineView: View {
@ViewBuilder
var overlay: some View {
if timelineItem.type == .gif {
if timelineItem.contentType == .gif {
Text(ElementL10n.roomTimelineImageGif)
.font(.element.bodyBold)
.foregroundStyle(.primary)
@ -96,7 +96,7 @@ struct ImageRoomTimelineView_Previews: PreviewProvider {
source: nil,
aspectRatio: 0.7,
blurhash: "L%KUc%kqS$RP?Ks,WEf8OlrqaekW",
type: .gif))
contentType: .gif))
}
}
}

View File

@ -1,74 +0,0 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
// MARK: - FileCacheProtocol
protocol FileCacheProtocol {
func file(forKey key: String, fileExtension: String) -> URL?
func store(_ data: Data, with fileExtension: String, forKey key: String) throws -> URL
func remove(forKey key: String, fileExtension: String) throws
func removeAll() throws
}
// MARK: - FileCache
/// Implementation of `FileCacheProtocol` under `FileManager.default.temporaryDirectory`.
class FileCache {
private let fileManager = FileManager.default
private let folder: URL
/// Default instance. Uses `Files` as the folder name.
static let `default` = FileCache(folderName: "Files")
init(folderName: String) {
folder = URL.cacheBaseDirectory.appending(path: folderName, directoryHint: .isDirectory)
}
// MARK: Private
private func filePath(forKey key: String, fileExtension: String) -> URL {
folder.appending(path: key, directoryHint: .notDirectory).appendingPathExtension(fileExtension)
}
}
// MARK: - FileCacheProtocol
extension FileCache: FileCacheProtocol {
func file(forKey key: String, fileExtension: String) -> URL? {
let url = filePath(forKey: key, fileExtension: fileExtension)
return fileManager.isReadableFile(atPath: url.path()) ? url : nil
}
func store(_ data: Data, with fileExtension: String, forKey key: String) throws -> URL {
try fileManager.createDirectoryIfNeeded(at: folder)
let url = filePath(forKey: key, fileExtension: fileExtension)
try data.write(to: url)
return url
}
func remove(forKey key: String, fileExtension: String) throws {
try fileManager.removeItem(at: filePath(forKey: key, fileExtension: fileExtension))
}
func removeAll() throws {
guard fileManager.directoryExists(at: folder) else {
return
}
try fileManager.removeItem(at: folder)
}
}

View File

@ -394,4 +394,8 @@ extension ClientProxy: MediaLoaderProtocol {
func loadMediaThumbnailForSource(_ source: MediaSourceProxy, width: UInt, height: UInt) async throws -> Data {
try await mediaLoader.loadMediaThumbnailForSource(source, width: width, height: height)
}
func loadMediaFileForSource(_ source: MediaSourceProxy) async throws -> MediaFileHandleProxy {
try await mediaLoader.loadMediaFileForSource(source)
}
}

View File

@ -76,6 +76,10 @@ class MockClientProxy: ClientProxyProtocol {
throw ClientProxyError.failedLoadingMedia
}
func loadMediaFileForSource(_ source: MediaSourceProxy) async throws -> MediaFileHandleProxy {
throw ClientProxyError.failedLoadingMedia
}
var sessionVerificationControllerProxyResult: Result<SessionVerificationControllerProxyProtocol, ClientProxyError>?
func sessionVerificationControllerProxy() async -> Result<SessionVerificationControllerProxyProtocol, ClientProxyError> {
if let sessionVerificationControllerProxyResult {

View File

@ -19,7 +19,7 @@ import UIKit
protocol ImageProviderProtocol {
func imageFromSource(_ source: MediaSourceProxy?, size: CGSize?) -> UIImage?
@discardableResult func loadImageFromSource(_ source: MediaSourceProxy, size: CGSize?) async -> Result<UIImage, MediaProviderError>
func loadImageFromSource(_ source: MediaSourceProxy, size: CGSize?) async -> Result<UIImage, MediaProviderError>
}
extension ImageProviderProtocol {
@ -27,7 +27,7 @@ extension ImageProviderProtocol {
imageFromSource(source, size: nil)
}
@discardableResult func loadImageFromSource(_ source: MediaSourceProxy) async -> Result<UIImage, MediaProviderError> {
func loadImageFromSource(_ source: MediaSourceProxy) async -> Result<UIImage, MediaProviderError> {
await loadImageFromSource(source, size: nil)
}
}

View File

@ -0,0 +1,68 @@
//
// Copyright 2023 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import MatrixRustSDK
/// A wrapper around Rust's `MediaFileHandle` type that provides us with a
/// media file that is stored unencrypted in a temporary location for previewing.
class MediaFileHandleProxy {
/// The underlying handle for the file.
private let handle: MediaFileHandleProtocol
/// Creates a new instance from the Rust type.
init(handle: MediaFileHandleProtocol) {
self.handle = handle
}
/// Creates an unmanaged instance (for mocking etc), using a raw `URL`
///
/// A media file created from a URL won't have the automatic clean-up mechanism
/// that is provided by the SDK's `MediaFileHandle`.
static func unmanaged(url: URL) -> MediaFileHandleProxy {
MediaFileHandleProxy(handle: UnmanagedMediaFileHandle(url: url))
}
/// The media file's location on disk.
var url: URL {
URL(filePath: handle.path())
}
}
// MARK: - Hashable
extension MediaFileHandleProxy: Hashable {
static func == (lhs: MediaFileHandleProxy, rhs: MediaFileHandleProxy) -> Bool {
lhs.url == rhs.url
}
func hash(into hasher: inout Hasher) {
hasher.combine(url)
}
}
// MARK: -
/// An unmanaged file handle that can be created direct from a URL.
///
/// This type allows for mocking but doesn't provide the automatic clean-up mechanism provided by the SDK.
private struct UnmanagedMediaFileHandle: MediaFileHandleProtocol {
let url: URL
func path() -> String {
url.path()
}
}

View File

@ -46,6 +46,14 @@ actor MediaLoader: MediaLoaderProtocol {
}
}
func loadMediaFileForSource(_ source: MediaSourceProxy) async throws -> MediaFileHandleProxy {
let result = try await Task.dispatch(on: clientQueue) {
try self.client.getMediaFile(source: source.underlyingSource, mimeType: source.mimeType ?? "application/octet-stream")
}
return MediaFileHandleProxy(handle: result)
}
// MARK: - Private
private func enqueueLoadMediaRequest(forSource source: MediaSourceProxy, operation: @escaping () throws -> [UInt8]) async throws -> Data {

View File

@ -20,4 +20,6 @@ protocol MediaLoaderProtocol {
func loadMediaContentForSource(_ source: MediaSourceProxy) async throws -> Data
func loadMediaThumbnailForSource(_ source: MediaSourceProxy, width: UInt, height: UInt) async throws -> Data
func loadMediaFileForSource(_ source: MediaSourceProxy) async throws -> MediaFileHandleProxy
}

View File

@ -20,16 +20,13 @@ import UIKit
struct MediaProvider: MediaProviderProtocol {
private let mediaLoader: MediaLoaderProtocol
private let imageCache: Kingfisher.ImageCache
private let fileCache: FileCacheProtocol
private let backgroundTaskService: BackgroundTaskServiceProtocol?
init(mediaLoader: MediaLoaderProtocol,
imageCache: Kingfisher.ImageCache,
fileCache: FileCacheProtocol,
backgroundTaskService: BackgroundTaskServiceProtocol?) {
self.mediaLoader = mediaLoader
self.imageCache = imageCache
self.fileCache = fileCache
self.backgroundTaskService = backgroundTaskService
}
@ -83,35 +80,17 @@ struct MediaProvider: MediaProviderProtocol {
}
// MARK: Files
func fileFromSource(_ source: MediaSourceProxy?, fileExtension: String) -> URL? {
guard let source else {
return nil
}
let cacheKey = fileCacheKeyForURL(source.url)
return fileCache.file(forKey: cacheKey, fileExtension: fileExtension)
}
@discardableResult func loadFileFromSource(_ source: MediaSourceProxy, fileExtension: String) async -> Result<URL, MediaProviderError> {
if let url = fileFromSource(source, fileExtension: fileExtension) {
return .success(url)
}
func loadFileFromSource(_ source: MediaSourceProxy) async -> Result<MediaFileHandleProxy, MediaProviderError> {
let loadFileBgTask = await backgroundTaskService?.startBackgroundTask(withName: "LoadFile: \(source.url.hashValue)")
defer {
loadFileBgTask?.stop()
}
let cacheKey = fileCacheKeyForURL(source.url)
defer { loadFileBgTask?.stop() }
do {
let data = try await mediaLoader.loadMediaContentForSource(source)
let url = try fileCache.store(data, with: fileExtension, forKey: cacheKey)
return .success(url)
let file = try await mediaLoader.loadMediaFileForSource(source)
return .success(file)
} catch {
MXLog.error("Failed retrieving file with error: \(error)")
return .failure(.failedRetrievingImage)
return .failure(.failedRetrievingFile)
}
}
@ -124,14 +103,6 @@ struct MediaProvider: MediaProviderProtocol {
return url.absoluteString
}
}
private func fileCacheKeyForURL(_ url: URL) -> String {
let component = url.lastPathComponent
guard !component.isEmpty else {
return url.absoluteString
}
return String(component)
}
}
private extension ImageCache {

View File

@ -24,7 +24,5 @@ enum MediaProviderError: Error {
}
protocol MediaProviderProtocol: ImageProviderProtocol {
func fileFromSource(_ source: MediaSourceProxy?, fileExtension: String) -> URL?
@discardableResult func loadFileFromSource(_ source: MediaSourceProxy, fileExtension: String) async -> Result<URL, MediaProviderError>
func loadFileFromSource(_ source: MediaSourceProxy) async -> Result<MediaFileHandleProxy, MediaProviderError>
}

View File

@ -18,14 +18,20 @@ import Foundation
import MatrixRustSDK
struct MediaSourceProxy: Hashable {
/// The media source provided by Rust.
let underlyingSource: MediaSource
/// The media's mime type, used when loading the media's file.
/// This is optional when loading images and thumbnails in memory.
let mimeType: String?
init(source: MediaSource) {
init(source: MediaSource, mimeType: String?) {
underlyingSource = source
self.mimeType = mimeType
}
init(url: URL) {
init(url: URL, mimeType: String?) {
underlyingSource = mediaSourceFromUrl(url: url.absoluteString)
self.mimeType = mimeType
}
var url: URL! {

View File

@ -41,12 +41,8 @@ struct MockMediaProvider: MediaProviderProtocol {
return .success(image)
}
func fileFromSource(_ source: MediaSourceProxy?, fileExtension: String) -> URL? {
nil
}
@discardableResult func loadFileFromSource(_ source: MediaSourceProxy, fileExtension: String) async -> Result<URL, MediaProviderError> {
func loadFileFromSource(_ source: MediaSourceProxy) async -> Result<MediaFileHandleProxy, MediaProviderError> {
.failure(.failedRetrievingFile)
}
}

View File

@ -17,7 +17,6 @@
import Combine
import Foundation
import UIKit
import UniformTypeIdentifiers
class RoomTimelineController: RoomTimelineControllerProtocol {
private let userId: String
@ -105,44 +104,34 @@ class RoomTimelineController: RoomTimelineControllerProtocol {
guard let timelineItem = timelineItems.first(where: { $0.id == itemID }) else {
return .none
}
var source: MediaSourceProxy?
var title: String
switch timelineItem {
case let item as ImageRoomTimelineItem:
await loadFileForImageTimelineItem(item)
guard let index = timelineItems.firstIndex(where: { $0.id == itemID }),
let item = timelineItems[index] as? ImageRoomTimelineItem,
let fileURL = item.cachedFileURL else {
return .none
}
return .displayFile(fileURL: fileURL, title: item.body)
source = item.source
title = item.body
case let item as VideoRoomTimelineItem:
await loadVideoForTimelineItem(item)
guard let index = timelineItems.firstIndex(where: { $0.id == itemID }),
let item = timelineItems[index] as? VideoRoomTimelineItem,
let videoURL = item.cachedVideoURL else {
return .none
}
return .displayVideo(videoURL: videoURL, title: item.body)
source = item.source
title = item.body
case let item as FileRoomTimelineItem:
await loadFileForTimelineItem(item)
guard let index = timelineItems.firstIndex(where: { $0.id == itemID }),
let item = timelineItems[index] as? FileRoomTimelineItem,
let fileURL = item.cachedFileURL else {
return .none
}
return .displayFile(fileURL: fileURL, title: item.body)
source = item.source
title = item.body
case let item as AudioRoomTimelineItem:
await loadAudioForTimelineItem(item)
guard let index = timelineItems.firstIndex(where: { $0.id == itemID }),
let item = timelineItems[index] as? AudioRoomTimelineItem,
let audioURL = item.cachedAudioURL else {
return .none
}
// For now we are just displaying audio messages with the File preview until we create a timeline player for them.
return .displayFile(fileURL: audioURL, title: item.body)
source = item.source
title = item.body
default:
return .none
}
guard let source else { return .none }
switch await mediaProvider.loadFileFromSource(source) {
case .success(let file):
return .displayMediaFile(file: file, title: title)
case .failure:
return .none
}
}
func sendMessage(_ message: String, inReplyTo itemID: String?) async {
@ -305,134 +294,4 @@ class RoomTimelineController: RoomTimelineControllerProtocol {
return false
}
private func loadVideoForTimelineItem(_ timelineItem: VideoRoomTimelineItem) async {
if timelineItem.cachedVideoURL != nil {
// already cached
return
}
guard let source = timelineItem.source else {
return
}
let fileExtension = movieFileExtension(for: timelineItem.body)
switch await mediaProvider.loadFileFromSource(source, fileExtension: fileExtension) {
case .success(let fileURL):
guard let index = timelineItems.firstIndex(where: { $0.id == timelineItem.id }),
var item = timelineItems[index] as? VideoRoomTimelineItem else {
return
}
item.cachedVideoURL = fileURL
timelineItems[index] = item
case .failure:
break
}
}
/// Temporary method that generates a file extension for a video file name
/// using `UTType.movie` and falls back to .mp4 if anything goes wrong.
///
/// Ideally Rust should be able to handle this for us, otherwise we should be
/// attempting to detect the file type from the data itself.
private func movieFileExtension(for text: String) -> String {
let fallbackExtension = "mp4"
// This is not great. We could better estimate file extension from the mimetype.
guard let fileExtensionComponent = text.split(separator: ".").last else { return fallbackExtension }
let fileExtension = String(fileExtensionComponent)
// We can't trust that the extension provided is an extension that AVFoundation will accept.
guard let fileType = UTType(filenameExtension: fileExtension),
fileType.isSubtype(of: .movie)
else { return fallbackExtension }
return fileExtension
}
private func loadFileForImageTimelineItem(_ timelineItem: ImageRoomTimelineItem) async {
if timelineItem.cachedFileURL != nil {
// already cached
return
}
guard let source = timelineItem.source else {
return
}
// This is not great. We could better estimate file extension from the mimetype.
guard let fileExtension = timelineItem.body.split(separator: ".").last else {
return
}
switch await mediaProvider.loadFileFromSource(source, fileExtension: String(fileExtension)) {
case .success(let fileURL):
guard let index = timelineItems.firstIndex(where: { $0.id == timelineItem.id }),
var item = timelineItems[index] as? ImageRoomTimelineItem else {
return
}
item.cachedFileURL = fileURL
timelineItems[index] = item
case .failure:
break
}
}
private func loadFileForTimelineItem(_ timelineItem: FileRoomTimelineItem) async {
if timelineItem.cachedFileURL != nil {
// already cached
return
}
guard let source = timelineItem.source else {
return
}
// This is not great. We could better estimate file extension from the mimetype.
guard let fileExtension = timelineItem.body.split(separator: ".").last else {
return
}
switch await mediaProvider.loadFileFromSource(source, fileExtension: String(fileExtension)) {
case .success(let fileURL):
guard let index = timelineItems.firstIndex(where: { $0.id == timelineItem.id }),
var item = timelineItems[index] as? FileRoomTimelineItem else {
return
}
item.cachedFileURL = fileURL
timelineItems[index] = item
case .failure:
break
}
}
private func loadAudioForTimelineItem(_ timelineItem: AudioRoomTimelineItem) async {
if timelineItem.cachedAudioURL != nil {
// already cached
return
}
guard let source = timelineItem.source else {
return
}
// This is not great. We could better estimate file extension from the mimetype.
guard let fileExtension = timelineItem.body.split(separator: ".").last else {
return
}
switch await mediaProvider.loadFileFromSource(source, fileExtension: String(fileExtension)) {
case .success(let audioURL):
guard let index = timelineItems.firstIndex(where: { $0.id == timelineItem.id }),
var item = timelineItems[index] as? AudioRoomTimelineItem else {
return
}
item.cachedAudioURL = audioURL
timelineItems[index] = item
case .failure:
break
}
}
}

View File

@ -25,8 +25,7 @@ enum RoomTimelineControllerCallback {
}
enum RoomTimelineControllerAction {
case displayVideo(videoURL: URL, title: String?)
case displayFile(fileURL: URL, title: String?)
case displayMediaFile(file: MediaFileHandleProxy, title: String?)
case none
}

View File

@ -27,7 +27,7 @@ protocol MessageContentProtocol: RoomMessageEventContentProtocol, CustomStringCo
/// A timeline item that represents an `m.room.message` event.
struct MessageTimelineItem<Content: MessageContentProtocol> {
let item: MatrixRustSDK.EventTimelineItem
let item: MatrixRustSDK.EventTimelineItemProtocol
let content: Content
var id: String {
@ -82,7 +82,7 @@ extension MatrixRustSDK.ImageMessageContent: MessageContentProtocol { }
/// A timeline item that represents an `m.room.message` event with a `msgtype` of `m.image`.
extension MessageTimelineItem where Content == MatrixRustSDK.ImageMessageContent {
var source: MediaSourceProxy {
.init(source: content.source)
.init(source: content.source, mimeType: content.info?.mimetype)
}
var width: CGFloat? {
@ -97,8 +97,8 @@ extension MessageTimelineItem where Content == MatrixRustSDK.ImageMessageContent
content.info?.blurhash
}
var type: UTType? {
content.info?.mimetype.flatMap { UTType(mimeType: $0) }
var contentType: UTType? {
UTType(mimeType: content.info?.mimetype, fallbackFilename: content.body)
}
}
@ -107,14 +107,14 @@ extension MatrixRustSDK.VideoMessageContent: MessageContentProtocol { }
/// A timeline item that represents an `m.room.message` event with a `msgtype` of `m.video`.
extension MessageTimelineItem where Content == MatrixRustSDK.VideoMessageContent {
var source: MediaSourceProxy {
.init(source: content.source)
.init(source: content.source, mimeType: content.info?.mimetype)
}
var thumbnailSource: MediaSourceProxy? {
guard let src = content.info?.thumbnailSource else {
return nil
}
return .init(source: src)
return .init(source: src, mimeType: content.info?.thumbnailInfo?.mimetype)
}
var duration: UInt64 {
@ -132,6 +132,10 @@ extension MessageTimelineItem where Content == MatrixRustSDK.VideoMessageContent
var blurhash: String? {
content.info?.blurhash
}
var contentType: UTType? {
UTType(mimeType: content.info?.mimetype, fallbackFilename: content.body)
}
}
extension MatrixRustSDK.FileMessageContent: MessageContentProtocol { }
@ -139,14 +143,18 @@ extension MatrixRustSDK.FileMessageContent: MessageContentProtocol { }
/// A timeline item that represents an `m.room.message` event with a `msgtype` of `m.file`.
extension MessageTimelineItem where Content == MatrixRustSDK.FileMessageContent {
var source: MediaSourceProxy {
.init(source: content.source)
.init(source: content.source, mimeType: content.info?.mimetype)
}
var thumbnailSource: MediaSourceProxy? {
guard let src = content.info?.thumbnailSource else {
return nil
}
return .init(source: src)
return .init(source: src, mimeType: content.info?.thumbnailInfo?.mimetype)
}
var contentType: UTType? {
UTType(mimeType: content.info?.mimetype, fallbackFilename: content.body)
}
}
@ -155,10 +163,14 @@ extension MatrixRustSDK.AudioMessageContent: MessageContentProtocol { }
/// A timeline item that represents an `m.room.message` event with a `msgtype` of `m.audio`.
extension MessageTimelineItem where Content == MatrixRustSDK.AudioMessageContent {
var source: MediaSourceProxy {
.init(source: content.source)
.init(source: content.source, mimeType: content.info?.mimetype)
}
var duration: UInt64 {
content.info?.duration ?? 0
}
var contentType: UTType? {
UTType(mimeType: content.info?.mimetype, fallbackFilename: content.body)
}
}

View File

@ -15,6 +15,7 @@
//
import Foundation
import UniformTypeIdentifiers
struct AudioRoomTimelineItem: EventBasedTimelineItemProtocol, Identifiable, Hashable {
let id: String
@ -26,7 +27,7 @@ struct AudioRoomTimelineItem: EventBasedTimelineItemProtocol, Identifiable, Hash
let duration: UInt64
let source: MediaSourceProxy?
var cachedAudioURL: URL?
var contentType: UTType?
var properties = RoomTimelineItemProperties()
}

View File

@ -15,6 +15,7 @@
//
import UIKit
import UniformTypeIdentifiers
struct FileRoomTimelineItem: EventBasedTimelineItemProtocol, Identifiable, Hashable {
let id: String
@ -27,7 +28,7 @@ struct FileRoomTimelineItem: EventBasedTimelineItemProtocol, Identifiable, Hasha
let source: MediaSourceProxy?
let thumbnailSource: MediaSourceProxy?
var cachedFileURL: URL?
var contentType: UTType?
var properties = RoomTimelineItemProperties()
}

View File

@ -27,13 +27,12 @@ struct ImageRoomTimelineItem: EventBasedTimelineItemProtocol, Identifiable, Hash
let sender: TimelineItemSender
let source: MediaSourceProxy?
var cachedFileURL: URL?
var width: CGFloat?
var height: CGFloat?
var aspectRatio: CGFloat?
var blurhash: String?
var type: UTType?
var contentType: UTType?
var properties = RoomTimelineItemProperties()
}

View File

@ -15,6 +15,7 @@
//
import UIKit
import UniformTypeIdentifiers
struct VideoRoomTimelineItem: EventBasedTimelineItemProtocol, Identifiable, Hashable {
let id: String
@ -28,12 +29,12 @@ struct VideoRoomTimelineItem: EventBasedTimelineItemProtocol, Identifiable, Hash
let duration: UInt64
let source: MediaSourceProxy?
let thumbnailSource: MediaSourceProxy?
var cachedVideoURL: URL?
var width: CGFloat?
var height: CGFloat?
var aspectRatio: CGFloat?
var blurhash: String?
var contentType: UTType?
var properties = RoomTimelineItemProperties()
}

View File

@ -222,7 +222,7 @@ struct RoomTimelineItemFactory: RoomTimelineItemFactoryProtocol {
height: message.height,
aspectRatio: aspectRatio,
blurhash: message.blurhash,
type: message.type,
contentType: message.contentType,
properties: RoomTimelineItemProperties(isEdited: message.isEdited,
reactions: aggregateReactions(eventItemProxy.reactions),
deliveryStatus: eventItemProxy.deliveryStatus))
@ -250,6 +250,7 @@ struct RoomTimelineItemFactory: RoomTimelineItemFactoryProtocol {
height: message.height,
aspectRatio: aspectRatio,
blurhash: message.blurhash,
contentType: message.contentType,
properties: RoomTimelineItemProperties(isEdited: message.isEdited,
reactions: aggregateReactions(eventItemProxy.reactions),
deliveryStatus: eventItemProxy.deliveryStatus))
@ -265,7 +266,11 @@ struct RoomTimelineItemFactory: RoomTimelineItemFactoryProtocol {
isEditable: eventItemProxy.isEditable,
sender: eventItemProxy.sender,
duration: message.duration,
source: message.source)
source: message.source,
contentType: message.contentType,
properties: RoomTimelineItemProperties(isEdited: message.isEdited,
reactions: aggregateReactions(eventItemProxy.reactions),
deliveryStatus: eventItemProxy.deliveryStatus))
}
private func buildFileTimelineItemFromMessage(_ eventItemProxy: EventTimelineItemProxy,
@ -279,6 +284,7 @@ struct RoomTimelineItemFactory: RoomTimelineItemFactoryProtocol {
sender: eventItemProxy.sender,
source: message.source,
thumbnailSource: message.thumbnailSource,
contentType: message.contentType,
properties: RoomTimelineItemProperties(isEdited: message.isEdited,
reactions: aggregateReactions(eventItemProxy.reactions),
deliveryStatus: eventItemProxy.deliveryStatus))

View File

@ -98,7 +98,6 @@ class UserSessionStore: UserSessionStoreProtocol {
return UserSession(clientProxy: clientProxy,
mediaProvider: MediaProvider(mediaLoader: clientProxy,
imageCache: imageCache,
fileCache: FileCache.default,
backgroundTaskService: backgroundTaskService))
}

View File

@ -130,7 +130,6 @@ class NotificationServiceExtension: UNNotificationServiceExtension {
return MediaProvider(mediaLoader: MediaLoader(client: client),
imageCache: .onlyOnDisk,
fileCache: FileCache.default,
backgroundTaskService: nil)
}

View File

@ -171,7 +171,7 @@ extension NotificationItemProxy {
notification.body = "📷 " + content.body
notification = try await notification.addMediaAttachment(using: mediaProvider,
mediaSource: .init(source: content.source))
mediaSource: .init(source: content.source, mimeType: content.info?.mimetype))
return notification
}
@ -186,7 +186,7 @@ extension NotificationItemProxy {
notification.body = "📹 " + content.body
notification = try await notification.addMediaAttachment(using: mediaProvider,
mediaSource: .init(source: content.source))
mediaSource: .init(source: content.source, mimeType: content.info?.mimetype))
return notification
}

View File

@ -24,11 +24,10 @@ extension UNMutableNotificationContent {
guard let mediaProvider else {
return self
}
switch await mediaProvider.loadFileFromSource(mediaSource, fileExtension: "") {
case .success(let url):
switch await mediaProvider.loadFileFromSource(mediaSource) {
case .success(let file):
let attachment = try UNNotificationAttachment(identifier: ProcessInfo.processInfo.globallyUniqueString,
url: url,
url: file.url, // Needs testing: Does the file get copied before the media handle is be dropped?
options: nil)
attachments.append(attachment)
case .failure(let error):
@ -47,14 +46,14 @@ extension UNMutableNotificationContent {
return self
}
switch await mediaProvider.loadFileFromSource(mediaSource, fileExtension: "jpg") {
case .success(let url):
switch await mediaProvider.loadFileFromSource(mediaSource) {
case .success(let mediaFile):
// Initialize only the sender for a one-to-one message intent.
let handle = INPersonHandle(value: senderId, type: .unknown)
let sender = try INPerson(personHandle: handle,
nameComponents: nil,
displayName: senderName,
image: INImage(imageData: Data(contentsOf: url)),
image: INImage(imageData: Data(contentsOf: mediaFile.url)),
contactIdentifier: nil,
customIdentifier: nil)

View File

@ -79,7 +79,6 @@ targets:
- path: ../../ElementX/Sources/Services/Media
- path: ../../ElementX/Sources/Services/Background/BackgroundTaskServiceProtocol.swift
- path: ../../ElementX/Sources/Services/Background/BackgroundTaskProtocol.swift
- path: ../../ElementX/Sources/Services/Cache/FileCache.swift
- path: ../../ElementX/Sources/Other/Logging
- path: ../../ElementX/Sources/Other/Extensions/Task.swift
- path: ../../ElementX/Sources/Other/Extensions/FileManager.swift
@ -87,5 +86,6 @@ targets:
- path: ../../ElementX/Sources/Other/Extensions/Bundle.swift
- path: ../../ElementX/Sources/Other/Extensions/Date.swift
- path: ../../ElementX/Sources/Other/Extensions/ImageCache.swift
- path: ../../ElementX/Sources/Other/Extensions/UTType.swift
- path: ../../ElementX/Sources/Other/AvatarSize.swift
- path: ../../ElementX/Sources/Other/InfoPlistReader.swift

View File

@ -1,78 +0,0 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import XCTest
@testable import ElementX
class FileCacheTests: XCTestCase {
private var cache: FileCache!
override func setUp() {