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

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

Open
Nefastor opened this issue Jun 26, 2018 · 7 comments
Open

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

Nefastor opened this issue Jun 26, 2018 · 7 comments

Comments

@Nefastor
Copy link

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.)

@Nefastor
Copy link
Author

Nefastor commented Jun 26, 2018

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;
        }

@abrudtkuhl
Copy link
Owner

abrudtkuhl commented Jun 26, 2018 via email

@Nefastor
Copy link
Author

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.

@Nefastor
Copy link
Author

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 ?

@Nefastor
Copy link
Author

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.

@abrudtkuhl
Copy link
Owner

abrudtkuhl commented Jun 26, 2018 via email

@Nefastor
Copy link
Author

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.

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

No branches or pull requests

2 participants