• Welcome to the Chevereto user community!

    Here users from all over the world gather around to learn the latest about Chevereto and contribute with ideas to improve the software.

    Please keep in mind:

    • 😌 This community is user driven. Be polite with other users.
    • 👉 Is required to purchase a Chevereto license to participate in this community (doesn't apply to Pre-sales).
    • 💸 Purchase a Pro Subscription to get access to active software support and faster ticket response times.

Uploading from iOS | API fails on base64 string

Status
Not open for further replies.

hagig

Chevereto Member
Hello, I have 2.1 configured properly and am trying to upload an image from a native iOS app. The block of code I'm using to do the API call is as follows, I get an error 403 response with "invalid source (thumb)" as the response.

NSData* imgDataJpg = UIImageJPEGRepresentation(image, 1.0);
NSString* imgDataEncoded = [imgDataJpg base64Encoding];

NSMutableURLRequest *request = [NSMutableURLRequest
requestWithURL:[NSURL URLWithString:mad:"http://<mydomain>/chevereto/api.php"]];
NSString* kAPIKey = @"secretstuff";

NSMutableString* requestParams =
[NSMutableString stringWithFormat:mad:"key=%@&format=xml&upload=%@",
kAPIKey, imgDataEncoded];

[request setHTTPMethod:mad:"POST"];
[ request setValue:mad:"application/x-www-form-urlencoded" forHTTPHeaderField:mad:"content-type"];
[request setHTTPBody:[requestParams dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection* conn = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

Has anyone dealt with this before? I can see on the server that the uploaded image is invalid but appears to have the right size. Might be an issue with the base64 encoding. I've tried several options for doing this in iOS, they've all failed. Currently using Nick Lockwood's component, found here: https://github.com/nicklockwood/Base64
 
It seems that you have trouble with the resize function. Do you have GD installed?
 
Rodolfo said:
It seems that you have trouble with the resize function. Do you have GD installed?

Yes, I checked by outputting phpinfo() and gd is enabled (2.0.34 compatible)

I can upload the same image via the default web page, so I think the environment is properly set up.
 
Thing is that you should check the thumbnails not the normal images. Which is your site URL?
 
Rodolfo said:
Thing is that you should check the thumbnails not the normal images. Which is your site URL?

When I switch back and forth between trying to upload encoded PNG and JPEG data, I get different errors. For PNG it's invalid source, and for JPG it's invalid mime.
 
It seems that this:

PHP:
stream_filter_append($fh,'convert.base64-decode',STREAM_FILTER_WRITE);

Is not working on api.php, all the images uploaded using the base64 image string just don't work. I'm still checking the error.
 
This is clearly not a server error is a script error. Topic has been moved from api back to Tech Support again.

I'm sorry but the API is failing pretty bad. I will need more time to fix this.
 
Rodolfo said:
This is clearly not a server error is a script error. Topic has been moved from api back to Tech Support again.

I'm sorry but the API is failing pretty bad. I will need more time to fix this.

Thanks for looking into this. In your testing, please ensure you try all supported image formats. There is a bit of code in api.php for base64 uploads which appears to be jpg-specific.
 
That is also related, the base64 string is incompleted. Try to convert a image (an icon for instance) using an online converter and then define $to_upload with that string on api.php and you will see that works.
 
btw, you should compare the result that you get with that tool you are using with other base64 image converters.
 
Rodolfo said:
Hi hagig, do you still have this issue?

Haven't explored it further yet, will get back to you once I have some results. I would suggest that the work in supporting base64 is not "done" even if I can get it working. A web API should be standards compliant and if I formed the request according to the RFC 1867 standard at http://www.ietf.org/rfc/rfc2388.txt it would not work with your code. So I'd suggest incorporating support for multipart/form-data requests and not just having it encoded in the url as it currently stands.

I do appreciate your follow-up and will get back to you after some more testing on my end. I want to compare data encoded on the iOS base64 extensions to some of the web-based ones and see what's different. I know that if I run the base64 encode and decode on the same data and then redisplay the image, it is intact.
 
I can enable $_FILES support for posting but the use of base64 is for your benefit because it uses a stream, is the best way to do large data submit between servers.

I understand your frustation but we are talking about how make thinks work properly, we can't stick to 1998 standars, but for me add multipart (files) is trivial and perhaps other devs need this feature, mostly because the base64 encoders not always do the right job.

I can do this for 2.1.2, if you want I can upload the new api once is done so you dont need to wait until I release this 2.1.2 version.
 
Rodolfo said:
I can enable $_FILES support for posting but the use of base64 is for your benefit because it uses a stream, is the best way to do large data submit between servers.

I understand your frustation but we are talking about how make thinks work properly, we can't stick to 1998 standars, but for me add multipart (files) is trivial and perhaps other devs need this feature, mostly because the base64 encoders not always do the right job.

I can do this for 2.1.2, if you want I can upload the new api once is done so you dont need to wait until I release this 2.1.2 version.

Thanks Rodolfo, I got it working. The problem wasn't the base64 encoding but the fact that after you encode the data, you have to process it to escape certain characters for url encoding. I found an open source iPhone Imgur uploader example, and adding the url escape code resolved my issues.

For anyone interested, the public repo is here: http://github.com/blladnar/iPhone-Imgur-Uploader/
 
Status
Not open for further replies.
Back
Top