From 537b404886546dadefb632d843a21ac6742ec1d7 Mon Sep 17 00:00:00 2001 From: Mauro Romito Date: Tue, 2 Jul 2024 16:10:05 +0200 Subject: [PATCH 1/8] implemented for the preview url API --- MatrixSDK/MXEnumConstants.h | 5 +++ MatrixSDK/MXEnumConstants.m | 1 + MatrixSDK/MXRestClient.h | 6 ++++ MatrixSDK/MXRestClient.m | 68 +++++++++++++++++++++++++------------ 4 files changed, 59 insertions(+), 21 deletions(-) diff --git a/MatrixSDK/MXEnumConstants.h b/MatrixSDK/MXEnumConstants.h index f41f89023d..6151c12625 100644 --- a/MatrixSDK/MXEnumConstants.h +++ b/MatrixSDK/MXEnumConstants.h @@ -33,6 +33,11 @@ FOUNDATION_EXPORT NSString *const kMXContentUriScheme; */ FOUNDATION_EXPORT NSString *const kMXContentPrefixPath; +/** + A constant representing the default prefix of the Matrix authenticated content repository path. + */ +FOUNDATION_EXPORT NSString *const kMXAuthenticatedContentPrefixPath; + /** A constant representing the URI path for as-yet unspecified of the AntiVirus Client-Server HTTP API. */ diff --git a/MatrixSDK/MXEnumConstants.m b/MatrixSDK/MXEnumConstants.m index 88cb32e176..5853507c4a 100644 --- a/MatrixSDK/MXEnumConstants.m +++ b/MatrixSDK/MXEnumConstants.m @@ -23,6 +23,7 @@ */ NSString *const kMXContentUriScheme = @"mxc://"; NSString *const kMXContentPrefixPath = @"_matrix/media/r0"; +NSString *const kMXAuthenticatedContentPrefixPath = @"_matrix/client/unstable/org.matrix.msc3916/media"; /** Prefix used in path of antivirus server API requests. diff --git a/MatrixSDK/MXRestClient.h b/MatrixSDK/MXRestClient.h index a44ccbfa54..d6b91d6726 100644 --- a/MatrixSDK/MXRestClient.h +++ b/MatrixSDK/MXRestClient.h @@ -229,6 +229,12 @@ extern NSString *const kMXCredentialsNewRefreshTokenDataKey; */ @property (nonatomic) NSString *contentPathPrefix; +/** + The Matrix content repository prefix to use for authenticated access. + By default, it is defined by the constant kMXContentPrefixPath. + */ +@property (nonatomic) NSString *authenticatedContentPathPrefix; + /** The current trusted certificate (if any). */ diff --git a/MatrixSDK/MXRestClient.m b/MatrixSDK/MXRestClient.m index a4c50fbfc5..e3820cce25 100644 --- a/MatrixSDK/MXRestClient.m +++ b/MatrixSDK/MXRestClient.m @@ -145,7 +145,7 @@ @interface MXRestClient () @end @implementation MXRestClient -@synthesize credentials, apiPathPrefix, contentPathPrefix, completionQueue, antivirusServerPathPrefix; +@synthesize credentials, apiPathPrefix, contentPathPrefix, authenticatedContentPathPrefix, completionQueue, antivirusServerPathPrefix; + (dispatch_queue_t)refreshQueue { @@ -198,6 +198,7 @@ -(id)initWithCredentials:(MXCredentials*)inCredentials apiPathPrefix = kMXAPIPrefixPathR0; antivirusServerPathPrefix = kMXAntivirusAPIPrefixPathUnstable; contentPathPrefix = kMXContentPrefixPath; + authenticatedContentPathPrefix = kMXAuthenticatedContentPrefixPath; credentials = inCredentials; _identityServer = credentials.identityServer; @@ -4500,26 +4501,51 @@ - (MXHTTPOperation *)previewForURL:(NSURL *)url parameters[@"url"] = [url absoluteString]; MXWeakify(self); - return [httpClient requestWithMethod:@"GET" - path:[NSString stringWithFormat:@"%@/preview_url", contentPathPrefix] - parameters:parameters - success:^(NSDictionary *JSONResponse) { - MXStrongifyAndReturnIfNil(self); - - if (success) - { - __block MXURLPreview *urlPreview; - [self dispatchProcessing:^{ - MXJSONModelSetMXJSONModel(urlPreview, MXURLPreview, JSONResponse); - } andCompletion:^{ - success(urlPreview); - }]; - } - } - failure:^(NSError *error) { - MXStrongifyAndReturnIfNil(self); - [self dispatchFailure:error inBlock:failure]; - }]; + MXHTTPOperation* operation; + operation = [httpClient requestWithMethod:@"GET" + path:[NSString stringWithFormat:@"%@/preview_url", authenticatedContentPathPrefix] + parameters:parameters + needsAuthentication:YES + success:^(NSDictionary *JSONResponse) { + MXStrongifyAndReturnIfNil(self); + + if (success) + { + __block MXURLPreview *urlPreview; + [self dispatchProcessing:^{ + MXJSONModelSetMXJSONModel(urlPreview, MXURLPreview, JSONResponse); + } andCompletion:^{ + success(urlPreview); + }]; + } + } + failure:^(NSError *error) { + return; + MXStrongifyAndReturnIfNil(self); + MXWeakify(self); + MXHTTPOperation* fallbackOperation = [self->httpClient requestWithMethod:@"GET" + path:[NSString stringWithFormat:@"%@/preview_url", self->contentPathPrefix] + parameters:parameters + success:^(NSDictionary *JSONResponse) { + MXStrongifyAndReturnIfNil(self); + + if (success) + { + __block MXURLPreview *urlPreview; + [self dispatchProcessing:^{ + MXJSONModelSetMXJSONModel(urlPreview, MXURLPreview, JSONResponse); + } andCompletion:^{ + success(urlPreview); + }]; + } + } + failure:^(NSError *error) { + MXStrongifyAndReturnIfNil(self); + [self dispatchFailure:error inBlock:failure]; + }]; + [operation mutateTo: fallbackOperation]; + }]; + return operation; } #pragma mark - Antivirus server API From ab2914275e48b888ae05331c6a4c0d9ebd3b3654 Mon Sep 17 00:00:00 2001 From: Mauro Romito Date: Wed, 3 Jul 2024 12:17:11 +0200 Subject: [PATCH 2/8] removing return used for testing --- MatrixSDK/MXRestClient.m | 1 - 1 file changed, 1 deletion(-) diff --git a/MatrixSDK/MXRestClient.m b/MatrixSDK/MXRestClient.m index e3820cce25..863fdf8077 100644 --- a/MatrixSDK/MXRestClient.m +++ b/MatrixSDK/MXRestClient.m @@ -4520,7 +4520,6 @@ - (MXHTTPOperation *)previewForURL:(NSURL *)url } } failure:^(NSError *error) { - return; MXStrongifyAndReturnIfNil(self); MXWeakify(self); MXHTTPOperation* fallbackOperation = [self->httpClient requestWithMethod:@"GET" From 15e214c95c2b9d97aae69b3adec5dfe23f61ecd0 Mon Sep 17 00:00:00 2001 From: Mauro Romito Date: Wed, 3 Jul 2024 13:10:26 +0200 Subject: [PATCH 3/8] using rest client instead of the home server directly --- MatrixSDK/MXSession.m | 2 +- MatrixSDK/Utils/Media/MXMediaManager.h | 9 +++++---- MatrixSDK/Utils/Media/MXMediaManager.m | 10 +++++----- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/MatrixSDK/MXSession.m b/MatrixSDK/MXSession.m index 7288253866..18550d042f 100644 --- a/MatrixSDK/MXSession.m +++ b/MatrixSDK/MXSession.m @@ -231,7 +231,7 @@ - (id)initWithMatrixRestClient:(MXRestClient*)mxRestClient { matrixRestClient = mxRestClient; _threePidAddManager = [[MX3PidAddManager alloc] initWithMatrixSession:self]; - mediaManager = [[MXMediaManager alloc] initWithHomeServer:matrixRestClient.homeserver]; + mediaManager = [[MXMediaManager alloc] initWithRestClient:matrixRestClient]; rooms = [NSMutableDictionary dictionary]; roomSummaries = [NSMutableDictionary dictionary]; _roomSummaryUpdateDelegate = [MXRoomSummaryUpdater roomSummaryUpdaterForSession:self]; diff --git a/MatrixSDK/Utils/Media/MXMediaManager.h b/MatrixSDK/Utils/Media/MXMediaManager.h index 325cbe6487..c7ba628dee 100644 --- a/MatrixSDK/Utils/Media/MXMediaManager.h +++ b/MatrixSDK/Utils/Media/MXMediaManager.h @@ -19,6 +19,7 @@ #import #import "MXMediaLoader.h" #import "MXEnumConstants.h" +#import "MXRestClient.h" #if TARGET_OS_IPHONE #import @@ -49,15 +50,15 @@ extern NSString *const kMXMediaManagerDefaultCacheFolder; Create an instance based on a homeserver url. This homeserver URL is required to resolve the Matrix Content URI (in the form of "mxc://..."). - @param homeserverURL the homeserver URL. + @param restClient the REST client. @return a MXMediaManager instance. */ -- (id)initWithHomeServer:(NSString *)homeserverURL; +- (id)initWithRestClient:(MXRestClient *)restClient; /** - The homeserver URL. + The rest client.. */ -@property (nonatomic, readonly) NSString *homeserverURL; +@property (nonatomic, readonly) MXRestClient *restClient; /** Antivirus scanner used to scan medias. diff --git a/MatrixSDK/Utils/Media/MXMediaManager.m b/MatrixSDK/Utils/Media/MXMediaManager.m index ef73ade893..12e593562f 100644 --- a/MatrixSDK/Utils/Media/MXMediaManager.m +++ b/MatrixSDK/Utils/Media/MXMediaManager.m @@ -49,12 +49,12 @@ @implementation MXMediaManager -- (id)initWithHomeServer:(NSString *)homeserverURL +- (id)initWithRestClient:(MXRestClient *)restClient { self = [super init]; if (self) { - _homeserverURL = homeserverURL; + _restClient = restClient; _scanManager = nil; } return self; @@ -376,7 +376,7 @@ - (NSString*)urlOfContent:(NSString*)mxContentURI } else { - mxMediaPrefix = [NSString stringWithFormat:@"%@/%@/download/", _homeserverURL, kMXContentPrefixPath]; + mxMediaPrefix = [NSString stringWithFormat:@"%@/%@/download/", _restClient.homeserver, kMXContentPrefixPath]; } contentURL = [mxContentURI stringByReplacingOccurrencesOfString:kMXContentUriScheme withString:mxMediaPrefix]; @@ -415,7 +415,7 @@ - (NSString*)urlOfContentThumbnail:(NSString*)mxContentURI } else { - mxThumbnailPrefix = [NSString stringWithFormat:@"%@/%@/thumbnail/", _homeserverURL, kMXContentPrefixPath]; + mxThumbnailPrefix = [NSString stringWithFormat:@"%@/%@/thumbnail/", _restClient.homeserver, kMXContentPrefixPath]; } NSString *thumbnailURL = [mxContentURI stringByReplacingOccurrencesOfString:kMXContentUriScheme withString:mxThumbnailPrefix]; @@ -447,7 +447,7 @@ - (NSString*)urlOfContentThumbnail:(NSString*)mxContentURI - (NSString *)urlOfIdenticon:(NSString *)identiconString { - return [NSString stringWithFormat:@"%@/%@/identicon/%@", _homeserverURL, kMXContentPrefixPath, [identiconString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]]; + return [NSString stringWithFormat:@"%@/%@/identicon/%@", _restClient.homeserver, kMXContentPrefixPath, [identiconString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]]; } From 4d22f5e5035f1cd9755b6c63cdb33e0188685edc Mon Sep 17 00:00:00 2001 From: Mauro Romito Date: Wed, 3 Jul 2024 13:25:35 +0200 Subject: [PATCH 4/8] added the access token to the media loader request --- MatrixSDK/Utils/Media/MXMediaLoader.h | 7 +++++++ MatrixSDK/Utils/Media/MXMediaLoader.m | 6 ++++-- MatrixSDK/Utils/Media/MXMediaManager.m | 6 +++++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/MatrixSDK/Utils/Media/MXMediaLoader.h b/MatrixSDK/Utils/Media/MXMediaLoader.h index 0144719859..b51fde8c18 100644 --- a/MatrixSDK/Utils/Media/MXMediaLoader.h +++ b/MatrixSDK/Utils/Media/MXMediaLoader.h @@ -129,6 +129,11 @@ extern NSString *const kMXMediaUploadIdPrefix; NSTimer* progressCheckTimer; } +/** + The access token. + */ +@property (nonatomic, readonly) NSString* accessToken; + /** The current state of the loader. */ @@ -177,6 +182,8 @@ extern NSString *const kMXMediaUploadIdPrefix; @property (readonly) CGFloat uploadInitialRange; @property (readonly) CGFloat uploadRange; +- (id)initWithAccessToken:(NSString *) accessToken; + /** Cancel the operation. */ diff --git a/MatrixSDK/Utils/Media/MXMediaLoader.m b/MatrixSDK/Utils/Media/MXMediaLoader.m index 9c0cabd8e7..0152d71038 100644 --- a/MatrixSDK/Utils/Media/MXMediaLoader.m +++ b/MatrixSDK/Utils/Media/MXMediaLoader.m @@ -41,11 +41,12 @@ @implementation MXMediaLoader @synthesize statisticsDict; -- (id)init +- (id)initWithAccessToken:(NSString *) accessToken { if (self = [super init]) { _state = MXMediaLoaderStateIdle; + _accessToken = accessToken; } return self; } @@ -144,6 +145,7 @@ - (void)downloadMediaFromURL:(NSString *)url // Use an HTTP POST method to send this data as JSON object. request.HTTPMethod = @"POST"; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; + [request setValue: [NSString stringWithFormat:@"Bearer %@", _accessToken] forHTTPHeaderField: @"Authorization"]; request.HTTPBody = [NSJSONSerialization dataWithJSONObject:data options:0 error:nil]; } @@ -378,7 +380,7 @@ - (id)initForUploadWithMatrixSession:(MXSession*)matrixSession initialRange:(CGF { // Create a unique upload Id _uploadId = [NSString stringWithFormat:@"%@%@", kMXMediaUploadIdPrefix, [[NSProcessInfo processInfo] globallyUniqueString]]; - + _accessToken = matrixSession.matrixRestClient.credentials.accessToken; mxSession = matrixSession; _uploadInitialRange = initialRange; _uploadRange = range; diff --git a/MatrixSDK/Utils/Media/MXMediaManager.m b/MatrixSDK/Utils/Media/MXMediaManager.m index 12e593562f..337ed7dfa6 100644 --- a/MatrixSDK/Utils/Media/MXMediaManager.m +++ b/MatrixSDK/Utils/Media/MXMediaManager.m @@ -495,6 +495,7 @@ - (MXMediaLoader*)downloadMediaFromMatrixContentURI:(NSString *)mxContentURI withData:nil andIdentifier:downloadId saveAtFilePath:filePath + accessToken: _restClient.credentials.accessToken scanManager:_scanManager success:success failure:failure]; @@ -535,6 +536,7 @@ - (MXMediaLoader*)downloadThumbnailFromMatrixContentURI:(NSString *)mxContentURI withData:nil andIdentifier:downloadId saveAtFilePath:filePath + accessToken: _restClient.credentials.accessToken scanManager:_scanManager success:success failure:failure]; @@ -545,6 +547,7 @@ + (MXMediaLoader*)downloadMedia:(NSString *)mediaURL withData:(NSDictionary *)data andIdentifier:(NSString *)downloadId saveAtFilePath:(NSString *)filePath + accessToken:(NSString *)accessToken scanManager:(MXScanManager *)scanManager success:(void (^)(NSString *outputFilePath))success failure:(void (^)(NSError *error))failure @@ -592,7 +595,7 @@ + (MXMediaLoader*)downloadMedia:(NSString *)mediaURL else { // Create a media loader to download data - mediaLoader = [[MXMediaLoader alloc] init]; + mediaLoader = [[MXMediaLoader alloc] initWithAccessToken:accessToken]; // Report this loader if (!downloadTable) { @@ -705,6 +708,7 @@ - (MXMediaLoader*)downloadEncryptedMediaFromMatrixContentFile:(MXEncryptedConten withData:dataToPost andIdentifier:downloadId saveAtFilePath:filePath + accessToken: _restClient.credentials.accessToken scanManager:_scanManager success:success failure:failure]; From 1c1760f8de52fa479f852c5e66cea975e50f39a0 Mon Sep 17 00:00:00 2001 From: Mauro Romito Date: Fri, 5 Jul 2024 15:58:37 +0200 Subject: [PATCH 5/8] implementation based on the server version --- MatrixSDK/JSONModels/MXMatrixVersions.h | 6 ++++ MatrixSDK/JSONModels/MXMatrixVersions.m | 9 ++++- MatrixSDK/MXRestClient.h | 4 +++ MatrixSDK/MXRestClient.m | 46 ++++++++----------------- MatrixSDK/Utils/Media/MXMediaManager.m | 8 +++++ 5 files changed, 41 insertions(+), 32 deletions(-) diff --git a/MatrixSDK/JSONModels/MXMatrixVersions.h b/MatrixSDK/JSONModels/MXMatrixVersions.h index b2a0bb1111..2e3d79ffed 100644 --- a/MatrixSDK/JSONModels/MXMatrixVersions.h +++ b/MatrixSDK/JSONModels/MXMatrixVersions.h @@ -35,6 +35,7 @@ struct MXMatrixClientServerAPIVersionStruct __unsafe_unretained NSString * const v1_1; __unsafe_unretained NSString * const v1_2; __unsafe_unretained NSString * const v1_3; + __unsafe_unretained NSString * const v1_11; }; extern const struct MXMatrixClientServerAPIVersionStruct MXMatrixClientServerAPIVersion; @@ -123,6 +124,11 @@ extern const struct MXMatrixVersionsFeatureStruct MXMatrixVersionsFeature; */ @property (nonatomic, readonly) BOOL supportsRedactionWithRelationsUnstable; +/** + Indicate if the server supports MSC3916 + */ +@property (nonatomic, readonly) BOOL supportsAuthenticatedMedia; + @end NS_ASSUME_NONNULL_END diff --git a/MatrixSDK/JSONModels/MXMatrixVersions.m b/MatrixSDK/JSONModels/MXMatrixVersions.m index 2dd5fb81f0..68f5eb996a 100644 --- a/MatrixSDK/JSONModels/MXMatrixVersions.m +++ b/MatrixSDK/JSONModels/MXMatrixVersions.m @@ -27,7 +27,9 @@ .r0_6_1 = @"r0.6.1", .v1_1 = @"v1.1", .v1_2 = @"v1.2", - .v1_3 = @"v1.3" + .v1_3 = @"v1.3", + // missing versions not considered + .v1_11 = @"v1.11" }; const struct MXMatrixVersionsFeatureStruct MXMatrixVersionsFeature = { @@ -145,6 +147,11 @@ - (BOOL)supportsRedactionWithRelationsUnstable return [self serverSupportsFeature:kJSONKeyMSC3912Unstable]; } +- (BOOL)supportsAuthenticatedMedia +{ + return [self serverSupportsVersion:MXMatrixClientServerAPIVersion.v1_11]; +} + #pragma mark - Private - (BOOL)serverSupportsVersion:(NSString *)version diff --git a/MatrixSDK/MXRestClient.h b/MatrixSDK/MXRestClient.h index d6b91d6726..8c28d094ef 100644 --- a/MatrixSDK/MXRestClient.h +++ b/MatrixSDK/MXRestClient.h @@ -251,6 +251,10 @@ extern NSString *const kMXCredentialsNewRefreshTokenDataKey; */ @property (nonatomic, copy) NSSet *acceptableContentTypes; +/** + Supported server versions of the matrix server, only for internal use of the SDK, use the stored version on the app side. + */ +@property (nonatomic, readonly, nullable) MXMatrixVersions *supportedVersions; /** Create an instance based on homeserver url. diff --git a/MatrixSDK/MXRestClient.m b/MatrixSDK/MXRestClient.m index 863fdf8077..184a86654c 100644 --- a/MatrixSDK/MXRestClient.m +++ b/MatrixSDK/MXRestClient.m @@ -142,10 +142,11 @@ @interface MXRestClient () */ dispatch_queue_t processingQueue; } +@property(nonatomic, nullable, readwrite) MXMatrixVersions *supportedVersions; @end @implementation MXRestClient -@synthesize credentials, apiPathPrefix, contentPathPrefix, authenticatedContentPathPrefix, completionQueue, antivirusServerPathPrefix; +@synthesize credentials, apiPathPrefix, contentPathPrefix, authenticatedContentPathPrefix, completionQueue, antivirusServerPathPrefix, supportedVersions; + (dispatch_queue_t)refreshQueue { @@ -530,6 +531,7 @@ - (MXHTTPOperation*)supportedMatrixVersions:(void (^)(MXMatrixVersions *matrixVe [self dispatchProcessing:^{ MXJSONModelSetMXJSONModel(matrixVersions, MXMatrixVersions, JSONResponse); } andCompletion:^{ + self.supportedVersions = matrixVersions; success(matrixVersions); }]; } @@ -4499,14 +4501,18 @@ - (MXHTTPOperation *)previewForURL:(NSURL *)url { NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; parameters[@"url"] = [url absoluteString]; + NSString* path = contentPathPrefix; + if (supportedVersions && [supportedVersions supportsAuthenticatedMedia]) + { + path = authenticatedContentPathPrefix; + } MXWeakify(self); - MXHTTPOperation* operation; - operation = [httpClient requestWithMethod:@"GET" - path:[NSString stringWithFormat:@"%@/preview_url", authenticatedContentPathPrefix] - parameters:parameters - needsAuthentication:YES - success:^(NSDictionary *JSONResponse) { + + return [httpClient requestWithMethod:@"GET" + path:[NSString stringWithFormat:@"%@/preview_url", path] + parameters:parameters + success:^(NSDictionary *JSONResponse) { MXStrongifyAndReturnIfNil(self); if (success) @@ -4519,32 +4525,10 @@ - (MXHTTPOperation *)previewForURL:(NSURL *)url }]; } } - failure:^(NSError *error) { + failure:^(NSError *error) { MXStrongifyAndReturnIfNil(self); - MXWeakify(self); - MXHTTPOperation* fallbackOperation = [self->httpClient requestWithMethod:@"GET" - path:[NSString stringWithFormat:@"%@/preview_url", self->contentPathPrefix] - parameters:parameters - success:^(NSDictionary *JSONResponse) { - MXStrongifyAndReturnIfNil(self); - - if (success) - { - __block MXURLPreview *urlPreview; - [self dispatchProcessing:^{ - MXJSONModelSetMXJSONModel(urlPreview, MXURLPreview, JSONResponse); - } andCompletion:^{ - success(urlPreview); - }]; - } - } - failure:^(NSError *error) { - MXStrongifyAndReturnIfNil(self); - [self dispatchFailure:error inBlock:failure]; - }]; - [operation mutateTo: fallbackOperation]; + [self dispatchFailure:error inBlock:failure]; }]; - return operation; } #pragma mark - Antivirus server API diff --git a/MatrixSDK/Utils/Media/MXMediaManager.m b/MatrixSDK/Utils/Media/MXMediaManager.m index 337ed7dfa6..0cc74081a8 100644 --- a/MatrixSDK/Utils/Media/MXMediaManager.m +++ b/MatrixSDK/Utils/Media/MXMediaManager.m @@ -377,6 +377,10 @@ - (NSString*)urlOfContent:(NSString*)mxContentURI else { mxMediaPrefix = [NSString stringWithFormat:@"%@/%@/download/", _restClient.homeserver, kMXContentPrefixPath]; + if (_restClient.supportedVersions && [_restClient.supportedVersions supportsAuthenticatedMedia]) + { + mxMediaPrefix = [NSString stringWithFormat:@"%@/%@/download/", _restClient.homeserver, kMXAuthenticatedContentPrefixPath]; + } } contentURL = [mxContentURI stringByReplacingOccurrencesOfString:kMXContentUriScheme withString:mxMediaPrefix]; @@ -416,6 +420,10 @@ - (NSString*)urlOfContentThumbnail:(NSString*)mxContentURI else { mxThumbnailPrefix = [NSString stringWithFormat:@"%@/%@/thumbnail/", _restClient.homeserver, kMXContentPrefixPath]; + if (_restClient.supportedVersions && [_restClient.supportedVersions supportsAuthenticatedMedia]) + { + mxThumbnailPrefix = [NSString stringWithFormat:@"%@/%@/thumbnail/", _restClient.homeserver, kMXAuthenticatedContentPrefixPath]; + } } NSString *thumbnailURL = [mxContentURI stringByReplacingOccurrencesOfString:kMXContentUriScheme withString:mxThumbnailPrefix]; From 10835525a6fcb30dc0475a36173a6285b8f59d76 Mon Sep 17 00:00:00 2001 From: Mauro Romito Date: Fri, 5 Jul 2024 16:03:59 +0200 Subject: [PATCH 6/8] access token fix and using the stable endpoint prefix --- MatrixSDK/MXEnumConstants.m | 2 +- MatrixSDK/Utils/Media/MXMediaLoader.m | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/MatrixSDK/MXEnumConstants.m b/MatrixSDK/MXEnumConstants.m index 5853507c4a..10f20274e2 100644 --- a/MatrixSDK/MXEnumConstants.m +++ b/MatrixSDK/MXEnumConstants.m @@ -23,7 +23,7 @@ */ NSString *const kMXContentUriScheme = @"mxc://"; NSString *const kMXContentPrefixPath = @"_matrix/media/r0"; -NSString *const kMXAuthenticatedContentPrefixPath = @"_matrix/client/unstable/org.matrix.msc3916/media"; +NSString *const kMXAuthenticatedContentPrefixPath = @"_matrix/client/v1/media"; /** Prefix used in path of antivirus server API requests. diff --git a/MatrixSDK/Utils/Media/MXMediaLoader.m b/MatrixSDK/Utils/Media/MXMediaLoader.m index 0152d71038..d65f10937b 100644 --- a/MatrixSDK/Utils/Media/MXMediaLoader.m +++ b/MatrixSDK/Utils/Media/MXMediaLoader.m @@ -140,12 +140,12 @@ - (void)downloadMediaFromURL:(NSString *)url [request setValue:value forHTTPHeaderField:key]; }]; + [request setValue: [NSString stringWithFormat:@"Bearer %@", _accessToken] forHTTPHeaderField: @"Authorization"]; if (data) { // Use an HTTP POST method to send this data as JSON object. request.HTTPMethod = @"POST"; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; - [request setValue: [NSString stringWithFormat:@"Bearer %@", _accessToken] forHTTPHeaderField: @"Authorization"]; request.HTTPBody = [NSJSONSerialization dataWithJSONObject:data options:0 error:nil]; } From 66f05d0468390bdddf838b73135353408fa5390c Mon Sep 17 00:00:00 2001 From: Mauro Romito Date: Fri, 5 Jul 2024 16:09:51 +0200 Subject: [PATCH 7/8] documentation --- MatrixSDK/Utils/Media/MXMediaManager.m | 1 + 1 file changed, 1 insertion(+) diff --git a/MatrixSDK/Utils/Media/MXMediaManager.m b/MatrixSDK/Utils/Media/MXMediaManager.m index 0cc74081a8..4aac77d3da 100644 --- a/MatrixSDK/Utils/Media/MXMediaManager.m +++ b/MatrixSDK/Utils/Media/MXMediaManager.m @@ -455,6 +455,7 @@ - (NSString*)urlOfContentThumbnail:(NSString*)mxContentURI - (NSString *)urlOfIdenticon:(NSString *)identiconString { + // Deprecated API, not need to use authenticated for this. return [NSString stringWithFormat:@"%@/%@/identicon/%@", _restClient.homeserver, kMXContentPrefixPath, [identiconString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]]; } From 389fdfad3c763fcc7a5080e5648cf5f7f6765b9a Mon Sep 17 00:00:00 2001 From: Mauro Romito Date: Fri, 5 Jul 2024 17:11:30 +0200 Subject: [PATCH 8/8] code improvement --- MatrixSDK/MXRestClient.h | 4 ++-- MatrixSDK/MXRestClient.m | 12 ++++-------- MatrixSDK/Utils/Media/MXMediaLoader.h | 5 ----- MatrixSDK/Utils/Media/MXMediaLoader.m | 7 +++++++ MatrixSDK/Utils/Media/MXMediaManager.m | 4 ++-- 5 files changed, 15 insertions(+), 17 deletions(-) diff --git a/MatrixSDK/MXRestClient.h b/MatrixSDK/MXRestClient.h index 8c28d094ef..7952d9b043 100644 --- a/MatrixSDK/MXRestClient.h +++ b/MatrixSDK/MXRestClient.h @@ -231,7 +231,7 @@ extern NSString *const kMXCredentialsNewRefreshTokenDataKey; /** The Matrix content repository prefix to use for authenticated access. - By default, it is defined by the constant kMXContentPrefixPath. + By default, it is defined by the constant kMXAuthenticatedContentPrefixPath. */ @property (nonatomic) NSString *authenticatedContentPathPrefix; @@ -254,7 +254,7 @@ extern NSString *const kMXCredentialsNewRefreshTokenDataKey; /** Supported server versions of the matrix server, only for internal use of the SDK, use the stored version on the app side. */ -@property (nonatomic, readonly, nullable) MXMatrixVersions *supportedVersions; +@property (readonly) BOOL isUsingAuthenticatedMedia; /** Create an instance based on homeserver url. diff --git a/MatrixSDK/MXRestClient.m b/MatrixSDK/MXRestClient.m index 184a86654c..2691d211f1 100644 --- a/MatrixSDK/MXRestClient.m +++ b/MatrixSDK/MXRestClient.m @@ -142,11 +142,11 @@ @interface MXRestClient () */ dispatch_queue_t processingQueue; } -@property(nonatomic, nullable, readwrite) MXMatrixVersions *supportedVersions; +@property(readwrite) BOOL isUsingAuthenticatedMedia; @end @implementation MXRestClient -@synthesize credentials, apiPathPrefix, contentPathPrefix, authenticatedContentPathPrefix, completionQueue, antivirusServerPathPrefix, supportedVersions; +@synthesize credentials, apiPathPrefix, contentPathPrefix, authenticatedContentPathPrefix, completionQueue, antivirusServerPathPrefix, isUsingAuthenticatedMedia; + (dispatch_queue_t)refreshQueue { @@ -531,7 +531,7 @@ - (MXHTTPOperation*)supportedMatrixVersions:(void (^)(MXMatrixVersions *matrixVe [self dispatchProcessing:^{ MXJSONModelSetMXJSONModel(matrixVersions, MXMatrixVersions, JSONResponse); } andCompletion:^{ - self.supportedVersions = matrixVersions; + self->isUsingAuthenticatedMedia = matrixVersions.supportsAuthenticatedMedia; success(matrixVersions); }]; } @@ -4501,11 +4501,7 @@ - (MXHTTPOperation *)previewForURL:(NSURL *)url { NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; parameters[@"url"] = [url absoluteString]; - NSString* path = contentPathPrefix; - if (supportedVersions && [supportedVersions supportsAuthenticatedMedia]) - { - path = authenticatedContentPathPrefix; - } + NSString* path = isUsingAuthenticatedMedia ? authenticatedContentPathPrefix : contentPathPrefix; MXWeakify(self); diff --git a/MatrixSDK/Utils/Media/MXMediaLoader.h b/MatrixSDK/Utils/Media/MXMediaLoader.h index b51fde8c18..2a254c587d 100644 --- a/MatrixSDK/Utils/Media/MXMediaLoader.h +++ b/MatrixSDK/Utils/Media/MXMediaLoader.h @@ -129,11 +129,6 @@ extern NSString *const kMXMediaUploadIdPrefix; NSTimer* progressCheckTimer; } -/** - The access token. - */ -@property (nonatomic, readonly) NSString* accessToken; - /** The current state of the loader. */ diff --git a/MatrixSDK/Utils/Media/MXMediaLoader.m b/MatrixSDK/Utils/Media/MXMediaLoader.m index d65f10937b..d10fbca68b 100644 --- a/MatrixSDK/Utils/Media/MXMediaLoader.m +++ b/MatrixSDK/Utils/Media/MXMediaLoader.m @@ -37,6 +37,13 @@ NSString *const kMXMediaUploadIdPrefix = @"upload-"; + +@interface MXMediaLoader() + +@property (nonatomic, readonly) NSString* accessToken; + +@end + @implementation MXMediaLoader @synthesize statisticsDict; diff --git a/MatrixSDK/Utils/Media/MXMediaManager.m b/MatrixSDK/Utils/Media/MXMediaManager.m index 4aac77d3da..2d40b97564 100644 --- a/MatrixSDK/Utils/Media/MXMediaManager.m +++ b/MatrixSDK/Utils/Media/MXMediaManager.m @@ -377,7 +377,7 @@ - (NSString*)urlOfContent:(NSString*)mxContentURI else { mxMediaPrefix = [NSString stringWithFormat:@"%@/%@/download/", _restClient.homeserver, kMXContentPrefixPath]; - if (_restClient.supportedVersions && [_restClient.supportedVersions supportsAuthenticatedMedia]) + if (_restClient.isUsingAuthenticatedMedia) { mxMediaPrefix = [NSString stringWithFormat:@"%@/%@/download/", _restClient.homeserver, kMXAuthenticatedContentPrefixPath]; } @@ -420,7 +420,7 @@ - (NSString*)urlOfContentThumbnail:(NSString*)mxContentURI else { mxThumbnailPrefix = [NSString stringWithFormat:@"%@/%@/thumbnail/", _restClient.homeserver, kMXContentPrefixPath]; - if (_restClient.supportedVersions && [_restClient.supportedVersions supportsAuthenticatedMedia]) + if (_restClient.isUsingAuthenticatedMedia) { mxThumbnailPrefix = [NSString stringWithFormat:@"%@/%@/thumbnail/", _restClient.homeserver, kMXAuthenticatedContentPrefixPath]; }