AWSS3TransferUtility+Validation.m 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //
  2. // Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License").
  5. // You may not use this file except in compliance with the License.
  6. // A copy of the License is located at
  7. //
  8. // http://aws.amazon.com/apache2.0
  9. //
  10. // or in the "license" file accompanying this file. This file is distributed
  11. // on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. // express or implied. See the License for the specific language governing
  13. // permissions and limitations under the License.
  14. //
  15. #import <AWSS3/AWSS3.h>
  16. @implementation AWSS3TransferUtility (Validation)
  17. - (AWSTask *) validateParameters: (NSString * )bucket key:(NSString *)key fileURL:(NSURL *)fileURL accelerationModeEnabled: (BOOL) accelerationModeEnabled
  18. {
  19. AWSTask *validationError = [self validateParameters:bucket key:key accelerationModeEnabled:accelerationModeEnabled];
  20. if (validationError) {
  21. return validationError;
  22. }
  23. NSString *filePath = [fileURL path];
  24. // Error out if the length of file name < minimum file path length (2 characters) or file does not exist
  25. if ([filePath length] < 2 ||
  26. ! [[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
  27. return [AWSTask taskWithError:[NSError errorWithDomain:AWSS3TransferUtilityErrorDomain
  28. code:AWSS3TransferUtilityErrorLocalFileNotFound
  29. userInfo:nil]];
  30. }
  31. return nil;
  32. }
  33. - (AWSTask *) validateParameters: (NSString * )bucket key:(NSString *)key accelerationModeEnabled: (BOOL) accelerationModeEnabled
  34. {
  35. //Validate input parameter: bucket
  36. if (!bucket || [bucket length] == 0) {
  37. NSInteger errorCode = (accelerationModeEnabled) ?
  38. AWSS3PresignedURLErrorInvalidBucketNameForAccelerateModeEnabled : AWSS3PresignedURLErrorInvalidBucketName;
  39. NSString *errorMessage = @"Invalid bucket specified. Please specify a bucket name or configure the bucket property in `AWSS3TransferUtilityConfiguration`.";
  40. NSDictionary *userInfo = [NSDictionary dictionaryWithObject:errorMessage
  41. forKey:NSLocalizedDescriptionKey];
  42. return [AWSTask taskWithError:[NSError errorWithDomain:AWSS3PresignedURLErrorDomain
  43. code:errorCode
  44. userInfo:userInfo]];
  45. }
  46. if (!key || [key length] == 0) {
  47. NSString *errorMessage = [NSString stringWithFormat: @"Empty key specified"];
  48. NSDictionary *userInfo = [NSDictionary dictionaryWithObject:errorMessage
  49. forKey:NSLocalizedDescriptionKey];
  50. return [AWSTask taskWithError:[NSError errorWithDomain:AWSS3TransferUtilityErrorDomain
  51. code:AWSS3TransferUtilityErrorClientError
  52. userInfo:userInfo]];
  53. }
  54. return nil;
  55. }
  56. @end