]> git.openstreetmap.org Git - rails.git/blob - app/models/trace.rb
Improve test coverage
[rails.git] / app / models / trace.rb
1 class Trace < ActiveRecord::Base
2   self.table_name = "gpx_files"
3
4   belongs_to :user, :counter_cache => true
5   has_many :tags, :class_name => "Tracetag", :foreign_key => "gpx_id", :dependent => :delete_all
6   has_many :points, :class_name => "Tracepoint", :foreign_key => "gpx_id", :dependent => :delete_all
7
8   scope :visible, -> { where(:visible => true) }
9   scope :visible_to, ->(u) { visible.where("visibility IN ('public', 'identifiable') OR user_id = ?", u) }
10   scope :visible_to_all, -> { where(:visibility => %w(public identifiable)) }
11   scope :tagged, ->(t) { joins(:tags).where(:gpx_file_tags => { :tag => t }) }
12
13   validates :user, :presence => true, :associated => true
14   validates :name, :presence => true, :length => 1..255
15   validates :description, :presence => { :on => :create }, :length => 1..255
16   validates :timestamp, :presence => true
17   validates :visibility, :inclusion => %w(private public trackable identifiable)
18
19   def destroy
20     super
21     FileUtils.rm_f(trace_name)
22     FileUtils.rm_f(icon_picture_name)
23     FileUtils.rm_f(large_picture_name)
24   end
25
26   def tagstring
27     tags.collect(&:tag).join(", ")
28   end
29
30   def tagstring=(s)
31     if s.include? ","
32       self.tags = s.split(/\s*,\s*/).select { |tag| tag !~ /^\s*$/ }.collect {|tag|
33         tt = Tracetag.new
34         tt.tag = tag
35         tt
36       }
37     else
38       # do as before for backwards compatibility:
39       self.tags = s.split.collect {|tag|
40         tt = Tracetag.new
41         tt.tag = tag
42         tt
43       }
44     end
45   end
46
47   def public?
48     visibility == "public" || visibility == "identifiable"
49   end
50
51   def trackable?
52     visibility == "trackable" || visibility == "identifiable"
53   end
54
55   def identifiable?
56     visibility == "identifiable"
57   end
58
59   def large_picture=(data)
60     f = File.new(large_picture_name, "wb")
61     f.syswrite(data)
62     f.close
63   end
64
65   def icon_picture=(data)
66     f = File.new(icon_picture_name, "wb")
67     f.syswrite(data)
68     f.close
69   end
70
71   def large_picture
72     f = File.new(large_picture_name, "rb")
73     logger.info "large picture file: '#{f.path}', bytes: #{File.size(f.path)}"
74     data = f.sysread(File.size(f.path))
75     logger.info "have read data, bytes: '#{data.length}'"
76     f.close
77     data
78   end
79
80   def icon_picture
81     f = File.new(icon_picture_name, "rb")
82     logger.info "icon picture file: '#{f.path}'"
83     data = f.sysread(File.size(f.path))
84     f.close
85     data
86   end
87
88   def large_picture_name
89     "#{GPX_IMAGE_DIR}/#{id}.gif"
90   end
91
92   def icon_picture_name
93     "#{GPX_IMAGE_DIR}/#{id}_icon.gif"
94   end
95
96   def trace_name
97     "#{GPX_TRACE_DIR}/#{id}.gpx"
98   end
99
100   def mime_type
101     filetype = `/usr/bin/file -bz #{trace_name}`.chomp
102     gzipped = filetype =~ /gzip compressed/
103     bzipped = filetype =~ /bzip2 compressed/
104     zipped = filetype =~ /Zip archive/
105
106     if gzipped
107       mimetype = "application/x-gzip"
108     elsif bzipped
109       mimetype = "application/x-bzip2"
110     elsif zipped
111       mimetype = "application/x-zip"
112     else
113       mimetype = "application/gpx+xml"
114     end
115
116     mimetype
117   end
118
119   def extension_name
120     filetype = `/usr/bin/file -bz #{trace_name}`.chomp
121     gzipped = filetype =~ /gzip compressed/
122     bzipped = filetype =~ /bzip2 compressed/
123     zipped = filetype =~ /Zip archive/
124     tarred = filetype =~ /tar archive/
125
126     if tarred && gzipped
127       extension = ".tar.gz"
128     elsif tarred && bzipped
129       extension = ".tar.bz2"
130     elsif tarred
131       extension = ".tar"
132     elsif gzipped
133       extension = ".gpx.gz"
134     elsif bzipped
135       extension = ".gpx.bz2"
136     elsif zipped
137       extension = ".zip"
138     else
139       extension = ".gpx"
140     end
141
142     extension
143   end
144
145   def to_xml
146     doc = OSM::API.new.get_xml_doc
147     doc.root << to_xml_node
148     doc
149   end
150
151   def to_xml_node
152     el1 = XML::Node.new "gpx_file"
153     el1["id"] = id.to_s
154     el1["name"] = name.to_s
155     el1["lat"] = latitude.to_s if inserted
156     el1["lon"] = longitude.to_s if inserted
157     el1["user"] = user.display_name
158     el1["visibility"] = visibility
159     el1["pending"] = (!inserted).to_s
160     el1["timestamp"] = timestamp.xmlschema
161
162     el2 = XML::Node.new "description"
163     el2 << description
164     el1 << el2
165
166     tags.each do |tag|
167       el2 = XML::Node.new("tag")
168       el2 << tag.tag
169       el1 << el2
170     end
171
172     el1
173   end
174
175   # Read in xml as text and return it's Node object representation
176   def self.from_xml(xml, create = false)
177     p = XML::Parser.string(xml)
178     doc = p.parse
179
180     doc.find("//osm/gpx_file").each do |pt|
181       return Trace.from_xml_node(pt, create)
182     end
183
184     fail OSM::APIBadXMLError.new("trace", xml, "XML doesn't contain an osm/gpx_file element.")
185   rescue LibXML::XML::Error, ArgumentError => ex
186     raise OSM::APIBadXMLError.new("trace", xml, ex.message)
187   end
188
189   def self.from_xml_node(pt, create = false)
190     trace = Trace.new
191
192     fail OSM::APIBadXMLError.new("trace", pt, "visibility missing") if pt["visibility"].nil?
193     trace.visibility = pt["visibility"]
194
195     unless create
196       fail OSM::APIBadXMLError.new("trace", pt, "ID is required when updating.") if pt["id"].nil?
197       trace.id = pt["id"].to_i
198       # .to_i will return 0 if there is no number that can be parsed.
199       # We want to make sure that there is no id with zero anyway
200       fail OSM::APIBadUserInput.new("ID of trace cannot be zero when updating.") if trace.id == 0
201     end
202
203     # We don't care about the time, as it is explicitly set on create/update/delete
204     # We don't care about the visibility as it is implicit based on the action
205     # and set manually before the actual delete
206     trace.visible = true
207
208     description = pt.find("description").first
209     fail OSM::APIBadXMLError.new("trace", pt, "description missing") if description.nil?
210     trace.description = description.content
211
212     pt.find("tag").each do |tag|
213       trace.tags.build(:tag => tag.content)
214     end
215
216     trace
217   end
218
219   def xml_file
220     # TODO: *nix specific, could do to work on windows... would be functionally inferior though - check for '.gz'
221     filetype = `/usr/bin/file -bz #{trace_name}`.chomp
222     gzipped = filetype =~ /gzip compressed/
223     bzipped = filetype =~ /bzip2 compressed/
224     zipped = filetype =~ /Zip archive/
225     tarred = filetype =~ /tar archive/
226
227     if gzipped || bzipped || zipped || tarred
228       tmpfile = Tempfile.new("trace.#{id}")
229
230       if tarred && gzipped
231         system("tar -zxOf #{trace_name} > #{tmpfile.path}")
232       elsif tarred && bzipped
233         system("tar -jxOf #{trace_name} > #{tmpfile.path}")
234       elsif tarred
235         system("tar -xOf #{trace_name} > #{tmpfile.path}")
236       elsif gzipped
237         system("gunzip -c #{trace_name} > #{tmpfile.path}")
238       elsif bzipped
239         system("bunzip2 -c #{trace_name} > #{tmpfile.path}")
240       elsif zipped
241         system("unzip -p #{trace_name} -x '__MACOSX/*' > #{tmpfile.path}")
242       end
243
244       tmpfile.unlink
245
246       file = tmpfile.file
247     else
248       file = File.open(trace_name)
249     end
250
251     file
252   end
253
254   def import
255     logger.info("GPX Import importing #{name} (#{id}) from #{user.email}")
256
257     gpx = GPX::File.new(xml_file)
258
259     f_lat = 0
260     f_lon = 0
261     first = true
262
263     # If there are any existing points for this trace then delete
264     # them - we check for existing points first to avoid locking
265     # the table in the common case where there aren't any.
266     if Tracepoint.where(:gpx_id => id).exists?
267       Tracepoint.delete_all(:gpx_id => id)
268     end
269
270     gpx.points do |point|
271       if first
272         f_lat = point.latitude
273         f_lon = point.longitude
274         first = false
275       end
276
277       tp = Tracepoint.new
278       tp.lat = point.latitude
279       tp.lon = point.longitude
280       tp.altitude = point.altitude
281       tp.timestamp = point.timestamp
282       tp.gpx_id = id
283       tp.trackid = point.segment
284       tp.save!
285     end
286
287     if gpx.actual_points > 0
288       max_lat = Tracepoint.where(:gpx_id => id).maximum(:latitude)
289       min_lat = Tracepoint.where(:gpx_id => id).minimum(:latitude)
290       max_lon = Tracepoint.where(:gpx_id => id).maximum(:longitude)
291       min_lon = Tracepoint.where(:gpx_id => id).minimum(:longitude)
292
293       max_lat = max_lat.to_f / 10000000
294       min_lat = min_lat.to_f / 10000000
295       max_lon = max_lon.to_f / 10000000
296       min_lon = min_lon.to_f / 10000000
297
298       self.latitude = f_lat
299       self.longitude = f_lon
300       self.large_picture = gpx.picture(min_lat, min_lon, max_lat, max_lon, gpx.actual_points)
301       self.icon_picture = gpx.icon(min_lat, min_lon, max_lat, max_lon)
302       self.size = gpx.actual_points
303       self.inserted = true
304       self.save!
305     end
306
307     logger.info "done trace #{id}"
308
309     gpx
310   end
311 end