Skip to content
This repository has been archived by the owner on Mar 19, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1 from moonflash/master
Browse files Browse the repository at this point in the history
.swf extension fix
  • Loading branch information
Krule committed May 5, 2011
2 parents 3c51faa + c33c5b7 commit bf3947f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 34 deletions.
37 changes: 8 additions & 29 deletions README.md
Expand Up @@ -40,40 +40,19 @@ Use it as you would any other Paperclip processor. In your model:

class Document < ActiveRecord::Base
has_attached_file :document,
:styles => {
:original => { :params => "-z -j 100 -qq -G" }
},
:processors => [:pdf_to_swf]
has_attached_file :pdf,
:styles => {
:swf => {
:params => "-z -j 100 -qq -G",
:format => "swf" }
},
:processors => [:pdf_to_swf]
end

which will convert your pdf document into swf. However, pdf extension will be retained.
which will convert your pdf document into swf , and keep both files (.swf and .pdf) on the server

In order to avoid this I have created a method to normalize file name and change it's extension.

class Document < ActiveRecord::Base

has_attached_file :document,
:styles => {
:original => { :params => "-z -j 100 -qq -G" }
},
:processors => [:pdf_to_swf],
:path => ":rails_root/public/system/:attachment/:normalized_file_name",
:url => "/system/:attachment/:normalized_file_name"

Paperclip.interpolates :normalized_file_name do |attachment, style|
attachment.instance.normalized_file_name
end

def normalized_file_name
file = self.document_file_name.gsub( /[^a-zA-Z0-9_\-\.]/, '_')
file = "#{self.id}-#{File.basename(file, ".pdf")}".slice(0...32)
"#{file}.swf".downcase
end
end

However, if you think you can do better, feel free to fork.

Expand Down
5 changes: 3 additions & 2 deletions lib/pdf_to_swf-paperclip-processor.rb
Expand Up @@ -2,19 +2,20 @@
module Paperclip
class PdfToSwf < Processor

attr_accessor :params
attr_accessor :file, :params, :format

def initialize file, options = {}, attachment = nil
super
@file = file
@params = options[:params]
@current_format = File.extname(@file.path)
@basename = File.basename(@file.path, @current_format)
@format = options[:format]
end

def make
src = @file
dst = Tempfile.new([@basename.tableize])
dst = Tempfile.new([@basename, @format ? ".#{@format}" : ''])
begin
parameters = []
parameters << @params
Expand Down
2 changes: 1 addition & 1 deletion lib/pdf_to_swf-paperclip-processor/version.rb
@@ -1,7 +1,7 @@
module Pdftoswf
module Paperclip
module Processor
VERSION = "0.0.2"
VERSION = "0.0.3"
end
end
end
4 changes: 2 additions & 2 deletions pdf_to_swf-paperclip-processor.gemspec
Expand Up @@ -6,8 +6,8 @@ Gem::Specification.new do |s|
s.name = "pdf_to_swf-paperclip-processor"
s.version = Pdftoswf::Paperclip::Processor::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ["Armin Pašalić"]
s.email = ["apasalic@devlogic.eu"]
s.authors = ["Armin Pašalić", "Mojmir Novakovic-moonflash"]
s.email = ["apasalic@devlogic.eu","moonflash.as3@gmail.com"]
s.homepage = "https://github.com/Krule/pdf_to_swf-paperclip-processor"
s.summary = %q{Converts uploaded pdf to swf}
s.description = %q{This gem is simple Paperclip processor which uses swftools to convert uploaded pdf files to swf}
Expand Down

0 comments on commit bf3947f

Please sign in to comment.