Modifications for images in html code
This commit is contained in:
1 parent
0ca354cfd0
commit
2e86d8081f
10 files changed
+110
-118
No files matched your search
@@ -16,6 +16,7 @@ class Design
|
||||
embeds_many :themes
|
||||
embeds_many :javascripts
|
||||
embeds_many :images
|
||||
embeds_many :custom_images, as: :design_image
|
||||
|
||||
validates_presence_of :title
|
||||
validates_presence_of :author
|
||||
@@ -75,7 +76,6 @@ class Design
|
||||
if self.layout.to_save
|
||||
self.layout.to_save = false
|
||||
self.layout.save
|
||||
self.layout.parse_urls
|
||||
end
|
||||
[[self.default_css], [self.reset_css], self.themes].each do |objects|
|
||||
objects.each do |object|
|
||||
@@ -91,11 +91,12 @@ class Design
|
||||
def process_object(object)
|
||||
if object.to_save
|
||||
if object.file_filename.nil?
|
||||
new_object = self.send(object._type.downcase.pluralize).build
|
||||
new_object.file = object.file
|
||||
new_object = self.send(object._type.downcase.pluralize).build(object)
|
||||
object.destroy
|
||||
new_object.to_save = false
|
||||
new_object.save
|
||||
else
|
||||
object.to_save = false
|
||||
object.save
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
class Image < DesignFile
|
||||
embedded_in :design
|
||||
|
||||
field :in_css, :type => Boolean
|
||||
field :name
|
||||
field :html_id
|
||||
field :html_class
|
||||
|
||||
embedded_in :design_image, polymorphic: true
|
||||
|
||||
mount_uploader :file, ImageUploader
|
||||
end
|
||||
@@ -1,8 +1,6 @@
|
||||
class Layout < DesignFile
|
||||
include Parser
|
||||
|
||||
mount_uploader :file_orig, AssetUploader
|
||||
|
||||
attr_reader :content
|
||||
|
||||
embeds_one :menu
|
||||
@@ -23,38 +21,4 @@ class Layout < DesignFile
|
||||
parse_layout_contents(self)
|
||||
end
|
||||
|
||||
def parse_urls
|
||||
orig_content = content = self.content
|
||||
self.remove_file!
|
||||
self.remove_file_orig!
|
||||
names = []
|
||||
images = self.design.images
|
||||
html_name = nil
|
||||
content.scan(/(?<=\<img)(.*?)(?=\/\>)/){
|
||||
$1.gsub(' ','').scan(/(?<=src=\")(.*?)(?=\")/){
|
||||
html_name = $1
|
||||
}
|
||||
name = File.basename(html_name).gsub(/[\\\"]/, '')
|
||||
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]) rescue nil
|
||||
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, :after, :parse_layout) do
|
||||
self.save
|
||||
end
|
||||
}
|
||||
end
|
||||
end
|
||||
@@ -6,6 +6,8 @@ class Page < Item
|
||||
belongs_to :design
|
||||
has_many :page_parts
|
||||
|
||||
embeds_many :custom_images, as: :design_image
|
||||
|
||||
def create_parts
|
||||
page_design = self.design
|
||||
page_design.layout.layout_parts.each do |p|
|
||||
|
||||
@@ -6,7 +6,7 @@ class AssetUploader < CarrierWave::Uploader::Base
|
||||
process :set_content_type
|
||||
|
||||
def set_content_type(*args)
|
||||
content_type = file.content_type == 'binary/octet-stream' || 'application/octet-stream' || file.content_type.blank? ? MIME::Types.type_for(original_filename).first.to_s : file.content_type
|
||||
content_type = file.content_type == ('binary/octet-stream' || 'application/octet-stream') || file.content_type.blank? ? MIME::Types.type_for(original_filename).first.to_s : file.content_type
|
||||
self.file.instance_variable_set(:@content_type, content_type)
|
||||
end
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ class ImageUploader < CarrierWave::Uploader::Base
|
||||
include CarrierWave::MiniMagick
|
||||
|
||||
def set_content_type(*args)
|
||||
content_type = file.content_type == 'binary/octet-stream' || 'application/octet-stream' || file.content_type.blank? ? MIME::Types.type_for(original_filename).first.to_s : file.content_type
|
||||
content_type = file.content_type == ('binary/octet-stream' || 'application/octet-stream') || file.content_type.blank? ? MIME::Types.type_for(original_filename).first.to_s : file.content_type
|
||||
self.file.instance_variable_set(:@content_type, content_type)
|
||||
end
|
||||
|
||||
@@ -58,14 +58,14 @@ class ImageUploader < CarrierWave::Uploader::Base
|
||||
# "something.jpg" if original_filename
|
||||
# end
|
||||
|
||||
# def manipulate!
|
||||
# raise current_path.inspect
|
||||
# image = ::MiniMagick::Image.open(current_path)
|
||||
# image = yield(image)
|
||||
# image.write(current_path)
|
||||
# ::MiniMagick::Image.open(current_path)
|
||||
# rescue ::MiniMagick::Error, ::MiniMagick::Invalid => e
|
||||
# raise CarrierWave::ProcessingError.new("Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: #{e}")
|
||||
# end
|
||||
|
||||
def manipulate!
|
||||
cache_stored_file! if !cached?
|
||||
image = ::MiniMagick::Image.open(current_path)
|
||||
image = yield(image)
|
||||
image.write(current_path)
|
||||
::MiniMagick::Image.open(current_path)
|
||||
rescue
|
||||
nil
|
||||
end
|
||||
|
||||
end
|
||||
Reference in new issue
Block a user