Last active
August 29, 2015 14:23
-
-
Save laginha87/a32da4d74076483236ea to your computer and use it in GitHub Desktop.
zip paperclip attachment processor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'zip' | |
# requires gem rubyzip | |
# usage has_attached_file :attachment, path: path, processors: %i(archive), styles: {original: {format: 'zip', archived_file_name:}} | |
module Paperclip | |
class Archive < Processor | |
def make | |
tempfile = create_zip_tempfile | |
Zip::File.open(tempfile.path, Zip::File::CREATE) do |zip| | |
zip.add(archived_file_name, @file.path) | |
end | |
tempfile | |
end | |
def archived_file_name | |
return @file.original_filename unless options[:archived_file_name] | |
options[:archived_file_name] + File.extname(@file.original_filename) | |
end | |
def create_zip_tempfile | |
Tempfile.new(%w(archive .zip)).tap {|tempfile| Zip::OutputStream.open(tempfile) { } } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment