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

Blobstore image upload fetch #35

Merged
merged 17 commits into from
Jun 16, 2020
Merged

Conversation

gwynethsc
Copy link
Owner

Enables site visitors to upload up to 1 image with each comment, which is displayed as a thumbnail

Copy link
Collaborator

@Google-JM Google-JM left a comment

Choose a reason for hiding this comment

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

Great job and nice comments where you think the reader of this code may not understand what is happening!

@@ -19,7 +19,7 @@
*
* @param loggedIn true if a user is logged in, else false
* @param userId a unique String identifying the user, if logged in; else null
* @param userEmail
* @param userEmail
* @param redirectURL the URL to be redirected to after logging in or out
*/
public LoginStatus(boolean loggedIn, String userId, String userEmail, String redirectURL) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I missed this is previous reviews, but something to think about if you have time to come back to this code:

Create a User object to hold userId, userEmail. That way you can do LoginStatus(User user, boolean loggedIn, String redirectUrl)

Loose rule of thumb if you are passing 2 or more params that are similar, think about creating an object to hold the values.


@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does BlobstoreService need to be initialized on every Get? Can this be a global constant variable?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Done

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
String uploadUrl = blobstoreService.createUploadUrl("/data");
Copy link
Collaborator

Choose a reason for hiding this comment

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

If you don't plan to use uploadUrl in any other way, you can combine it with line 34.

response.getWriter().println(blobstoreService.createUploadUrl("/data"));

Copy link
Owner Author

Choose a reason for hiding this comment

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

Done


// User submitted form without selecting a file, so we can't get a URL. (dev server)
if (blobKeys == null || blobKeys.isEmpty()) {
return null;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Given this method can return null, you should think about using @Nullable annotation on the method. This is a good way of getting compiler errors on incompatibile Nullability and help prevent issues at compile time instead of runtime.

// https://stackoverflow.com/q/10779564/873165

// Use ImagesService to get a URL that points to the uploaded file.
ImagesService imagesService = ImagesServiceFactory.getImagesService();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Similar comment to above, can this be made into a global constant?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Done


@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
BlobKey blobKey = new BlobKey(request.getParameter("blob-key"));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Make "blob-key" into a constant. This is more preemptive since it is only be used once currently.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Done

fetch(SERVLET_BLOBSTORE).then(response => response.text()).then(result => {
commentForm.action = result;
})
.catch(error => console.error(error.message));
Copy link
Collaborator

Choose a reason for hiding this comment

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

In the future think about how you would display this error to the user, if it is human readable.

@gwynethsc gwynethsc merged commit 62a3e31 into master Jun 16, 2020
@gwynethsc gwynethsc deleted the blobstore-image-upload-fetch branch June 16, 2020 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants