0

I'm working on a project and for ACID compliancy reasons, images are stored in the database. I'm running flask/jinja/SQLAlchemy. I have images going to an images table, in a largebinary column. Upon calling images from the database I get a bunch of "gibberish" as output. Snippets of code below. I'm using a for loop to display images from the database in jinja but not working as expected.

        image = request.files['new_image']
        new_image= Images(creationdatetime=now, image=image.read())
        db.session.add(new_image)
        db.session.commit()

and in html/jinja

        {% for image in images%}
            <li><img src="{{image.image}}"></li>
        {%endfor%}

with regards to displaying logic:

images=Images.query.all()
return render_template('/admin/index.jinja2', images=images)

I get a bunch of gibberish. A new record is created in postgres, checked through psql. Any suggestions?

Cliff Ribeiro
  • 77
  • 1
  • 1
  • 7
  • not the solution to the question you ask, but it would be possible to have an ACID compliant system while storing the image on disk and keeping only the image path in your database, if a new image is uploaded, replacing an existing image, the new image is stored on disk first & the associated record gets updated in the database. The database is still acid compliant. Periodically you have to _garbage collect_, i.e. get all the image paths from the database, and delete the actual images if they don't exist in your list of paths from database & have been created a while ago – Haleemur Ali Mar 27 '21 at 03:13
  • You probably want to configure the `src` attribute as a [data-uri](https://en.wikipedia.org/wiki/Data_URI_scheme), for example like in the answers to [this question](https://stackoverflow.com/a/6375973/5320906) – snakecharmerb Mar 27 '21 at 10:26
  • haven't been able to figure this out. As far as I understand please correct me if I'm wrong, if the image is stored in postgres its stored as a binary file. I've read that this converts it to base64 and I have to decode it back with the appropriate mimetype to properly display. Does this sound accurate? – Cliff Ribeiro Mar 30 '21 at 15:09

0 Answers0