Skip to content
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

Add error for attempt to upload directory #5750

Merged
merged 10 commits into from
Jun 10, 2020

Conversation

ncooke3
Copy link
Member

@ncooke3 ncooke3 commented Jun 4, 2020

In issue #5720, putFile was used to upload a .numbers document. Since .numbers is actually a directory and not a file, the method resulted in an error. The error is a bit misleading as it appears to come from the URLSession upload task.

In an attempt to provide a better error for such cases, I added a check that validates that the fileURL passed into putFile is indeed a file and not a directory.

Since this is just a draft PR, I figured I could? include two documents (Home Improvement.numbers and star_wars.pdf) that support some initial tests I wrote for the addition I made to FIRStorageReference.m. If we decide to move forward with this, I would definitely take those out before opening a real PR

I appreciate any feedback!

@@ -215,6 +215,13 @@ - (FIRStorageUploadTask *)putFile:(NSURL *)fileURL
- (FIRStorageUploadTask *)putFile:(NSURL *)fileURL
metadata:(nullable FIRStorageMetadata *)metadata
completion:(nullable FIRStorageVoidMetadataError)completion {
if ([fileURL frs_hasDirectoryPath]) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check that it is a file versus not a directory? Like https://github.com/firebase/firebase-ios-sdk/pull/5742/files?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will play with this. Maybe instead of the check looking like this:

 if fileUrl is a directory -> return error

We could instead have something like?:

 if !(fileUrl is a file) -> return error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I think the 2nd option is preferable because there may be other types of URLs that are not handled now, e.g. a symlink or not a file system URL.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM

Copy link
Contributor

@schmidt-sebastian schmidt-sebastian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty great! I think you can probably remove star_wars.pdf and the contents of Home Improvement.numbers, but keep the folder itself. The folder by itself should hopefully be enough to let you keep the new test you added.

Copy link
Contributor

@schmidt-sebastian schmidt-sebastian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically LGTM. Please delete the extra files (the *.jpg files) and add a Changelog entry here: https://github.com/firebase/firebase-ios-sdk/blob/master/FirebaseStorage/CHANGELOG.md

This will likely be released with version 3.6.2, but you can also create a new "Unreleased" section.

@ncooke3 ncooke3 marked this pull request as ready for review June 8, 2020 17:08
@ncooke3 ncooke3 changed the title Draft: Add error for attempt to upload directory Jun 8, 2020
@ncooke3
Copy link
Member Author

ncooke3 commented Jun 8, 2020

Looks like I broke some of the objc unit tests by making the change to check that the passed in fileURL is a file rather than a directory. The good news is the ones that failed were testReferenceWithNilFileURLFailsWithCompletion and testReferenceWithNonexistentFileURLFailsWithCompletion which were caught by the new isFile check. Should I fix the objc tests so that the assertions match?

@paulb777
Copy link
Member

paulb777 commented Jun 8, 2020

Updating the tests SGTM since its improving the tests with a more accurate error message, right?

@ncooke3
Copy link
Member Author

ncooke3 commented Jun 8, 2020

Updating the tests SGTM since its improving the tests with a more accurate error message, right?

Yup, I believe so. Up until now, it looks like errors related to an invalid fileURL input occur sometime during the upload process. With this check, if we start the upload process, then we should be able to assume that the fileURL is valid and any other errors are related to the actual upload process.

@paulb777
Copy link
Member

paulb777 commented Jun 9, 2020

On second thought, I'm not completely certain, if the error message is considered part of the API

@paulb777 paulb777 closed this Jun 9, 2020
@paulb777 paulb777 reopened this Jun 9, 2020
Comment on lines +196 to +197
if (![_fileURL checkResourceIsReachableAndReturnError:&fileReachabilityError] ||
![self fileURLisFile:_fileURL]) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The checkResourceIsReachableAndReturnError returns true or false depending on if the _fileURL can be reached. This is why attempts to upload directories weren't being caught in the first place, because they are reachable. Instead of doing a check in putFile:, I figured I should move the fileURLisFile helper method to FIRStorageUploadTask.m and call it in this isContentToUploadValid method so we have a single check to verify that the file we are trying to upload is both a reachable resource and it is a file.

Comment on lines +200 to +203
userInfo[NSLocalizedDescriptionKey] = [NSString
stringWithFormat:@"File at URL: %@ is not reachable. "
@"Ensure file URL is not a directory, symbolic link, or invalid url.",
_fileURL.absoluteString];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some extra context to the error since we are now explicitly checking if the url points to a file.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@paulb777 paulb777 added this to the 6.27.0 - M73 milestone Jun 10, 2020
@paulb777 paulb777 merged commit 6d08f10 into master Jun 10, 2020
@paulb777 paulb777 deleted the nc-storage-directory-upload-attempt-error branch June 10, 2020 15:19
@firebase firebase locked and limited conversation to collaborators Jul 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.