alright, im still stuck, erm, this is my code...
First, this is my action mailer, which read and create file from the
email, it will auto generates the file after reading the email and save
it somewhere
require 'ActionMailer'
class AssetSubmitHandler < ActionMailer::Base
# content type should be validated to image/gif, image/jpg, or
image/jpeg
def receive(email)
if email.has_attachments?
email.attachments.each do |attachment|
# save original file
File.open("C:/rubydev/prac1/mail6/log/" +
base_part_of(attachment.original_filename),File::CREAT|File::TRUNC|File::WRONLY,0666){
f>
f.write(attachment.read)
f.close()
}
end
end
end
def base_part_of(file_name)
name = File.basename(file_name)
name.gsub(/[^W._-]/, '')
sanitize_filename(name)
end
# Fixes a 'feature' of IE where it passes the entire path instead of
just the filename
def sanitize_filename(value)
#get only the filename (not the whole path)
just_filename = value.gsub(/^.*(\\|\/)/, '')
just_filename.gsub(/[^\w\.\-]/,'_')
end
end
then, at my map class, i need to read the file and process the lmx file
and save to database.
require 'ActiveRecord'
class Map
require 'rexml/document'
require "mysql"
require "dbi"
include REXML
require 'AssetSubmitHandler'
#~ scanfile = File.new("C:/rubydev/prac1/mail6/log/River_Va.lmx")
#~ dbname="email_development"
#~ doc = REXML::Document.new scanfile
a = File.open("C:/rubydev/prac1/mail6/log/new_file_name.lmx" )
puts a
doc = Document.new File.new("C:/rubydev/prac1/mail6/log/River_Va.lmx")
#~ doc.elements.each("lm:landmarkCollection") { |element| puts
element.attributes["lm:latitude"]
#~ latitude = element.attributes["lm:latitude"]
#~ }
#~ root = doc.root
#~ puts
root.elements["lm:lmx/lm:landmarkCollection/lm:landmark/lm:coordinates"].attributes["lm:latitude"]
names =
invisibility = XPath.first( doc, "//lm:landmark" )
XPath.each( doc, "//lm:name") { |element| puts names << element.text}
XPath.match( doc, "//lm:name" )
puts names
latitude =
invisibility = XPath.first( doc, "/*/lm:landmark" )
XPath.each( doc, "//lm:latitude") { |element| puts latitude <<
element.text}
XPath.match( doc, "//lm:latitude" )
puts latitude
longitude =
invisibility = XPath.first( doc, "/*/lm:landmark" )
XPath.each( doc, "//lm:longitude") { |element| puts longitude <<
element.text}
XPath.match( doc, "//lm:longitude" )
puts longitude
# db insert
m = Mysql.new("localhost", "root", "", "email_development")
sth=m.query("insert into maps (name,latitude,longtitude) values
('#{names}','#{latitude}','#{longitude}')")
end
Now, the problem i faced is, how am i going to read the file was
generated from action mailer, by using file.new, file.basename, or file
open?
or is there a better way to get the file generated from action mailer?
thx for helping me again~~
cheers~~