Skip to content

Commit

Permalink
Add changes made in wpios
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Jul 13, 2024
1 parent 25ccccd commit 01fe272
Showing 1 changed file with 46 additions and 12 deletions.
58 changes: 46 additions & 12 deletions Sources/WordPressKit/Services/MediaServiceRemoteXMLRPC.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ - (void)getMediaLibraryStartOffset:(NSUInteger)offset
pageLoad(pageMedia);
}
NSUInteger newOffset = offset + pageSize;
[self getMediaLibraryStartOffset:newOffset media:resultMedia pageLoad:pageLoad success: success failure: failure];
[self getMediaLibraryStartOffset:newOffset media:resultMedia pageLoad:pageLoad success: success failure: failure];
}
failure:^(NSError *error, NSHTTPURLResponse *httpResponse) {
if (failure) {
Expand Down Expand Up @@ -104,7 +104,7 @@ - (NSURLCredential *)findCredentialForHost:(NSString *)host port:(NSInteger)port
[[[NSURLCredentialStorage sharedCredentialStorage] allCredentials] enumerateKeysAndObjectsUsingBlock:^(NSURLProtectionSpace *ps, NSDictionary *dict, BOOL *stop) {
[dict enumerateKeysAndObjectsUsingBlock:^(id key, NSURLCredential *credential, BOOL *stop) {
if ([[ps host] isEqualToString:host] && [ps port] == port)

{
foundCredential = credential;
*stop = YES;
Expand All @@ -117,9 +117,9 @@ - (NSURLCredential *)findCredentialForHost:(NSString *)host port:(NSInteger)port
return foundCredential;
}

/**
/**
Adds a basic auth header to a request if a credential is stored for that specific host.
The credentials will only be added if a set of credentials for the request host are stored on the shared credential storage
@param request The request to where the authentication information will be added.
*/
Expand Down Expand Up @@ -174,21 +174,21 @@ - (void)uploadMedia:(RemoteMedia *)media
NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorBadServerResponse userInfo:@{NSLocalizedDescriptionKey: NSLocalizedString(@"The server returned an empty response. This usually means you need to increase the memory limit for your site.", @"")}];
if (failure) {
failure(error);
}
}
} else {
localProgress.completedUnitCount=localProgress.totalUnitCount;
RemoteMedia * remoteMedia = [self remoteMediaFromXMLRPCDictionary:response];
if (success){
success(remoteMedia);
}
}
} failure:^(NSError *error, NSHTTPURLResponse *httpResponse) {
} failure:^(NSError *error, NSHTTPURLResponse *httpResponse) {
if (failure) {
failure(error);
}
}];


if (progress) {
*progress = localProgress;
}
Expand All @@ -198,10 +198,44 @@ - (void)updateMedia:(RemoteMedia *)media
success:(void (^)(RemoteMedia *remoteMedia))success
failure:(void (^)(NSError *error))failure
{
//HACK: Sergio Estevao: 2016-04-06 this option doens't exist on XML-RPC so we will always say that all was good
if (success) {
success(media);
NSParameterAssert([media.mediaID longLongValue] > 0);

NSMutableDictionary *content = [NSMutableDictionary dictionary];

if (media.title != nil) {
content[@"post_title"] = media.title;
}

if (media.caption != nil) {
content[@"post_excerpt"] = media.caption;
}

if (media.descriptionText != nil) {
content[@"post_content"] = media.descriptionText;
}

NSArray *extraDefaults = @[media.mediaID];
NSArray *parameters = [self XMLRPCArgumentsWithExtraDefaults:extraDefaults andExtra:content];

[self.api callMethod:@"wp.editPost"
parameters:parameters
success:^(id responseObject, NSHTTPURLResponse *httpResponse) {
BOOL updated = [responseObject boolValue];
if (updated) {
if (success) {
success(media);
}
} else {
if (failure) {
NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorUnknown userInfo:nil];
failure(error);
}
}
} failure:^(NSError *error, NSHTTPURLResponse *httpResponse) {
if (failure) {
failure(error);
}
}];
}

- (void)deleteMedia:(RemoteMedia *)media
Expand Down Expand Up @@ -285,11 +319,11 @@ - (RemoteMedia *)remoteMediaFromXMLRPCDictionary:(NSDictionary*)xmlRPC
link = [xmlRPC stringForKeyPath:@"link"];
}
remoteMedia.file = [link lastPathComponent] ?: [[xmlRPC objectForKeyPath:@"file"] lastPathComponent];

if ([xmlRPC stringForKeyPath:@"metadata.sizes.large.file"] != nil) {
remoteMedia.largeURL = [NSURL URLWithString: [NSString stringWithFormat:@"%@%@", remoteMedia.url.URLByDeletingLastPathComponent, [xmlRPC stringForKeyPath:@"metadata.sizes.large.file"]]];
}

if ([xmlRPC stringForKeyPath:@"metadata.sizes.medium.file"] != nil) {
remoteMedia.mediumURL = [NSURL URLWithString: [NSString stringWithFormat:@"%@%@", remoteMedia.url.URLByDeletingLastPathComponent, [xmlRPC stringForKeyPath:@"metadata.sizes.medium.file"]]];
}
Expand Down

0 comments on commit 01fe272

Please sign in to comment.