1717#import " PFAsyncTaskQueue.h"
1818#import " PFCommandResult.h"
1919#import " PFCoreManager.h"
20+ #import " PFErrorUtilities.h"
2021#import " PFFileController.h"
2122#import " PFFileManager.h"
2223#import " PFFileStagingController.h"
@@ -73,12 +74,26 @@ + (instancetype)fileWithName:(NSString *)name contentsAtPath:(NSString *)path {
7374+ (instancetype )fileWithName : (NSString *)name contentsAtPath : (NSString *)path error : (NSError **)error {
7475 NSFileManager *fileManager = [NSFileManager defaultManager ];
7576 BOOL directory = NO ;
76- PFParameterAssert ([fileManager fileExistsAtPath: path isDirectory: &directory] && !directory,
77- @" %@ is not a valid file path for a PFFile." , path);
7877
79- NSDictionary *attributess = [fileManager attributesOfItemAtPath: path error: nil ];
80- unsigned long long length = [attributess[NSFileSize ] unsignedLongValue ];
81- PFParameterAssert (length <= PFFileMaxFileSize, @" PFFile cannot be larger than %lli bytes" , PFFileMaxFileSize);
78+ if (![fileManager fileExistsAtPath: path isDirectory: &directory] || directory) {
79+ NSString *message = [NSString stringWithFormat: @" Failed to create PFFile at path '%@ ': "
80+ " file does not exist." , path];
81+ *error = [NSError errorWithDomain: NSCocoaErrorDomain
82+ code: NSFileNoSuchFileError
83+ userInfo: @{ NSLocalizedDescriptionKey : message }];
84+ return nil ;
85+ }
86+
87+ NSDictionary *attributes = [fileManager attributesOfItemAtPath: path error: nil ];
88+ unsigned long long length = [attributes[NSFileSize ] unsignedLongValue ];
89+ if (length > PFFileMaxFileSize) {
90+ NSString *message = [NSString stringWithFormat: @" Failed to create PFFile at path '%@ ': "
91+ " file is larger than %llu MB." , path, (PFFileMaxFileSize >> 20 )];
92+ *error = [NSError errorWithDomain: NSCocoaErrorDomain
93+ code: NSFileReadTooLargeError
94+ userInfo: @{ NSLocalizedDescriptionKey : message }];
95+ return nil ;
96+ }
8297
8398 PFFile *file = [self fileWithName: name url: nil ];
8499 if (![file _stageWithPath: path error: error]) {
@@ -100,8 +115,22 @@ + (instancetype)fileWithName:(NSString *)name
100115 data : (NSData *)data
101116 contentType : (NSString *)contentType
102117 error : (NSError **)error {
103- PFParameterAssert ([data length ] <= PFFileMaxFileSize,
104- @" PFFile cannot be larger than %llu bytes" , PFFileMaxFileSize);
118+ if (!data) {
119+ NSString *message = @" Cannot create a PFFile with nil data." ;
120+ *error = [NSError errorWithDomain: NSCocoaErrorDomain
121+ code: NSFileNoSuchFileError
122+ userInfo: @{ NSLocalizedDescriptionKey : message }];
123+ return nil ;
124+ }
125+
126+ if ([data length ] > PFFileMaxFileSize) {
127+ NSString *message = [NSString stringWithFormat: @" Failed to create PFFile with data: "
128+ " data is larger than %llu MB." , (PFFileMaxFileSize >> 20 )];
129+ *error = [NSError errorWithDomain: NSCocoaErrorDomain
130+ code: NSFileReadTooLargeError
131+ userInfo: @{ NSLocalizedDescriptionKey : message }];
132+ return nil ;
133+ }
105134
106135 PFFile *file = [[self alloc ] initWithName: name urlString: nil mimeType: contentType];
107136 if (![file _stageWithData: data error: error]) {
0 commit comments