Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trumbowyg: Django server can detect file upload but not image URL input

Tags:

I'm using Trumbowyg, a WYSIWYG JavaScript editor which has a feature of rendering images from URLs pasted in. It also has an upload plugin which enables uploading local images and custom server side handling.

My python/django function upload_image() can successfully detect the uploaded image - however when I use the URL image input, my python function cannot detect it. Trumbowyg simply renders the image without going through my python backend.

Here's my code:

$('#id_content').trumbowyg({
    btnsDef: {
        // Create a new dropdown
        image: {
            dropdown: ['insertImage', 'upload'],
            ico: 'insertImage'
        }
    },
    // Redefine the button pane
    btns: [
        ['strong', 'em', 'del'],
        ['link'],
        ['image'], // Our fresh created dropdown
    ],
    plugins: {
        // Add imagur parameters to upload plugin for demo purposes
        upload: {
            serverPath: '/upload_image/',
            fileFieldName: 'content_image',
            urlPropertyName: 'url'
        }
    }
});
def upload_image(request):
    print('Success') #only prints when I use the upload input, not the URL input

Why can in detect the uploaded image but not the URL input?

like image 832
Zorgan Avatar asked Jul 04 '18 04:07

Zorgan


1 Answers

As already pointed, Trumbowyg doesn't send the URL to backend if the user uploads the image using a URL.

But if you really want to host the images on your own server, there's a way you can do that.

When the user submits the form, you'll receive the content of the textarea in your backend. You can then read the content and look for <img src="..."> tag.

At that point, you can check if the src value doesn't start with your S3 bucket hostname, you can download that image using urllib or requests library, save it to your bucket and replace the src value.

Since the submitted data will be in HTML format, check out the excellent Beautiful Soup. It will make parsing HTML easy.

like image 197
xyres Avatar answered Oct 07 '22 02:10

xyres