Skip to content
This repository has been archived by the owner on Jun 10, 2022. It is now read-only.

Difficult to tell when an image has been provided, and can't delete an image cleanly #21

Open
brendanhatton opened this issue Nov 18, 2014 · 1 comment

Comments

@brendanhatton
Copy link

I have raised these together as one might be causing the other.

Firstly, I have a User model with an uploader mounted as photo_thumb. I can store and retrieve images fine. Every user is allocated an oid in the photo_thumb attribute, regardless of whether or not they have provided a photo - is this expected? As a result, my code to determine whether to display the image or use a placeholder is:
photo_thumb_url.present? && photo_thumb.file.size > 0

which was working ok, until I wanted to allow the user to delete an image. I did this by calling
user.remove_photo_thumb!
which I believe deletes the image from the db, but the user.photo_thumb attribute is still the oid, and when my code calls photo_thumb.file.size, I receive an error

PG::Error: can't open large object: ERROR: large object 41263 does not exist
(41263 is the oid for this users photo_thumb)

I was wondering if you could explain how I can determine initially if a user has provided an image or not (or if the file size test is the best way) - and secondly how I can safely delete a file that has been uploaded, such that it behaves the same as a user that has never uploaded a file.

I am using rails 4 and using the postgresql_lo_streamer gem for the display of images.

Thanks in advance!

@rbatta
Copy link

rbatta commented Dec 8, 2015

Although OP wrote this over a year ago, I recently encountered this as well with ruby 2.0.0 and rails 4.0.13 using carrierwave-postgres 0.1.4. Our PDFs were the ones that were affected. Our .length.zero? checks on views and in our model returned that same error as OP after being batch deleted with carrierwave's remove_pdf! method. Basically rendering any page that called for PDFs that no longer existed, though Postgres still had a reference ID for it, gave us the that error in the logs:

PG::Error: can't open large object: ERROR: large object 61477 does not exist

I should note that deleting pdf's individually (vs batch) still worked for us. r.remove_pdf! on a single entry worked and made the value nil as expected. For whatever reason though, running it against many 100s of records made it go kaput.

How I understand it is that the referenced oid wasn't removed and replaced with nil after a certain while in the batch deletion process. We came up with a workaround/solution.

In our model, we added this method.

def pdf_exists?
    begin
        self.class.connection.raw_connection.lo_open(package_pdf.identifier.to_i)
        return true
    rescue => e
        return false
    end
end

Since our views relied on @pdf.length.zero?, we swapped that out for @pdf.pdf_exists? and voila! A working workaround. :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants