Upload Image: MD5Sum problem
flyingdutchie
Registered Users Posts: 1,286 Major grins
When I upload an image, using Kallasoft Smugmug API, i provide the byte-array and its MD5 sum.
The MD5 sum for image http://www.streetsofboston.com/gallery/3827114/1#221257930 is
Content-Length = 56342
Content-MD5 = 8d57d127da7259e01ef9b92544b93c0
These are the values assigned to the HTTP Put upload as headers.
What am i doing wrong?
This is the error message:
wrong format (ByteCount given: , received. MD5Sum given: , actual.)
(why are the actual values not given in the error-message?)
- First i tried to upload it without the MD5Sum. That works fine.
- Then i retrieved the image-info (getInfo) from smugmug and noted the md5sum.
- Then i used the java messagedigest for MD5 and calculated the md5sum. The result is exactly the same as in point (2.).
- However, when i provide this same calculated MD5 sum during the upload, i get an error message from smugmug that the MD5 sum is incorrect... :scratch
This is the code called just before uploading imgData.
final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final BufferedInputStream bufIS = new BufferedInputStream(mData.mIS); final BufferedOutputStream bufOS = new BufferedOutputStream(bos); synchronized(smBuffer) { int didRead = bufIS.read(smBuffer); while (didRead > 0) { bufOS.write(smBuffer, 0, didRead); totalWritten += didRead; didRead = bufIS.read(smBuffer); } bufOS.flush(); imgData = bos.toByteArray(); try { final MessageDigest md5 = MessageDigest.getInstance("MD5"); final byte[] digest = md5.digest(imgData); for (byte bt : digest) { int intBt = ((int)bt) & (0x000000ff); [B]md5Sum[/B] += Integer.toHexString(intBt); } } catch (NoSuchAlgorithmException e) { [B]md5Sum[/B] = ""; } }
The MD5 sum for image http://www.streetsofboston.com/gallery/3827114/1#221257930 is
Content-Length = 56342
Content-MD5 = 8d57d127da7259e01ef9b92544b93c0
These are the values assigned to the HTTP Put upload as headers.
What am i doing wrong?
This is the error message:
wrong format (ByteCount given: , received. MD5Sum given: , actual.)
(why are the actual values not given in the error-message?)
I can't grasp the notion of time.
When I hear the earth will melt into the sun,
in two billion years,
all I can think is:
"Will that be on a Monday?"
==========================
http://www.streetsofboston.com
http://blog.antonspaans.com
When I hear the earth will melt into the sun,
in two billion years,
all I can think is:
"Will that be on a Monday?"
==========================
http://www.streetsofboston.com
http://blog.antonspaans.com
0
Comments
can u post the raw http put request ?
Cheers,
David
SmugMug API Developer
My Photos
Wow! Talk about a quick reply! Thanks!
Here is the raw data (PUT and response): (i removed all the gibberish binary data).
And if Content-MD5: 8d57d127da7259e01ef9b92544b93c0 is not sent, all goes fine.
When I hear the earth will melt into the sun,
in two billion years,
all I can think is:
"Will that be on a Monday?"
==========================
http://www.streetsofboston.com
http://blog.antonspaans.com
I fought with something similar to this a year or so ago. Forgive me if my memory is flaky, I might be barking up the wrong tree.
If I remember correctly there are issues with making sure that the padding on hex strings is done correctly, otherwise they get rejected.
In C# the chunk of code that I have used is (straight out of my source code repository, shout if it doesn't make any sense)
int byteCount = System.Convert.ToInt32(file.Length); //Get byte count for image
string md5sum = null;
//Compute the MD5 Hash for the image
using (FileStream fs = new FileStream(file.FullName, FileMode.Open, FileAccess.Read))
{
//Compute the hash
MD5 md5 = new MD5CryptoServiceProvider();
byte[] hash = md5.ComputeHash(fs);
//Convert it to the format that smugmug want (0 padded bytes, lower case)
StringBuilder buff = new StringBuilder();
foreach (byte hashByte in hash)
{
buff.Append(String.Format("{0:x2}", hashByte));
}
md5sum = buff.ToString();
} //Dispose of the file stream
The Java string formatting APIs will have something similar.
I remember that it wasn't exactly fun finding out what the issue was. Hopefully this will speed you on your way.
Luke
SmugSoftware: www.smugtools.com
Thank you Luke.
Comparing your code to mine, i'm doing the same thing. I get 16 bytes (signed bytes), i make them unsigned and convert each of them in to 2-character hex-digits. The final result is exactly the same as the value returned by Smugmug when i do a 'images.getInfo' on the image.
-- Anton
When I hear the earth will melt into the sun,
in two billion years,
all I can think is:
"Will that be on a Monday?"
==========================
http://www.streetsofboston.com
http://blog.antonspaans.com
Try this out, I have a FF extension for uploading testing.
It generates the MD5 hash, so you can compared to Riyad's java api.
Let me know how you get on.
David
SmugMug API Developer
My Photos
I tried it, but when i run it, i get this error:
When I hear the earth will melt into the sun,
in two billion years,
all I can think is:
"Will that be on a Monday?"
==========================
http://www.streetsofboston.com
http://blog.antonspaans.com
SmugMug API Developer
My Photos
It runs now, but the page is entirely transparent... i can only see the borders of the fields and the field-groups. All the backgrounds are transparent...
I'll try anyway and see if i can get it to work.
When I hear the earth will melt into the sun,
in two billion years,
all I can think is:
"Will that be on a Monday?"
==========================
http://www.streetsofboston.com
http://blog.antonspaans.com
I got it to work... luckily i could click on the input-fields and type in stuff.
However, all the transparent areas are click-through. Instead of clicking on the upload form, my desktop gets focus. Therefore i can not copy (to the clipboard) from the plug-in page.
However, the 'Request' part on the right side shows me the MD5-Sum. It is the exactly the same as i got: 8d57 etc etc... 03c0
The raw request data looks different, though:
- POST instead of PUT
- xmlrawadd.mg instead of /hack/jason/1.2.0
- filename is me.jpg instead of C:\me.jpg
- Host is api.smugmug.com instead of upload.smugmug.com
But Content-Length and Content-MD5 are exactly the same.When I hear the earth will melt into the sun,
in two billion years,
all I can think is:
"Will that be on a Monday?"
==========================
http://www.streetsofboston.com
http://blog.antonspaans.com
Anton,
this could be the problem...both the PUT location and FileName, should only have the file not that path...
so it should be...
[code]
PUT /me.jpg HTTP/1.1
X-Smug-FileName: me.jpg
....
[/quote]
Give that a go.
Cheers,
David
SmugMug API Developer
My Photos
Give that a go.
Cheers,
David[/quote]
Alas, that did not work. I removed the "C:\" in front of the file-name and still it did not work.
BTW: Could the error-message be changed and actual specify what MD5-sum is expected?
Thanks!
When I hear the earth will melt into the sun,
in two billion years,
all I can think is:
"Will that be on a Monday?"
==========================
http://www.streetsofboston.com
http://blog.antonspaans.com
please email me the file you're trying to upload to david at [smugmug].
also, grab the latest version of the upload tester, i have fixed the transparent window issue.
David
SmugMug API Developer
My Photos
Hi David,
I ran the upload tester and here is a screen shot of the result. The md5-sum is the same. If the md5-sum is the same, what then is different?
PS: I'd like to attach a screen-shot, but DGrin is reporting that i already have "uploaded the maximum of 0 files"
When I hear the earth will melt into the sun,
in two billion years,
all I can think is:
"Will that be on a Monday?"
==========================
http://www.streetsofboston.com
http://blog.antonspaans.com
Here is my REST response I got when testing this. My HTTP Request should have been this: The above request resulted in a successful upload.
I modified my HTTP Request to this: The only thing I modified was subtracting 5 from the content-length.
I tried adding 5 to the content-length but my script died with an error because the file it was reading to send wasn't big enough or something.
I know I beat my head on this one for a bit last month, but in my case I was opening the image in ASCII mode instead of BINARY mode to calculate the file size to small.
Content-Length = byte_array.length - 5 ?
When I hear the earth will melt into the sun,
in two billion years,
all I can think is:
"Will that be on a Monday?"
==========================
http://www.streetsofboston.com
http://blog.antonspaans.com
Guys, sorry for the delay in responding, I finally got around to testing this and it worked fine using straight API call into the kallasoft SmugMug Java API images.UploadHTTPPut method.
The result, is a very suave-looking Anton
http://kalla.smugmug.com/gallery/3403336#223617633
kallasoft | The "Break It Down" Blog
I'm using the images.UploadHTTPPut method and it works great .... except that the MD5Sum is not accepted. Riyad, how did you get it to work with the MD5Sum?
PS. That image is from a b-day party of a friend of mine where i dressed up as a bouncer... it was pretty effective, since i almost turned a few people away... hehehehe
When I hear the earth will melt into the sun,
in two billion years,
all I can think is:
"Will that be on a Monday?"
==========================
http://www.streetsofboston.com
http://blog.antonspaans.com
I updated the implementation of UploadHTTPPut to now offer a slew of convenience methods that auto-fill out the HTTP header values for you, so you don't need to pass that stuff anymore. So for example passing in a session ID, album ID and File for an image, the API will load the image, get it's content length, calculate the MD5 hash for it, and set all the remaining headers then do the upload for you.
Additionally you can set all the geo-coding values too if you want. Overall it's just a lot friendly. BUT that being said, I didn't change any of the MD5 logic or anything like that... it just worked. Maybe the whole MD5 issue was a red herring before? Not sure. Either way it should be smoother with the new class.
I'll do another pre-M2 build for you and get it to you later.
kallasoft | The "Break It Down" Blog
I sent you an IM a little while ago with the text below. I don't know if you received it or not on your IM:
When I hear the earth will melt into the sun,
in two billion years,
all I can think is:
"Will that be on a Monday?"
==========================
http://www.streetsofboston.com
http://blog.antonspaans.com
Ahhh... so you were computing the MD5 independently in your API? I think this might explain why we were seeing a discrepency in the behavior.
In that case the changes I made won't help you, they are automated use-and-throw-away changes done with additional convenience methods... if your API still needs the MD5 (as a hash for example) then you'll still need to compute it yourself.
If it helps, I'm using the Apache, Commons-Codec project's DigesterUtils class, and the md5Hex method:
Maybe we could use the same method call to computer the MD5 so the APIs match up?
kallasoft | The "Break It Down" Blog
Hi Riyad,
I'll try that one (DigestUtils) when i get home. I have doubts it would help, because the MD5Sum that i calculate using my current code (MessageDigest) is the same one that is returned by 'images.getInfo' and the same one that is calculated by David's utility (http://hacks.introversion.com.au/SmugUploadTester)
David, did you figure out why my upload fails when i specify an MD5-sum?
When I hear the earth will melt into the sun,
in two billion years,
all I can think is:
"Will that be on a Monday?"
==========================
http://www.streetsofboston.com
http://blog.antonspaans.com
Anton,
I forwarded a copy of that image onto Riyad, so that he could verify whether or not he was seeing the same result.
I haven't heard back from him.
David
SmugMug API Developer
My Photos
Per David's request, here's my raw HTTP Put data:
kallasoft | The "Break It Down" Blog
Thanks!
"8d57d127da7259e01ef9b92544b903c0" is the same MD5 my code is sending. Content-Length is the same too "56342".
The main difference is that i uploaded my image using "PUT /services/api/json/1.2.0" and not through "PUT /services/api/json/1.2.1" and X-Smug-Version is set to "1.2.0" instead of "1.2.1".
When I hear the earth will melt into the sun,
in two billion years,
all I can think is:
"Will that be on a Monday?"
==========================
http://www.streetsofboston.com
http://blog.antonspaans.com
Anton,
putting to /services/api/json/1.2.0 is not required, /filename.jpg is fine. And I don't believe there are any code differences between 1.2.0 and 1.2.1
SmugMug API Developer
My Photos
Thanks
Then i don't know what the difference is. The MD5Sum that my code sends is correct, so why is the Smugmug server responding that the MD5 sum is incorrect? (see the raw request-data in one of my earlier messages)
When I hear the earth will melt into the sun,
in two billion years,
all I can think is:
"Will that be on a Monday?"
==========================
http://www.streetsofboston.com
http://blog.antonspaans.com
Anton,
Are you able to provide me with your app so I can do some testing ?
And if need be, do some debugging on internal servers ?
SmugMug API Developer
My Photos
I sent you an e-mail with all the code.
When I hear the earth will melt into the sun,
in two billion years,
all I can think is:
"Will that be on a Monday?"
==========================
http://www.streetsofboston.com
http://blog.antonspaans.com
David, Riyad and everyone else: So sorry to have taken up your time. Thank you very much for all your help.
My routine that translates a byte-array into a hex-string, skipped leading 0s.
My MD5Sum
8d57d127da7259e01ef9b92544b93c0
Actual MD5Sum
8d57d127da7259e01ef9b92544b903c0
Again, thanks!
When I hear the earth will melt into the sun,
in two billion years,
all I can think is:
"Will that be on a Monday?"
==========================
http://www.streetsofboston.com
http://blog.antonspaans.com
Anton no worries, I'm glad it's working. And I think this thread will be helpful to anyone else that wanders by running into the same problem, atleast they will know where to look right away.
kallasoft | The "Break It Down" Blog