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 write-only image_source field for asset create/edit API endpoint #6146

Merged
merged 1 commit into from
Mar 14, 2019
Merged

Add write-only image_source field for asset create/edit API endpoint #6146

merged 1 commit into from
Mar 14, 2019

Conversation

mskrip
Copy link
Contributor

@mskrip mskrip commented Aug 30, 2018

Tested with Content-Type: application/x-www-form-urlencoded and file
attached as base64 encoded string after mime type in field image_source.

@mskrip mskrip requested a review from snipe as a code owner August 30, 2018 21:37
*/
public function image(Request $request, $asset_id)
{
$this->authorize('image', Asset::class);
Copy link
Owner

Choose a reason for hiding this comment

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

We don't have a permission mask for image, do we?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed

@snipe
Copy link
Owner

snipe commented Aug 30, 2018

Wouldn't this make more sense in the edit method? Do we need a separate endpoint for image uploads?

Also, if we were going to do that, it should be files in general, not just images. Image is only relevant in the edit/create process, everything else is a file upload, which could be images or other types of files.

@mskrip mskrip changed the title Add API endpoint for uploading image for asset Add write-only image_source field for asset create/edit API endpoint Sep 3, 2018
@mskrip
Copy link
Contributor Author

mskrip commented Sep 3, 2018

Moved it to edit/create method as you suggested. But didn't want to create inconsistencies so I made write-only field image_source for that.

@@ -506,6 +520,13 @@ public function update(Request $request, $id)
($request->filled('company_id')) ?
$asset->company_id = Company::getIdForCurrentUser($request->get('company_id')) : '';

if ($request->has('image_source')) {
try {
Copy link
Owner

Choose a reason for hiding this comment

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

Hi there - what's the reason for a try/catch here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi, added a comment on that. Basically the called function can throw an error if source is not in supported format or somehow malformed.

Copy link
Owner

Choose a reason for hiding this comment

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

Hi - What's the error it can throw? It looks like you're just setting the $asset->image value, no?

$asset->image = $this->image($request->input('image_source'));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

$this->image() can throw the exception

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, sorry... I had no idea it works like that. As you might imagine I'm new to php. Removed both try/catches.

* @return string path to uploaded image
* @throws \Exception if image source is not in correct format
*/
private function image(String $image_data)
Copy link
Owner

Choose a reason for hiding this comment

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

Sorry for the delay in finishing this review. We should also probably rename this method, since it actually performs an action, and return false if something goes wrong. Something like processUploadedImage, or something that's a bit more specific as to what it's doing. In this massive codebase, I'm not sure I'll ever remember that ->image() actually processes things, and could be easily confused with the image property of many of our models.

Copy link
Owner

Choose a reason for hiding this comment

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

Also should this maybe be a helper, trait or model method? Lots of our models have images. If there's a way to break it out so that it could be re-used, that would probably be more useful.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi, I've moved the method to helpers and renamed it according to your suggestion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The method now returns false if something goes wrong. So it's much more general while the endpoint still retains the possibility to wipe the image from hardware if image_source field is set but empty.

@mskrip
Copy link
Contributor Author

mskrip commented Oct 12, 2018

Hi @snipe, should I do something with the failing check? Or is it out of my control completely?

@mskrip
Copy link
Contributor Author

mskrip commented Nov 9, 2018

ping

@mskrip
Copy link
Contributor Author

mskrip commented Feb 8, 2019

@snipe Ping again, any update on this? It would be very useful

`image_source` should contain base64 encoded image data with mime-type.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants