-
Notifications
You must be signed in to change notification settings - Fork 50
/
attach.rb
135 lines (108 loc) · 4.02 KB
/
attach.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
module Wagn::Model::Attach
def attach_array(rev_id=nil)
c=if rev_id || self.new_card? || selected_rev_id==current_revision_id
self.content
else
Card::Revision.find_by_id(selected_rev_id).content
end
!c || c =~ /^\s*<img / ? ['','',''] : c.split(/\n/)
end
def attach_array_set(i, v)
c = attach_array((cr=current_revision)&&cr.id)
if c[i] != v
c[i] = v
self.content = c*"\n"
end
end
def attach_file_name() attach_array[0] end
def attach_content_type() attach_array[1] end
def attach_file_size() attach_array[2] end
def attach_extension() attach.send( :interpolate, ':extension' ) end
def attach_file_name=(v)
return if !v # does this happen?
attach_array_set 0, v
attach_array_set 1, MIME::Types.type_for(v).first.to_s
# was having issues with browsers getting mime types wrong,
# eg application/octet-stream for pdfs in Firefox (both versions 4 and 10)
# this solution means we just do a lookup based on the extension.
# perhaps not ideal, but at least consistent. Not sure browsers do much more.
end
def attach_file_size=(v) attach_array_set(2, v) if v end
STYLES = %w{ icon small medium large original }
def attachment_style type_id, style
case type_id
when Card::TypeID
''
when Card::ImageID
if style.nil? || style.to_sym == :full
:original
else
style
end
end
end
def attachment_format(ext)
return nil unless ext.present? && attach
return nil unless original_ext = attach_extension
return original_ext if ['file', original_ext].member? ext
exts = MIME::Types[attach.content_type]
return nil unless exts
return ext if exts.find {|mt| mt.extensions.member? ext }
return exts[0].extensions[0]
rescue Exception => e
Rails.logger.info "attachment_format issue: #{e.message}"
nil
end
# FIXME: test extension matches content type
def attachment_link(rev_id) # create filesystem links to previous revision
if styles = case typecode
when 'File'; ['']
when 'Image'; STYLES
end
save_rev_id = selected_rev_id
links = {}
self.selected_rev_id = rev_id
styles.each { |style| links[style] = attach.path(style) }
self.selected_rev_id = current_revision_id
styles.each { |style| File.link links[style], attach.path(style) }
self.selected_rev_id = save_rev_id
end
end
def before_post_attach
at=self.attach
at.instance_write :file_name, at.original_filename
Card::ImageID == (type_id || Card.fetch_id( @type_args[:type] ) )
# returning true enables thumnail creation
end
def self.included(base)
base.class_eval do
has_attached_file :attach, :preserve_files=>true,
:default_url => "missing",
:url => ":base_url/:basename-:size:revision_id.:extension",
:path => ":local/:card_id/:size:revision_id.:extension",
:styles => { :icon => '16x16#', :small => '75x75',
:medium => '200x200>', :large => '500x500>' }
before_post_process :before_post_attach
validates_each :attach do |rec, attr, value|
if [Card::FileID, Card::ImageID].member? rec.type_id
max_size = (max = Card['*upload max']) ? max.content.to_i : 5
if value.size.to_i > max_size.megabytes
rec.errors.add :file_size, "File cannot be larger than #{max_size} megabytes"
end
end
end
end
end
end
module Paperclip::Interpolations
def local( at, style_name ) Wagn::Conf[:attachment_storage_dir] end
def base_url( at, style_name ) Wagn::Conf[:attachment_web_dir] end
def card_id( at, style_name ) at.instance.id end
def basename(at, style_name)
at.instance.name.to_cardname.to_url_key
end
def size(at, style_name)
at.instance.type_id==Card::FileID || style_name.blank? ? '' : "#{style_name}-"
end
def revision_id(at, style_name) at.instance.selected_rev_id end
end