Class: Raw::Mixin::WebFile

Inherits:
Object
  • Object
show all
Defined in:
lib/raw/mixin/webfile.rb

Overview

A Web File.

You can customize the path where the uploaded file will be by defining a webfile_path class method before the property:

class Icon

def self.webfile_path request, name
  File.join(Uploads.public_root, request.user.name, 'icon.png')
end      

attr_accessor :file, WebFile, :magick => { :small => '64x64', :medium => '96x96' }

end – TODO: webfile_path customization sucks, should be improved! ++

Class Method Summary collapse

Class Method Details

.included_as_property(base, args) ⇒ Object

Modify the base class when this class is included as a property – TODO: find a better name. ++



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/raw/mixin/webfile.rb', line 41

def self.included_as_property(base, args)
  if args.last.is_a?(Hash)
    options = args.pop
  else
    options = Hash.new
  end
  
  args.pop if args.last.is_a?(Class)
  
  if thumbnails = (options[:thumbnail] || options[:thumbnails] || options[:magick]) or self.name == 'WebImage'
    require 'glue/thumbnails'
    base.send :include, Thumbnails
    thumbnails = { :small => :thumbnails } if thumbnails.is_a?(String) 
  end
  
  for name in args
    base.module_eval do
      # The 'web' path to the file (relative to the public
      # root directory. Uses the original property name
      # or the #{name}_path alias.
               
      attr_accessor name.to_sym, String, :control => :file
      alias_method "#{name}_path".to_sym, name.to_sym
      
      # The file size.
      
      attr_accessor "#{name}_size".to_sym, Fixnum, :control => :none
      
      # The mime type.

      attr_accessor "#{name}_mime_type".to_sym, String , :control => :none
   
      # Assignment callbacks.        
      #--
      # gmosx, FIXME: this is a hack!! better implementation
      # is needed (generalized property assigners).
     
      inheritor(:assign_callbacks, [], :merge) unless @assign_callbacks        
    end

    if thumbnails
      for tname in thumbnails.keys
        base.module_eval do
          attr_accessor "#{name}_#{tname}_thumbnail".to_sym, String, :control => :none
        end        
      end
    end

    code = %{
      def #{name}_real_path
        File.join(Nitro::Server.public_root, @#{name})
      end

      def #{name}_from_request(request)
        param = request['#{name}']
        
        return if param.nil? or param.original_filename.blank?
    }
    if base.respond_to? :webfile_path
      code << %{
        path = #{base}.webfile_path(request, '#{name}')
      }
    else
      code << %{
        path = File.join(WebFile.upload_root, WebFile.sanitize(param.original_filename))
      }
    end
    
    code << %{ 
        @#{name} = path
        @#{name}_size = param.size
        
        real_path = #{name}_real_path
        raise 'file exists' if !WebFile.override_files and File.exists?(real_path)
        FileUtils.mkdir_p(File.dirname(real_path))
        if param.path
          FileUtils.cp(param.path, real_path)
        else
          # gmosx FIXME: this is a hack!!
          param.rewind
          File.open(real_path, 'wb') { |f| f << param.read }
        end
        FileUtils.chmod(0664, real_path)
    }

    if thumbnails
      for tname, geostring in thumbnails
        code << %{
          @#{name}_#{tname}_thumbnail = Thumbnails.generate_thumbnail(path, '#{tname}', '#{geostring}') 
        }
      end
    end
          
    code << %{          
      end
      
      def delete_#{name}
        FileUtils.rm(#{name}_real_path)
      end

      assign_callbacks! << proc { |obj, values, options| 
        obj.#{name}_from_request(values)
      }
    }
    
    base.module_eval(code)
  end
end

.sanitize(filename) ⇒ Object

Sanitize a filename. You can override this method to make this suit your needs.



153
154
155
156
157
# File 'lib/raw/mixin/webfile.rb', line 153

def self.sanitize(filename)
  ext = File::extname(filename)
  base = File::basename(filename, ext).gsub(/[\\\/\? !@$\(\)]/, '-')[0..64]
  return "#{base}.#{ext}"
end