Modifications for image urls

Find and replace the urls by the file urls in DB, in the theme files and
layout.html file. Save a copy of those files under "file_orig".
This commit is contained in:
chris2tof 2011-09-01 19:08:45 +08:00
parent f78400c274
commit 9101a2921e
5 changed files with 100 additions and 57 deletions

View File

@ -4,34 +4,6 @@ class GridfsController < ActionController::Metal
def serve
gridfs_path = env["PATH_INFO"].gsub("/gridfs/", "")
gridfs_file = Mongo::GridFileSystem.new(Mongoid.database).open(gridfs_path, 'r') rescue nil
if gridfs_file
self.response_body = gridfs_file.read
self.content_type = gridfs_file.content_type
else
serve_rescue(gridfs_path.split('/').pop)
end
end
def serve_images
gridfs_path = env["PATH_INFO"]
serve_rescue(gridfs_path.split('/').pop)
end
def serve_rescue(file_name)
referer = request.env['HTTP_REFERER']
if referer.include?('admin')
page = Page.find(referer.split('/').pop)
else
vars = request.env['HTTP_REFERER'].split('/')
names = get_names(vars, [])
page = Page.find_by_name(names.pop)
names.reverse_each do |name|
page = page.children.find_by_name(name)
end
end
image = page.design.images.detect{|i| i.file_filename == file_name}
gridfs_path = image.file.url.gsub("/gridfs/", "")
begin
gridfs_file = Mongo::GridFileSystem.new(Mongoid.database).open(gridfs_path, 'r')
self.response_body = gridfs_file.read
@ -43,14 +15,4 @@ class GridfsController < ActionController::Metal
end
end
def get_names(vars, names)
page = Page.all(:conditions => {:name => vars.last})
names << vars.pop
if page.count > 1
get_names(vars, names)
else
names
end
end
end

View File

@ -60,6 +60,7 @@ class Design
end
protected
def del_embedded_objects
[[self.layout],self.themes, self.javascripts, self.images].each do |objects|
objects.each do |object|
@ -67,20 +68,32 @@ class Design
end
end
end
def procs_embedded_objects
[[self.layout],self.themes, self.javascripts, self.images].each do |objects|
[[self.layout], self.javascripts, self.images].each do |objects|
objects.each do |object|
if object.file.blank?
object.to_save = false
end
if object.to_save
object.to_save = false
object.save
end
if object.to_destroy
object.destroy
end
process_object(object)
end
end
self.layout.parse_urls
self.themes.each do |theme|
to_parse_url = theme.to_save ? true : false
process_object(theme)
theme.parse_urls if to_parse_url
end
end
def process_object(object)
if object.file.blank?
object.to_save = false
end
if object.to_save
object.to_save = false
object.save
end
if object.to_destroy
object.destroy
end
end
end

View File

@ -1,11 +1,16 @@
class Layout < DesignFile
include Parser
embeds_one :menu
embedded_in :design
mount_uploader :file_orig, AssetUploader
attr_reader :content
embeds_one :menu
embedded_in :design
embeds_many :layout_parts
after_save :parse_layout
def content
self.file.read
end
@ -14,9 +19,39 @@ class Layout < DesignFile
Layout.count > 0
end
after_save:parse_layout
def parse_layout
parse_layout_layout_part(self)
end
def parse_urls
orig_content = content = self.file.read.force_encoding("UTF-8")
self.remove_file!
self.remove_file_orig!
names = []
images = self.design.images
content.scan(/(?<=\<img src=)(.*?)(?=[ \t]|\/\>)/){
html_name = $1
name = File.basename(html_name).gsub(/[\\\"]/, '').strip
file_name = images.detect{ |i| i.file_filename.eql?(name) }.file_url rescue nil
names << [html_name, file_name]
}
names.each do |name|
content.gsub!(name[0], name[1])
end
Dir.mktmpdir('f_path') { |dir|
orig_file_name = self.file_filename
temp_file = File.new(dir + '/' + orig_file_name, 'w+')
temp_file.write orig_content.force_encoding("UTF-8")
self.file_orig = temp_file
temp_file = File.new(dir + '/' + orig_file_name, 'w+')
temp_file.write content.force_encoding("UTF-8")
self.file = temp_file
Layout.without_callback(:save) do
self.save
end
}
end
end

View File

@ -1,9 +1,40 @@
class Theme < Stylesheet
embedded_in :design
field :name
mount_uploader :file_orig, AssetUploader
before_save :set_name
def parse_urls
orig_content = content = self.file.read
self.remove_file!
self.remove_file_orig!
names = []
images = self.design.images
content.scan(/(?<=url\()(.*?)(?=\))/){
css_name = $1
name = File.basename(css_name).gsub(/[\\\"]/, '')
file_name = images.detect{ |i| i.file_filename.eql?(name) }.file_url rescue nil
names << [css_name, file_name]
}
names.each do |name|
content.gsub!(name[0], name[1])
end
Dir.mktmpdir('f_path') { |dir|
orig_file_name = self.file_filename
temp_file = File.new(dir + '/' + orig_file_name, 'w+')
temp_file.write orig_content.force_encoding("UTF-8")
self.file_orig = temp_file
temp_file = File.new(dir + '/' + orig_file_name, 'w+')
temp_file.write content.force_encoding("UTF-8")
self.file = temp_file
self.save
}
end
protected
def set_name

View File

@ -12,8 +12,10 @@ PrototypeR4::Application.routes.draw do
namespace :admin do
resources :assets
resources :designs do
get 'upload_package' ,:on => :collection
post 'upload_package' ,:on => :collection
collection do
get 'upload_package'
post 'upload_package'
end
member do
post 'edit_file' => 'designs#edit_file'
end
@ -50,8 +52,8 @@ PrototypeR4::Application.routes.draw do
# routes for gridfs files
match "/gridfs/*path" => "gridfs#serve"
match "/images/*path" => "gridfs#serve_images"
match "/admin/pages/images/*path" => "gridfs#serve_images"
# match "/images/*path" => "gridfs#serve_images"
# match "/admin/pages/images/*path" => "gridfs#serve_images"
# routes for pages to generate
match '*page_name' => 'pages#show', :as => :page, :constraints => lambda{|request|