Uploading images to WP Media Library using REST api from a Python Web App

[ad_1]

Hi, and apologies if this isn’t the right place for my question, but maybe there’s someone with a similar WordPress related project here.

I’m trying to build a Python web app using Flask. In short, I want to upload photos (using WTForms’ FileField) from a web app to the WordPress media library (through the REST api).

For context: The main use case for the web app is on an iOS device and the goal is to be able to input text data (to mysql database) and images (to WordPress media library) as part of an inventory management web application (which is then also connected to Woocommerce).

I’m facing a couple of issues.

1st issue:
I have to upload the image directly to the WP media library’s directory folder on the server. This is because of two sub-issues:

1.1. I can only use CURL post command to upload. Python’s “requests” library’s post command doesn’t work for some reason. When using it, I’m able to upload the photo to the media library, but somehow it gets corrupted. The file appears in the WP media library but is blank. (The file size also shrinks significantly so somehow, for some reason, information is lost.) CURL post commands work both in terminal and python.

Here’s the code for the “non-working” requests command.

headers = {
“Content-Disposition”: “attachment; filename=IMG_3333.jpeg”,
“Content-Type”: “image/jpeg”
}

with open(FILE_PATH, “rb”) as file:
files = {“file”: file.read()}

response = requests.post(URL, auth=(USER, APP_PASSWORD), headers=headers, files=files)

1.2. At first, the idea was to upload each image temporarily to the same server where the app is running and then re-upload the photo from there to WP. This also does not work.

I’m able to successfully upload the image to the temporary folder, but upon re-uploading it, the image gets corrupted (even if I use the otherwise working CURL command). Thus, the only option is to upload it directly to the media library folder, which works.

The temporarily uploaded image is perfectly viewable in its folder, but the re-uploaded photo in the WP media library appears to be in ASCII format. (So I’m guessing it’s being saved as a text file for some reason.)

2nd issue:
I’m unable to convert .HEIC files to .JPEG in python. I have tried both pyheif and pillow_heif libraries exactly as I’ve seen others use them online, but for some reason it doesn’t work. After conversion I just end up with a PIL.Image.Image instance with .format = None.

So I gave up on that for the time being, because with iOS, any uploaded .HEIC photo is automatically converted to .JPEG. But this surfaced yet another issue, which, I guess, relates to the size of the photo being uploaded:

UserWarning: The ‘session’ cookie is too large: the value was 2801443 bytes but the header required 26 extra bytes. The final size was 2801469 bytes but the limit is 4093 bytes. Browsers may silently ignore cookies larger than this.
samesite=samesite,
OSError: LSAPI: Socket read/write error

My question:
Does anyone happen have an idea on how it would be possible to upload (.HEIC) photos from a web app input field to the WordPress media library? I feel like it shouldn’t be this complicated and that there must already be a working solution for this use case?

[ad_2]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer