4

I was trying to post an image in version 12.1 using the Stackexchange uploader. It brings up an error in mathematica

SETools`SEUploader`Private`stackImage::err: Server returner error: You didn\u0027t enter a valid URL

Any idea of the problem?

Thanks

| |
  • 1
    Huh? I'm using the uploader since the early days of 12.1 prerelease and it never failed me. For instance, i.stack.imgur.com/P0XVD.png was uploaded just now with 12.1. Do you always have these problems or just sometimes? Is your uploader version 130? What OS are you using (that should not matter really). Anyone else having problems? – halirutan Mar 27 at 18:18
  • 1
    @halirutan Just restarted mathematica and this time it works. It failed repeatably previously. uploader version is 130 and I am using Windows 10. Possibly a false alarm. I will let you know if it happens again. – Hugh Mar 27 at 19:22
  • If it happens again, ping me in Mathematica Chat. We can debug this when your installation is again in this weird state. – halirutan Mar 27 at 22:25
  • 1
    I'm voting to close this question as off-topic because the error cannot be reproduced. – bbgodfrey Mar 28 at 0:03
  • I am seeing an identical error. Mathematica v11.2 over Windows. – Emilio Pisanty Apr 2 at 17:53
  • 2
    ... and similar confirmations were reported earlier in chat. – Emilio Pisanty Apr 2 at 18:20
  • 1
    @halirutan I got a second error today. After selecting an image to post, the window with the image came up but there were no buttons to press. The buttons at the bottom were missing. – Hugh Apr 2 at 19:42
4

Update

The issue is fixed and the SE Uploader works for me on Mathematica version 12.1 and 11. This was only possible because Lukas provided everything necessary and I only had to convert it to Java. A big thanks for that!

The current version is 131 and you can try the "Update" button or simply use

Import["https://raw.githubusercontent.com/halirutan/Mathematica-SE-Tools/master/SETools/Installer.m"]

There are, however, some important things to note:

  • Due to https, Java needs to use TLSv1.x which unfortunately wasn't introduced until Java 7. Mathematica 9 uses Java 1.6, Mathematica 10.4 uses Java 1.7.0_13. In both versions, I get an IO error because the TLS version is not supported. That means I cannot even make a simple request to get the StackExchange page code. The issue came up in this SO post, but I have to admit I'm far out of my comfort zone here. Although there seems to be a way to use additional libraries to make it work, it'd take me a lot of reading to understand everything.

  • For uploading cells or notebooks, I had to fix the Hash issue (hopefully once and for all). When you upload an expression encoded in an image, I add a small consistency hash but since Hash[{1,2,3},"MD5"] gives different results in Mathematica 10 and 11, this never worked well. I fixed this now but it also means that you get a warning with every uploaded code from before. You can wrap AbortProtect[...] around everything to make it work. It wasn't supposed to be a security measure anyway.

  • In 2019, the upload time for images increased. I talked to Szabolcs about this back then, but now I know that it's not the uploader. I verified that even a tiny image takes a long time when uploaded purely within Java. That means SE added a delay or some other measure. It's annoying but I can assure you, it's not the uploader.

Now here are two positive things besides that the uploader works again:

  • I stripped all meta-information, but more importantly, the palette cannot save itself anymore. That means you won't get these stupid warnings about "the palette was changed in another program" or "the palette was used in a newer version", when you use it with different Mathematica versions.
  • The uploader will have the correct size in version 12 which it didn't on my system. Buttons are all visible. I hope it works for everyone.

Original

Confirmed. StackExchange did something to its network that won't let us upload images as we did before.

I investigated a bit, and I believe things have changed drastically. The relevant JS code for uploading is here and after pretty-printing it, the important stuff happens here

img

This means when you click on the upload image button in an SE post, a dialog opens. There you can choose an image file and when you then press "Add image", this portion is called.

To all the web-devs among us, I have the following question:

Can we imitate this? In particular, what is the fkey that is appended in line number 2341? Our current implementation of sending PNG bytes to SE is only 20 lines of Java code.

If we want to fix this, I need some help with debugging this JS code. If you believe you can help, just edit this answer, make a line below, add your name and write down the solution :)

In case we are not able to fix this, all I can say to our Uploader is: It's been a delight you were around for so long and you brought true sunlight into our hearts. You will be dearly missed.


Added by @LukasLang

The fkey is apparently some kind of XSRF protection. But it looks like it's easy to get one - an example Mathematica implementation looks like this:

StringCases["fkey: '" ~~ Shortest@fkey__ ~~ "'" :> fkey]@
 Import["https://stackexchange.com", "Source"]

And with the following, we can actually upload an image:

fkey = 
 First@StringCases["fkey: '" ~~ Shortest@fkey__ ~~ "'" :> fkey]@
   Import["https://stackexchange.com", "Source"]
(* "390e967caf1c8023d41975cced0b9e3049b5e297a3fa41ac418d7269ad750f9a" *)

image = FindFile["ExampleData/Ocelot.jpg"];

result = URLRead[HTTPRequest[
   "https://stackoverflow.com/upload/image?method=json&https=true",
   <|
    Method -> "POST",
    "Body" -> <|
      "file" -> File@image,
      "fkey" -> fkey
      |>,
    "Expect" -> "",
    "ContentType" -> "multipart/form-data"
    |>
   ],
  "Body"
  ]
(* "{\"Success\":true,\"Error\":0,\"UploadedImage\":\"https://i.\stack.imgur.com/zfLEC.jpg\"}" *)

Import@<|ImportString[result, "JSON"]|>["UploadedImage"]

image

| |
  • Life will not be the same any more without SEUploader :( – Nasser Apr 2 at 21:04
  • 2
    I checked my fkey using the Chrome devtools and it seems to track who I am, since it's different if I'm logged on vs if I'm not (b190a09bf9a7c42118167b3922ebc44a734c3961bf666725d2492c5e69480b35 seems to be the key for a new incognito window, no matter which window). Potentially you need to query this from the SE API, which would require an API connection which might be annoying to write, but presumably there's a Java client lib? – b3m2a1 Apr 2 at 21:21
  • 3
    I've updated the post with an example Mathematica implementation that seems to be able to upload an image successfully – Lukas Lang Apr 2 at 21:38
  • 1
    Both of you are awesome. I hope I find some time on the weekend to implement it in Java and revive our Uploader. – halirutan Apr 2 at 22:26
  • @halirutan If uploading ever becomes impossible, I would like to make the case that the encoder/decoder capability is still enormously useful, and it would still be good to have it around to turn cells and notebooks into images that can then be uploaded directly in the Write An Answer / A Question block. – Emilio Pisanty Apr 3 at 13:45
  • Thank you everyone for such great work. I just tried it and it worked. Image uploaded OK. This using V 12.1 on windows 10. One minor installation thing. When I first run the command (the Import command shown at top above), I get error that it can not delete AppData\Roaming\Mathematica\Applications\SETools, I suppose because it is in use by current mathematica. So had to exit MMA and delete the SETools folder from windows, then start MMA again and now the import command worked. – Nasser Apr 5 at 22:51
  • @Nasser Yep, this is likely possible. I cannot test on Windows, but I've heard of similar things before. – halirutan Apr 5 at 22:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .