Carrierwave uploading file saves the file to AWS but the data doesnt persist after reload

231 Views Asked by At

Rails 6.1 with carrierwave 1.2.3 and fog 2.1.0

I'm attaching pdfs to a Document class internally using a DocsUploader. When saved everything looks to work fine and the document is uploaded to AWS and the filename is added to the model. The prob is when i reload or retrieve the model the file and data isn't persisting.

There is another place in the code whereby i'm using the exact same uploader with a different model to upload a pdf and that works fine. Any tips on how i can debug, or what i can try as i've tried a lot of things.. it all looks to work fine but just doesn't save in the db. Here is my output and code.

    [2] pry(#<DocumentSigner>)> self.document.file

=> #<DocsUploader:0x00007fd78d9cc590
 @cache_id="1628692242-77414-0004-4934",
 @cache_storage=
  #<CarrierWave::Storage::File:0x00007fd78d9c50b0
   @cache_called=nil,
   @uploader=#<DocsUploader:0x00007fd78d9cc590 ...>>,
 @file=
  #<CarrierWave::SanitizedFile:0x00007fd78d9c6988
   @content=nil,
   @content_type="application/pdf",
   @file=
    "/Users/chrisward/Sites/***/tmp/uploads/1628692242-77414-0004-4934/B00977.pdf20210811-77414-1igeaqe.pdf",
   @original_filename="B00977.pdf20210811-77414-1igeaqe.pdf">,
 @filename="B00977.pdf20210811-77414-1igeaqe.pdf",
 @length=2,
 @model=
  #<Document:0x00007fd78b427790
   id: 114661,
   uuid: "5b400351-538d-42ca-8254-1dc22384feb2",
   title: "r bar",
   description: nil,
   file: nil,
   signed: nil,
   created_at: Wed, 11 Aug 2021 13:13:40.000000000 BST +01:00,
   updated_at: Wed, 11 Aug 2021 14:23:33.000000000 BST +01:00,
   user_id: 740,
   status: "Signed",
   qrcode: "5b400351-538d-42ca-8254-1dc22384feb2.png",
   archived: nil,
   original_file_id: 131629,
   append_signatures: true,
   pages: 2,
   additional_info: nil,
   attachment_file_id: nil,


[3] pry(#<DocumentSigner>)> self.document.save
=> true


[5] pry(#<DocumentSigner>)> self.document.file.url
=> "https://development.s3-eu-west-1.amazonaws.com/uploads/document/file/000/114/661/B00977.pdf20210811-77414-1igeaqe.pdf?X-Amz-Expires=600&X-Amz-Date=20210811T143355Z&X-Amz-Algorithm=AWS4-HMAC-SHA256"


[7] pry(#<DocumentSigner>)> self.document
=> #<Document:0x00007fd78b427790
 id: 114661,
 uuid: "5b400351-538d-42ca-8254-1dc22384feb2",
 title: "r bar",
 description: nil,
 file: "B00977.pdf20210811-77414-1igeaqe.pdf",

But after reload or retrieving from the db

[8] pry(#<DocumentSigner>)> self.document.reload
=> #<Document:0x00007fd78b427790
 id: 114661,
 uuid: "5b400351-538d-42ca-8254-1dc22384feb2",
 title: "r bar",
 description: nil,
 file: nil,

[9] pry(#<DocumentSigner>)> self.document.file.url
=> nil

** The Code **

*document.rb*

class Document < ApplicationRecord
  mount_uploader :file, DocsUploader
end


*document_signer.rb*

after_save :update_document

document.save_document({
  :stamps       => stamps,
  :labels       => labels,
  :append_cover => @document.append_signatures,
  :encrypt      => get_overall_document_status == "Signed" }) do |file|
     self.document.file = file
end

def update_document
  self.document.save!
end

*docs uploader*

# encoding: utf-8

class DocsUploader < CarrierWave::Uploader::Base

  require 'docsplit'
  require 'fileutils'

  process :set_page_count
  process :set_content_type
  before :store, :length

  def filename
    if original_filename
      converted_filename = original_filename.sub(/[^.]+\z/,"pdf")
      model.filename = converted_filename if model.instance_of?(OriginalFile)
      converted_filename
    end
  end

  def fingerprint
    Digest::SHA1.hexdigest(File.read(current_path))
  end

  def timestamp
    var = :"@#{mounted_as}_timestamp"
    model.instance_variable_get(var) or model.instance_variable_set(var, Time.now.to_i)
  end

  def cache_dir
    Rails.root.join 'tmp/uploads'
  end

  def store_dir
    "#{base_store_dir}/#{model_id_partition}"
  end

  def base_store_dir
     if Rails.env.test?
       "#{Rails.root}/spec/support"
     else
      "uploads/#{model.class.to_s.underscore}/#{mounted_as}"
     end
  end

  def model_id_partition
    ("%09d" % model.id).scan(/\d{3}/).join("/")
  end

  def extension_white_list
    %w(pdf doc docx rtf txt html ppt tiff tif xls xlsx csv key rtf numbers pages jpg jpeg png)
  end

  def pdf?
    current_path[-3,3].downcase == 'pdf'
  end

  def pdf
    current_path[0..current_path.rindex('.')] + 'pdf'
  end

  def length(file)
    if Rails.env.test?
      @length ||= 0
    else
      begin
        @length ||= Docsplit.extract_length(current_path)
      rescue Exception => e
        raise CarrierWave::ProcessingError, e.message
      end
    end
  end


  private

  def output_path
    "#{Rails.root}/tmp"
  end

  def set_content_type(*args)
    self.file.instance_variable_set(:@content_type, "application/pdf")
  end

  def set_page_count
    model.pages = length(nil) if model.has_attribute?('pages')
  end

  def secure_token
    var = :"@#{mounted_as}_secure_token"
    hash = Digest::SHA1.hexdigest("#{Time.now.to_i}#{model.id}#{read}")
    model.instance_variable_get(var) or model.instance_variable_set(var, hash)
  end
end
0

There are 0 best solutions below