-
Notifications
You must be signed in to change notification settings - Fork 497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Check filesize before uploading file to server and display alert if file is too big #7778
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can't make changes to the translations directly in the project - these should be added on https://translate.element.io once the PR is merged, otherwise we run the risk of handling conflicts. |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -46,11 +46,12 @@ | |||||
|
||||||
NSString * const MXKRoomDataSourceErrorDomain = @"kMXKRoomDataSourceErrorDomain"; | ||||||
|
||||||
typedef NS_ENUM (NSUInteger, MXKRoomDataSourceError) { | ||||||
MXKRoomDataSourceErrorResendGeneric = 10001, | ||||||
MXKRoomDataSourceErrorResendInvalidMessageType = 10002, | ||||||
MXKRoomDataSourceErrorResendInvalidLocalFilePath = 10003, | ||||||
}; | ||||||
// Check filesize before sending: make RoomDataSource errors public | ||||||
//typedef NS_ENUM (NSUInteger, MXKRoomDataSourceError) { | ||||||
// MXKRoomDataSourceErrorResendGeneric = 10001, | ||||||
// MXKRoomDataSourceErrorResendInvalidMessageType = 10002, | ||||||
// MXKRoomDataSourceErrorResendInvalidLocalFilePath = 10003, | ||||||
//}; | ||||||
|
||||||
|
||||||
@interface MXKRoomDataSource () | ||||||
|
@@ -1923,6 +1924,67 @@ - (BOOL)canReplyToEventWithId:(NSString*)eventIdToReply | |||||
return [self.room canReplyToEvent:eventToReply]; | ||||||
} | ||||||
|
||||||
// Check filesize before sending: check file size before sending | ||||||
- (BOOL)_isFilesizeOkToBeSent:(NSUInteger)filesize | ||||||
{ | ||||||
// Check maxUploadSize accepted by the home server before trying to upload. | ||||||
NSUInteger maxUploadFileSize = self.mxSession.maxUploadSize; | ||||||
if (filesize > maxUploadFileSize) | ||||||
{ | ||||||
return NO; | ||||||
} | ||||||
Comment on lines
+1932
to
+1935
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same question again here:
|
||||||
else | ||||||
{ | ||||||
return YES; | ||||||
} | ||||||
} | ||||||
|
||||||
- (BOOL)isFilesizeOkToBeSentForData:(NSData *)fileData | ||||||
{ | ||||||
return [self _isFilesizeOkToBeSent:fileData.length]; | ||||||
} | ||||||
|
||||||
- (BOOL)isFilesizeOkToBeSentForLocalFileUrl:(NSURL *)localFileUrl | ||||||
{ | ||||||
NSDictionary *fileAttributes = [NSFileManager.defaultManager attributesOfItemAtPath:localFileUrl.path error:nil]; | ||||||
if (fileAttributes) | ||||||
{ | ||||||
return [self _isFilesizeOkToBeSent:fileAttributes.fileSize]; | ||||||
} | ||||||
else | ||||||
{ | ||||||
return NO; | ||||||
} | ||||||
} | ||||||
|
||||||
- (BOOL)isFilesizeOkToBeSentForLocalAVAsset:(AVAsset *)asset | ||||||
{ | ||||||
// Check if asset points to a local file | ||||||
if( ![asset isKindOfClass:AVURLAsset.class] ) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Our objective-c style should look like so:
Suggested change
There's a few of these in here. |
||||||
{ | ||||||
// If asset doesn't point to a local asset, we can't check size. | ||||||
// Return YES to let the upload happens and get the result of the backend. | ||||||
return YES; | ||||||
} | ||||||
|
||||||
AVURLAsset *urlAsset = (AVURLAsset *)asset; | ||||||
NSNumber *assetFilesize; | ||||||
NSError *error; | ||||||
|
||||||
// Try to get asset filesize. | ||||||
[urlAsset.URL getResourceValue:&assetFilesize forKey:NSURLFileSizeKey error:&error]; | ||||||
|
||||||
// If we can't check size, | ||||||
if( error != NULL || assetFilesize == NULL ) | ||||||
{ | ||||||
// return YES to let the upload happens and get the result of the backend. | ||||||
return YES; | ||||||
} | ||||||
|
||||||
return [self _isFilesizeOkToBeSent:assetFilesize.unsignedLongValue]; | ||||||
} | ||||||
|
||||||
|
||||||
- (void)sendImage:(NSData *)imageData mimeType:(NSString *)mimetype success:(void (^)(NSString *))success failure:(void (^)(NSError *))failure | ||||||
{ | ||||||
UIImage *image = [UIImage imageWithData:imageData]; | ||||||
|
@@ -1944,6 +2006,13 @@ - (void)sendImage:(NSData *)imageData mimeType:(NSString *)mimetype success:(voi | |||||
|
||||||
- (void)sendImageData:(NSData*)imageData withImageSize:(CGSize)imageSize mimeType:(NSString*)mimetype andThumbnail:(UIImage*)thumbnail success:(void (^)(NSString *eventId))success failure:(void (^)(NSError *error))failure | ||||||
{ | ||||||
// Check filesize before sending: check fielsize before trying to send file | ||||||
if( ![self isFilesizeOkToBeSentForData:imageData] ) | ||||||
{ | ||||||
failure([NSError errorWithDomain:MXKRoomDataSourceErrorDomain code:MXKRoomDataSourceErrorCantSendFileToBig userInfo:nil]); | ||||||
return; | ||||||
} | ||||||
|
||||||
__block MXEvent *localEchoEvent = nil; | ||||||
|
||||||
[_room sendImage:imageData withImageSize:imageSize mimeType:mimetype andThumbnail:thumbnail threadId:self.threadId localEcho:&localEchoEvent success:success failure:failure]; | ||||||
|
@@ -1964,6 +2033,13 @@ - (void)sendVideo:(NSURL *)videoLocalURL withThumbnail:(UIImage *)videoThumbnail | |||||
|
||||||
- (void)sendVideoAsset:(AVAsset *)videoAsset withThumbnail:(UIImage *)videoThumbnail success:(void (^)(NSString *))success failure:(void (^)(NSError *))failure | ||||||
{ | ||||||
// Check filesize before sending: check fielsize before trying to send file | ||||||
if( ![self isFilesizeOkToBeSentForLocalAVAsset:videoAsset] ) | ||||||
{ | ||||||
failure([NSError errorWithDomain:MXKRoomDataSourceErrorDomain code:MXKRoomDataSourceErrorCantSendFileToBig userInfo:nil]); | ||||||
return; | ||||||
} | ||||||
|
||||||
__block MXEvent *localEchoEvent = nil; | ||||||
|
||||||
[_room sendVideoAsset:videoAsset withThumbnail:videoThumbnail threadId:self.threadId localEcho:&localEchoEvent success:success failure:failure]; | ||||||
Comment on lines
+2037
to
2045
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @NicolasBuquet, thanks for the update. To my eyes this one still appears to be too early: |
||||||
|
@@ -2013,6 +2089,13 @@ - (void)sendVoiceMessage:(NSURL *)audioFileLocalURL | |||||
|
||||||
- (void)sendFile:(NSURL *)fileLocalURL mimeType:(NSString*)mimeType success:(void (^)(NSString *))success failure:(void (^)(NSError *))failure | ||||||
{ | ||||||
// Check filesize before sending: check fielsize before trying to send file | ||||||
if( ![self isFilesizeOkToBeSentForLocalFileUrl:fileLocalURL] ) | ||||||
{ | ||||||
failure([NSError errorWithDomain:MXKRoomDataSourceErrorDomain code:MXKRoomDataSourceErrorCantSendFileToBig userInfo:nil]); | ||||||
return; | ||||||
} | ||||||
|
||||||
__block MXEvent *localEchoEvent = nil; | ||||||
|
||||||
[_room sendFile:fileLocalURL mimeType:mimeType threadId:self.threadId localEcho:&localEchoEvent success:success failure:failure]; | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Check filesize before uploading file to server and display alert if file is too big. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you checked what this looks like? Now that there's an
NSByteCountFormatter
feeding the alert, it should already contain the appropriate unit formatted appropriately in the string.