Reputation: 2071
I am trying to convert the lines of an uploaded text file attachment using a Paperclip processor in rails. I can verify with a debugger that my processor is called, but the file that gets attached is my original file, not the file written by the processor. Here is my processor:
module Paperclip
class Utf8 < Processor
def initialize(file, options={}, attachment=nil)
super
@file = file
@attachment = attachment
@current_format = File.extname(@file.path)
@format = options[:format]
@basename = File.basename(@file.path, @current_format)
end
def make
@file.rewind
tmp = Tempfile.new([@basename, @format])
IO.foreach(@file.path) do |line|
tmp << line.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
end
tmp.flush
tmp
end
end
end
and here is my model:
class List < ActiveRecord::Base
has_attached_file :file,
storage: :s3,
s3_credentials: Rails.root.join('config', 'aws.yml'),
bucket: Rails.application.config.s3_bucket,
processors: [:utf8],
styles: {
utf8: {
format: 'txt'
}
}
end
Any idea what I'm doing wrong? As I understand it, the file returned from make is what paperclip attaches to the model. Might s3 have anything to do with this?
Upvotes: 1
Views: 455
Reputation: 2071
Found a solution: paperclip saves each style is as a separate file. To overwrite the original file instead of creating a new one, I had to change my model to:
class List < ActiveRecord::Base
has_attached_file :file,
storage: :s3,
s3_credentials: Rails.root.join('config', 'aws.yml'),
bucket: Rails.application.config.s3_bucket,
processors: [:utf8],
styles: {
original: { # specifying original style here causes the original file to be overwritten
format: 'txt'
}
}
end
Upvotes: 1