Class: GollumRails::Upload
- Inherits:
-
Object
- Object
- GollumRails::Upload
show all
- Extended by:
- ActiveModel::Callbacks, ActiveModel::Naming
- Includes:
- ActiveModel::Conversion, ActiveModel::Model, ActiveModel::Validations, Attributes, Store, ClassDefinitions
- Defined in:
- lib/gollum_rails/upload.rb,
lib/gollum_rails/upload/class_definitions.rb,
lib/gollum_rails/upload/file_too_big_error.rb,
lib/gollum_rails/upload/blacklisted_filetype_error.rb
Defined Under Namespace
Modules: ClassDefinitions
Classes: BlacklistedFiletypeError, FileTooBigError
Constant Summary
collapse
- API_VERSION =
3
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Attributes
#assign_attributes
Constructor Details
#initialize(attrs) {|_self| ... } ⇒ Upload
Returns a new instance of Upload.
27
28
29
30
|
# File 'lib/gollum_rails/upload.rb', line 27
def initialize(attrs)
assign_attributes(attrs)
yield self if block_given?
end
|
Instance Attribute Details
#commit ⇒ Object
Returns the value of attribute commit.
24
25
26
|
# File 'lib/gollum_rails/upload.rb', line 24
def commit
@commit
end
|
#destination ⇒ Object
Returns the value of attribute destination.
23
24
25
|
# File 'lib/gollum_rails/upload.rb', line 23
def destination
@destination
end
|
#file ⇒ Object
Returns the value of attribute file.
22
23
24
|
# File 'lib/gollum_rails/upload.rb', line 22
def file
@file
end
|
#gollum_file ⇒ Object
Returns the value of attribute gollum_file.
25
26
27
|
# File 'lib/gollum_rails/upload.rb', line 25
def gollum_file
@gollum_file
end
|
Class Method Details
.create(attributes) ⇒ Object
97
98
99
|
# File 'lib/gollum_rails/upload.rb', line 97
def self.create(attributes)
self.new(attributes).save
end
|
.find(path) ⇒ Object
101
102
103
104
105
|
# File 'lib/gollum_rails/upload.rb', line 101
def self.find(path)
ins = self.new({})
ins._update_gollum_file(path)
ins
end
|
Instance Method Details
#_find_gollum_file(path) ⇒ Object
128
129
130
|
# File 'lib/gollum_rails/upload.rb', line 128
def _find_gollum_file(path)
wiki.file(path)
end
|
#_update_gollum_file(path) ⇒ Object
132
133
134
135
136
137
138
|
# File 'lib/gollum_rails/upload.rb', line 132
def _update_gollum_file(path)
if path
self.gollum_file = _find_gollum_file(path)
else
self.gollum_file = nil
end
end
|
#destroy(commit = nil) ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/gollum_rails/upload.rb', line 77
def destroy(commit=nil)
return false if !persisted?
committer = Gollum::Committer.new(self.class.wiki, commit||self.commit)
committer.delete(self.gollum_file.path)
committer.after_commit do |index, sha|
path = self.gollum_file.path
dir = ::File.dirname(path)
dir = '' if dir == '.'
fullname = ::File.basename(path)
ext = ::File.extname(fullname)
format = ext.split('.').last || "txt"
filename = ::File.basename(fullname, ext)
self.class.wiki.clear_cache
index.update_working_dir(dir, filename, format)
end
sha = committer.commit
_update_gollum_file(nil)
sha
end
|
#persisted? ⇒ Boolean
107
108
109
|
# File 'lib/gollum_rails/upload.rb', line 107
def persisted?
!!self.gollum_file
end
|
#save ⇒ Object
67
68
69
70
71
72
73
74
75
|
# File 'lib/gollum_rails/upload.rb', line 67
def save
save!
rescue Gollum::DuplicatePageError
_update_gollum_file(File.join(@dir,@fullname))
self
rescue FileTooBigError => e
@error = e.message
self
end
|
#save! ⇒ Object
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
|
# File 'lib/gollum_rails/upload.rb', line 32
def save!
if self.file
fullname = Gollum::Page.cname(self.file.original_filename)
tempfile = self.file
validate! tempfile
end
@dir = self.destination || self.class.destination
@fullname = fullname
ext = ::File.extname(fullname)
format = ext.split('.').last || "txt"
filename = ::File.basename(fullname, ext)
if tempfile.respond_to?(:tempfile)
contents = ::File.read(tempfile.tempfile)
else
contents = ::File.read(tempfile)
end
head = self.class.wiki.repo.head
@commit ||= {}
options = self.commit.merge(parent: head.commit)
committer = Gollum::Committer.new(self.class.wiki, options)
committer.add_to_index(@dir, filename, format, contents)
committer.after_commit do |cmntr, sha|
self.class.wiki.clear_cache
cmntr.update_working_dir(@dir, filename, format)
end
committer.commit
_update_gollum_file(File.join(@dir,@fullname))
self
end
|
#to_s ⇒ Object
140
141
142
143
144
145
146
|
# File 'lib/gollum_rails/upload.rb', line 140
def to_s
if @error
"Error occured: <%s>" % @error
else
super
end
end
|
#validate!(tempfile) ⇒ Object
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/gollum_rails/upload.rb', line 111
def validate!(tempfile)
if self.class.max_size
raise FileTooBigError,
"File is too big. Max. size is #{self.class.max_size}" if tempfile.size > self.class.max_size
end
ext = ::File.extname(tempfile.original_filename)
type = (ext.split('.').last || "txt").to_sym
if self.class.whitelist
raise BlacklistedFiletypeError,
"Filetype #{type} is blacklisted." unless self.class.whitelist.include?(type)
end
if self.class.blacklist
raise BlacklistedFiletypeError,
"Filetype #{type} is blacklisted." if self.class.blacklist.include?(type)
end
end
|