17
17
#import " PFAsyncTaskQueue.h"
18
18
#import " PFCommandResult.h"
19
19
#import " PFCoreManager.h"
20
+ #import " PFErrorUtilities.h"
20
21
#import " PFFileController.h"
21
22
#import " PFFileManager.h"
22
23
#import " PFFileStagingController.h"
@@ -73,12 +74,26 @@ + (instancetype)fileWithName:(NSString *)name contentsAtPath:(NSString *)path {
73
74
+ (instancetype )fileWithName : (NSString *)name contentsAtPath : (NSString *)path error : (NSError **)error {
74
75
NSFileManager *fileManager = [NSFileManager defaultManager ];
75
76
BOOL directory = NO ;
76
- PFParameterAssert ([fileManager fileExistsAtPath: path isDirectory: &directory] && !directory,
77
- @" %@ is not a valid file path for a PFFile." , path);
78
77
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: NSPOSIXErrorDomain
82
+ code: ENOENT
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: NSPOSIXErrorDomain
93
+ code: EFBIG
94
+ userInfo: @{ NSLocalizedDescriptionKey : message }];
95
+ return nil ;
96
+ }
82
97
83
98
PFFile *file = [self fileWithName: name url: nil ];
84
99
if (![file _stageWithPath: path error: error]) {
@@ -100,8 +115,22 @@ + (instancetype)fileWithName:(NSString *)name
100
115
data : (NSData *)data
101
116
contentType : (NSString *)contentType
102
117
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: NSPOSIXErrorDomain
121
+ code: EINVAL
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: NSPOSIXErrorDomain
130
+ code: EFBIG
131
+ userInfo: @{ NSLocalizedDescriptionKey : message }];
132
+ return nil ;
133
+ }
105
134
106
135
PFFile *file = [[self alloc ] initWithName: name urlString: nil mimeType: contentType];
107
136
if (![file _stageWithData: data error: error]) {
0 commit comments