]> git.openstreetmap.org Git - rails.git/blob - app/models/trace.rb
Fix race condition adding traces. Closes #1201.
[rails.git] / app / models / trace.rb
1 class Trace < ActiveRecord::Base
2   set_table_name 'gpx_files'
3
4   validates_presence_of :user_id, :name, :timestamp
5   validates_presence_of :description, :on => :create
6 #  validates_numericality_of :latitude, :longitude
7   validates_inclusion_of :public, :inserted, :in => [ true, false]
8   
9   belongs_to :user
10   has_many :tags, :class_name => 'Tracetag', :foreign_key => 'gpx_id', :dependent => :delete_all
11   has_many :points, :class_name => 'Tracepoint', :foreign_key => 'gpx_id', :dependent => :delete_all
12
13   def destroy
14     super
15     FileUtils.rm_f(trace_name)
16     FileUtils.rm_f(icon_picture_name)
17     FileUtils.rm_f(large_picture_name)
18   end
19
20   def tagstring
21     return tags.collect {|tt| tt.tag}.join(" ")
22   end
23
24   def tagstring=(s)
25     self.tags = s.split().collect {|tag|
26       tt = Tracetag.new
27       tt.tag = tag
28       tt
29     }
30   end
31   
32   def large_picture= (data)
33     f = File.new(large_picture_name, "wb")
34     f.syswrite(data)
35     f.close
36   end
37   
38   def icon_picture= (data)
39     f = File.new(icon_picture_name, "wb")
40     f.syswrite(data)
41     f.close
42   end
43
44   def large_picture
45     f = File.new(large_picture_name, "rb")
46     logger.info "large picture file: '#{f.path}', bytes: #{File.size(f.path)}"
47     data = f.sysread(File.size(f.path))
48     logger.info "have read data, bytes: '#{data.length}'"
49     f.close
50     data
51   end
52   
53   def icon_picture
54     f = File.new(icon_picture_name, "rb")
55     logger.info "icon picture file: '#{f.path}'"
56     data = f.sysread(File.size(f.path))
57     f.close
58     data
59   end
60   
61   def large_picture_name
62     "#{GPX_IMAGE_DIR}/#{id}.gif"
63   end
64
65   def icon_picture_name
66     "#{GPX_IMAGE_DIR}/#{id}_icon.gif"
67   end
68
69   def trace_name
70     "#{GPX_TRACE_DIR}/#{id}.gpx"
71   end
72
73   def mime_type
74     filetype = `/usr/bin/file -bz #{trace_name}`.chomp
75     gzipped = filetype =~ /gzip compressed/
76     bzipped = filetype =~ /bzip2 compressed/
77     zipped = filetype =~ /Zip archive/
78
79     if gzipped then
80       mimetype = "application/x-gzip"
81     elsif bzipped then
82       mimetype = "application/x-bzip2"
83     elsif zipped
84       mimetype = "application/x-zip"
85     else
86       mimetype = "text/xml"
87     end
88
89     return mimetype
90   end
91
92   def extension_name
93     filetype = `/usr/bin/file -bz #{trace_name}`.chomp
94     gzipped = filetype =~ /gzip compressed/
95     bzipped = filetype =~ /bzip2 compressed/
96     zipped = filetype =~ /Zip archive/
97     tarred = filetype =~ /tar archive/
98
99     if tarred and gzipped then
100       extension = ".tar.gz"
101     elsif tarred and bzipped then
102       extension = ".tar.bz2"
103     elsif tarred
104       extension = ".tar"
105     elsif gzipped
106       extension = ".gpx.gz"
107     elsif bzipped
108       extension = ".gpx.bz2"
109     elsif zipped
110       extension = ".zip"
111     else
112       extension = ".gpx"
113     end
114
115     return extension
116   end
117
118   def to_xml
119     doc = OSM::API.new.get_xml_doc
120     doc.root << to_xml_node()
121     return doc
122   end
123
124   def to_xml_node
125     el1 = XML::Node.new 'gpx_file'
126     el1['id'] = self.id.to_s
127     el1['name'] = self.name.to_s
128     el1['lat'] = self.latitude.to_s
129     el1['lon'] = self.longitude.to_s
130     el1['user'] = self.user.display_name
131     el1['public'] = self.public.to_s
132     el1['pending'] = (!self.inserted).to_s
133     el1['timestamp'] = self.timestamp.xmlschema
134     return el1
135   end
136
137   def xml_file
138     # TODO *nix specific, could do to work on windows... would be functionally inferior though - check for '.gz'
139     filetype = `/usr/bin/file -bz #{trace_name}`.chomp
140     gzipped = filetype =~ /gzip compressed/
141     bzipped = filetype =~ /bzip2 compressed/
142     zipped = filetype =~ /Zip archive/
143     tarred = filetype =~ /tar archive/
144
145     if gzipped or bzipped or zipped or tarred then
146       tmpfile = Tempfile.new("trace.#{id}");
147
148       if tarred and gzipped then
149         system("tar -zxOf #{trace_name} > #{tmpfile.path}")
150       elsif tarred and bzipped then
151         system("tar -jxOf #{trace_name} > #{tmpfile.path}")
152       elsif tarred
153         system("tar -xOf #{trace_name} > #{tmpfile.path}")
154       elsif gzipped
155         system("gunzip -c #{trace_name} > #{tmpfile.path}")
156       elsif bzipped
157         system("bunzip2 -c #{trace_name} > #{tmpfile.path}")
158       elsif zipped
159         system("unzip -p #{trace_name} -x '__MACOSX/*' > #{tmpfile.path}")
160       end
161
162       tmpfile.unlink
163
164       file = tmpfile.file
165     else
166       file = File.open(trace_name)
167     end
168
169     return file
170   end
171
172   def import
173     logger.info("GPX Import importing #{name} (#{id}) from #{user.email}")
174
175     gpx = GPX::File.new(self.xml_file)
176
177     f_lat = 0
178     f_lon = 0
179     first = true
180
181     # If there are any existing points for this trace then delete
182     # them - we check for existing points first to avoid locking
183     # the table in the common case where there aren't any.
184     if Tracepoint.find(:first, :conditions => ['gpx_id = ?', self.id])
185       Tracepoint.delete_all(['gpx_id = ?', self.id])
186     end
187
188     gpx.points do |point|
189       if first
190         f_lat = point.latitude
191         f_lon = point.longitude
192         first = false
193       end
194
195       tp = Tracepoint.new
196       tp.lat = point.latitude
197       tp.lon = point.longitude
198       tp.altitude = point.altitude
199       tp.timestamp = point.timestamp
200       tp.gpx_id = id
201       tp.trackid = point.segment
202       tp.save!
203     end
204
205     if gpx.actual_points > 0
206       max_lat = Tracepoint.maximum('latitude', :conditions => ['gpx_id = ?', id])
207       min_lat = Tracepoint.minimum('latitude', :conditions => ['gpx_id = ?', id])
208       max_lon = Tracepoint.maximum('longitude', :conditions => ['gpx_id = ?', id])
209       min_lon = Tracepoint.minimum('longitude', :conditions => ['gpx_id = ?', id])
210
211       max_lat = max_lat.to_f / 10000000
212       min_lat = min_lat.to_f / 10000000
213       max_lon = max_lon.to_f / 10000000
214       min_lon = min_lon.to_f / 10000000
215
216       self.latitude = f_lat
217       self.longitude = f_lon
218       self.large_picture = gpx.picture(min_lat, min_lon, max_lat, max_lon, gpx.actual_points)
219       self.icon_picture = gpx.icon(min_lat, min_lon, max_lat, max_lon)
220       self.size = gpx.actual_points
221       self.inserted = true
222       self.save!
223     end
224
225     logger.info "done trace #{id}"
226
227     return gpx
228   end
229 end