abrudtkuhl / WordPressSharp

A C# client to to interact with the WordPress XML-RPC API

Home Page:abrudtkuhl.github.io/WordPressSharp/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

UploadFile : worked a couple of times, and now doesn't

Nefastor opened this issue · comments

Hi,

I'm attempting to upload images to a WordPress install using the WordPressClient.UploadFile method. The code I use is very straightforward and it worked a couple of time, resulting in proper entries in the site's media library. Then I went out for lunch, tried to upload more files, and it didn't work. I tried to upload the first file again, it didn't work either.

I put a breakpoint at the UploadFile call, hoping to have a look at its return value. However, when when I hit F10 (Step Over) I ended up in the calling method's calling method, bypassing other breakpoints along the way.

So I get a piece of code that worked once, and now doesn't, and I can't get a clue as to what's wrong. I'm not excluding the possibility of a server-side issue (it's the first time I try to upload to a site programmatically) but even then I'd expect UploadFile to behave more "normally" when debugging. Can you tell me what's going on ?

By the way, I've installed WordPressSharp from NuGet. I'm using Visual Studio 2017. Network connectivity is OK, and I own the website to which I'm trying to post. Here's my method for uploading an image file (those are all PNG's by the way)

(oops... I can't seem to find a way to paste my code so it'll be readable. I'll try again later.)

Here's the code I'm using :

string UploadImageRPC(string FileName)
        {
            // Create a site configuration
            WordPressSiteConfig conf = new WordPressSiteConfig();
            conf.BaseUrl = "https://" + Site.Domain;
            conf.BlogId = 1;
            conf.Username = Site.User;
            conf.Password = Site.Password;
            // Now create a client
            WordPressClient client = new WordPressClient(conf);
            // Now use that client to upload a file :
            // Start by turning the file into a Data object
            Data file = new Data();            
            file.Name = FileName.Substring(FileName.LastIndexOf(@"\") + 1);  // Eliminate the path to the file
            file.Overwrite = false;
            file.Type = "image/png";
            // Encode the image data
            Byte[] bytearray = File.ReadAllBytes(FileName); // that works !!! wait... it only worked once :-(
            file.Bits = bytearray;
            // And now upload it
            UploadResult result = client.UploadFile(file);
            // Return the URL to the picture
            return result.Url;
        }

Thank you for the tip, Andy. I'll try that and get back to you. For information, I've been able to update a few files without changing my code, and then it started misbehaving again. I'm wondering if it may have something to do with my server. If updating the code won't work I'll contact my hosting provider.

So I did what you suggest, and there's no difference. Just to be sure I got it right : I cloned the repo, opened the solution in Visual Studio, rebuilt it, grabbed the DLL from the output folder and copied it to the packages\WordPressSharp.1.1.0\lib\net45 in my own project, then rebuilt that. Did I miss a step ?

Progress update : I've tried my code on a second PC that uses a different internet connection, and I got the same result. It's quite deterministic really : it stops working after I send 5 files in rapid succession. Those are PNG's between 2 and 5 KB so I don't think the size is the issue, rather I think the people who host my site might have some sort of "hacker protection" that detects those rapid uploads and prevents anything else from going through. What's annoying though is that I don't get some sort of alert... I will get in touch with them and we'll see what's what, but for what it's worth I can't find anything wrong with your code or the way I use it, and the best proof is that when it works, it works very well.

Hello Andy,

So yeah, the problem was my host. Their security software identified the XML-RPC file uploads as attacks designed to exploit CVE-2013-0235, a WordPress vulnerability. The details are at :

https://www.cvedetails.com/cve/CVE-2013-0235/

I think we can close the issue, but maybe it's worth mentioning somewhere that this is a scenario that can happen with some hosts.

Anyway, thanks for your time, and for WordPressSharp ! Even though I'll try to upload files to WordPress by some other means ASAP, it's nice to have something that works and that's easy to work with.