mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-04 01:20:12 +00:00
Move media file loading logic to the SDK. (#702)
* Adopt getMediaFile for media previewing.
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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>
|
||||
}
|
||||
|
||||
@@ -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! {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user