def handle_zip(origin_name, user_id)
if self.image_url=~ /\.zip$/i
begin
folder = origin_name.gsub('.zip', '') if origin_name.present?
dirname = "#{Rails.root}/public/upload_files/a03/html5/#{self.id}"
userid_name = "#{Rails.root}/public/upload_files/temp_zip/#{user_id}"
replaced_url = "http://a03.optimix.asia/html5/#{self.id}"
FileUtils.mkdir_p(dirname) unless File.directory?(dirname)
%x(rm -rf #{dirname}/*)
%x(cp -r #{userid_name}/* #{dirname})
%x(chown xmo:xmo -R #{dirname}) if Rails.env.production?
unless File.exist?("#{userid_name}/index.html")
folder = Dir.entries("#{userid_name}").select{|dir| ![".",".."].include?(dir)}.last.to_s
File.rename("#{dirname}/#{folder}","#{dirname}/html5_assets")
folder = "html5_assets"
end
%x(rm -rf #{userid_name})
read_list_path = File.directory?("#{dirname}/#{folder}") ? "#{dirname}/#{folder}" : dirname
replaced_url = File.directory?("#{dirname}/#{folder}") ? "#{replaced_url}/#{folder}" : replaced_url
ab_index_url = File.directory?("#{dirname}/#{folder}") ? "#{dirname}/#{folder}/index.html" : "#{dirname}/index.html"
sign = read_list_path =~ /#{self.id.to_s}$/ ? self.id.to_s : folder
all_urls = get_file_list(read_list_path, self.id.to_s, folder, sign)
replace_url = all_urls.select{|url| url.downcase =~ /\.(gif|jpg|jpeg|png|js|css)$/i}
all_js_path = replace_url.select{|url| url.downcase =~ /\.js$/i}
replace_js_files(dirname,folder,all_js_path,replace_url,replaced_url)
replace_html_file(ab_index_url, replace_url, replaced_url)
rescue
return {"status" => "invalid" , "message" => 'please reupload zip'}
end
end
end
def get_urls_from_js(js,directory)
reg = /\"([^\"\']+\.(png|gif|jpg|jpeg|js|css))\"|\'([^\'\"]+\.(png|gif|jpg|jpeg|js|css))\'/
urls = js.scan(reg).map{|b| b[0].present? ? b[0] : b[2]}.uniq
urls.present? ? sort_url(urls.select{|url| directory.any?{|dir| dir.include?(url.gsub(/\.(\.|)\//,''))}}) : []
end
def replace_js_files(dirname,folder,all_js_path,replace_url,replaced_url)
all_js_path.each do |path|
js_pathname = File.directory?("#{dirname}/#{folder}") ? "#{dirname}/#{folder}/#{path}" : "#{dirname}/#{path}"
file = File.open(js_pathname)
js = file.read
js_urls_from_file = get_urls_from_js(js.gsub(/\s+/,''), replace_url)
js_urls_from_file.each {|url|
path1 = path.split('/')
(url.scan(/\.\.\//).count + 1).times {path1.pop}
root_url = path1.push(url.gsub(/\.(\.|)\//,'').gsub('.', '00replace00')).join('/')
js = js.gsub(url,"#{replaced_url}/#{root_url}")
}
File.open(js_pathname, "w") { |fw| fw.write(js.gsub('00replace00', '.'))} if js_urls_from_file.present?
end
end
def replace_html_file(ab_index_url, replace_url, replaced_url)
file = File.open(ab_index_url)
html = file.read
replace_url.each{|r| html = html.gsub("./#{r}","#{replaced_url}/#{r.gsub('.','00replace00')}").gsub(r,"#{replaced_url}/#{r}").gsub('00replace00','.')}
File.open(ab_index_url, "w") { |fw| fw.write(html) }
self.update_attributes(:h5_code => html)
end
def sort_url(urls)
urls1 = urls.select{|url| url.match(/\.(\.|)\//)}
sort_has_point(urls1) + (urls - urls1).sort{|x,y| y.scan(/\//).count <=> x.scan(/\//).count}
end
def sort_has_point(urls)
urls.sort{|x,y|
if x.scan(/\.(\.|)\//).count > 0 && x.scan(/\.(\.|)\//).count == y.scan(/\.(\.|)\//).count
x.scan(/\.\.\//).count > y.scan(/\.\.\//).count ? -1 : 1
else
y.scan(/\.(\.|)\//).count <=> x.scan(/\.(\.|)\//).count
end
}
end
def verification_zip(origin_name,user_id)
message = []
begin
folder = origin_name.gsub('.zip', '') if origin_name.present?
pathname = "#{Rails.root}/public/upload_files/a03/#{self.image_url.gsub(/http:\/\/a03.optimix.asia\//, '')}"
userid_name = "#{Rails.root}/public/upload_files/temp_zip/#{user_id}"
FileUtils.mkdir_p(userid_name) unless File.directory?(userid_name)
return {"status" => "invalid" , "message" => I18n.t('pretargetings.validate.html5_too_large') } if File.size("#{pathname}").to_i/1024 > ZIP_MAX_SIZE
%x(rm -rf #{userid_name}/*)
%x(unzip -o -d #{userid_name} #{pathname})
%x(rm -rf #{userid_name}/__MACOSX) if File.directory?("#{userid_name}/__MACOSX")
directory = Dir.entries("#{userid_name}").select{|dir| ![".",".."].include?(dir)}
index_html = directory.select{|dir| dir =~ /\.html$/ }
if index_html.present?
File.rename("#{userid_name}/#{index_html.last}", "#{userid_name}/index.html")
else
folder = directory.last
dire = Dir.entries("#{userid_name}/#{folder}").select{|dir| dir =~ /\.html$/}
File.rename("#{userid_name}/#{folder}/#{dire.last}", "#{userid_name}/#{folder}/index.html")
end
read_list_path = File.directory?("#{userid_name}/#{folder}") ? "#{userid_name}/#{folder}" : userid_name
ab_index_url = File.directory?("#{userid_name}/#{folder}") ? "#{userid_name}/#{folder}/index.html" : "#{userid_name}/index.html"
ve = verification_html(ab_index_url)
message << ve["message"] if ve.is_a?(Hash) && ve["status"] == "invalid"
sign = read_list_path =~ /#{user_id.to_s}$/ ? user_id.to_s : folder
all_urls = get_file_list(read_list_path, user_id.to_s, folder, sign)
ve = verification_file_type(all_urls,ab_index_url)
message << ve["message"] if ve.is_a?(Hash) && ve["status"] == "invalid"
ve = verification_head_size(ab_index_url,self.size)
message << ve["message"] if ve.is_a?(Hash) && ve["status"] == "invalid"
message.present? ? {"status" => "invalid" , "message" => message.join('</p>')} : true
rescue
return {"status" => "invalid" , "message" => 'please reupload zip'}
end
end
def get_file_list(path, id, folder=nil,sign="0")
b = []
Dir.entries(path).each do |sub|
if sub != '.' && sub != '..'
if File.directory?("#{path}/#{sub}")
b = b + get_file_list("#{path}/#{sub}", id, folder, sign)
end
b << (path + '/' + sub).split(sign + '/')[1] unless File.directory?("#{path}/#{sub}")
end
end
b
end
def verification_html(path)
File.open(path) do |fr|
xml = fr.read
match_data = xml.gsub(/\s+/,'').match(/<metaname=(\"|\')ad\.size(\"|\')content=(\"|\')width=\d+,height=\d+(\"|\')(\/|)>/i)
unless match_data.present? && xml =~ /\$clickurl\$/i && xml =~ /\<\!DOCTYPE html\>/i && xml =~ /\<html/i && xml =~ /\<body/i && xml =~ /\<\/body\>/i && xml =~ /\<\/html\>/i
{"status" => "invalid" , "message" => I18n.t('pretargetings.validate.please_upload_effective_material') }
end
end
end
def verification_file_type(array,path)
if array.any?{|item| item.downcase =~ /\.(bmp|pcx|tiff|tga|exif|fpx|svg|psd|cdr|pcd|dxf|ufo|eps|ai|hdri|raw)$/}
{"status" => "invalid" , "message" => I18n.t('pretargetings.validate.image_Format_error') }
end
if array.any?{|item| item.downcase =~ /\.json$/}
{"status" => "invalid" , "message" => I18n.t('pretargetings.validate.please_upload_effective_material') }
end
end
def verification_head_size(path,size)
selected_size = size.split('*')
File.open(path) do |fr|
xml=fr.read.gsub(/\s+/,'')
match_data = xml.match(/<metaname=(\"|\')ad\.size(\"|\')content=(\"|\')width=\d+,height=\d+(\"|\')(\/|)>/i)
{"status" => "invalid" , "message" => I18n.t('pretargetings.validate.size_Format_error') } if match_data.present? && match_data.to_s.scan(/\d+/) != selected_size
end
end
下一步读取数据库的index.html代码显示。
http://www.schooltop.net/blogs/179
分享到:
相关推荐
路劲
pso和ga混合算法用来解决路劲规划问题
传入指定文件或者文件夹路劲数组进行压缩,支持混合的文件夹或者文件。速度快,不需要额外引入包
首先,确定文件的绝对路径。 ```java import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Properties; public class PropertiesReader { public static ...
内容简介:上海路劲佘山院子住宅建筑研发方案( PDF+171页)。图纸包含:项目概况、示范区范围、建筑部分、室内部分、景观设计、设计理念、解决策略、项目区位、方案思路、案例研究、设计寓意、形体生成、概念剖面、...
蚁群算法(Ant Colony Algorithm) 蚁群算法是一种 metaheuristics 算法,仿真蚂蚁觅食的过程,以解决组合优化问题。该算法的主要思想是,蚂蚁在觅食时,会留下化学物质(pheromone),以引导其他蚂蚁follow 该路径...
自己写的一个关于最短路劲的小程序,有图像,很直观
3. 相对路径的使用可以减少因为应用程序部署到不同环境时需要修改文件路径的麻烦。 接下来,我们将具体分析几种常见的使用相对路径读取文件的方法: 方法一:向上查找目录法 在应用程序的bin\debug目录下生成的....
源码文件“粒子群最短路_粒子群算法路劲规划_availableaof_粒子群最短路_源码.rar”可能包含了实现上述算法的详细代码,包括粒子的初始化、速度和位置更新的逻辑、路径成本计算、边界处理以及迭代终止条件的判断。...
3. 资源分配不合理:可能将过多资源投入到建设,而忽视了营销和推广的重要性。 【解决策略】 1. 创新促销手段:结合项目特点,推出个性化、差异化的促销活动,如定制装修方案、合作金融机构提供低利率贷款等。 2. ...
描述中的"源点到节点的权值与路劲走线对比选择最短路劲"暗示了我们正在寻找的是从特定源节点出发,经过一系列节点,到达目标节点的最短路径,这与Dijkstra算法的适用场景非常吻合。 在提供的压缩文件中,"zui duan....
stata 1.2 stata文件路劲02.mp4
通和路劲商鼎路项目提案.pptx
在"粒子群最短路_粒子群算法路劲规划_availableaof_粒子群最短路_源码.rar"文件中,包含了PSO算法应用于道路网络最短路径规划的具体代码实现。这些源码通常包括数据结构设计(如图的表示、粒子的状态等)、算法流程...
检测一个url路劲是否有效,数据库中保存的url有的是错误的,有的可能过一段就ping不通,所以适时检测URL很有必要
当处理一个节点时,需要将其可能的子状态(即其所有未尝试的邻居节点)加入到待处理队列中,这个过程就是分支。 - **边界**:边界是指当前搜索空间中的最佳解。每次处理节点时,都会更新这个边界值,以反映当前找到...
7. **合作与伙伴关系**:如果通和路劲商鼎路项目涉及到与其他企业或机构的合作,提案会明确合作伙伴的角色、合作方式以及合作的优势。 8. **结论与建议**:在提案的最后,通常会总结项目的整体优势,提出实施项目的...
农田污染综合防治技术路劲和风险管控.doc
临床路劲实施情况统计分析和改进措施总结.doc
很抱歉,由于提供的内容仅为页面编号,并未包含具体的知识点信息,无法直接生成关于"通和路劲商鼎路项目提案学习教案.pptx"的详细说明。通常,这样的PPTX文件可能涵盖项目背景、目标设定、市场分析、财务预算、风险...