socialforge/app/helpers/avatar_helper.rb

81 lines
2.0 KiB
Ruby
Raw Normal View History

2013-08-01 10:33:49 +08:00
module AvatarHelper
AVATAR_SIZE="50x50"
2016-01-29 22:09:16 +08:00
def copy_avatar(des, src)
src_file = disk_filename(src.class,src.id)
des_file = disk_filename(des.class,des.id)
FileUtils.cp(src_file, des_file) if File.exist?(src_file)
end
2013-08-01 10:33:49 +08:00
2016-01-29 22:09:16 +08:00
def avatar_image(source, copyed=false)
source_type = source.class
source_id = source.id
course = Course.find(source_id) rescue nil
if course && copyed
source_id = course.is_copy
end
File.join(relative_path, avatar_directory(source_type), source_id.to_s)
2013-08-01 10:33:49 +08:00
end
def relative_path
"avatars" #File.join("images","avatars")
end
def storage_path
File.join(Rails.root, "public", "images", relative_path)
end
def avatar_directory(source_type)
"#{source_type}"
end
def avatar_filename(source_id,image_file)
"#{source_id}" #<< file_extension(image_file)
end
def disk_filename(source_type,source_id,image_file=nil)
2016-01-29 22:09:16 +08:00
File.join(storage_path,avatar_directory(source_type),avatar_filename(source_id,image_file))
end
def copy_course?(source_type, source_id)
file= disk_filename(source_type, source_id)
if source_type == Course && !File.exist?(file)
course = Course.find(source_id) rescue nil
if course && course.is_copy>0
return true
end
end
false
2013-08-01 10:33:49 +08:00
end
def file_extension(filename=nil)
$1 if filename =~ %r{(\.[a-zA-Z0-9]+)$}
end
def get_avatar(source)
if source.nil?
return File.join(relative_path,'AnonymousUser','0')
end
if source.class && source.id && File.exist?(disk_filename(source.class,source.id))
2016-01-29 22:09:16 +08:00
avatar_image(source, false)
elsif copy_course?(source.class, source.id)
2016-02-04 17:30:49 +08:00
get_avatar(Course.find(source.is_copy)) rescue nil
2013-08-01 10:33:49 +08:00
else
File.join(relative_path,avatar_directory(source.class),'0')
end
2013-08-01 10:33:49 +08:00
end
def get_avatar?(source)
if source && File.exist?(disk_filename(source.class,source.id))
2013-08-01 10:33:49 +08:00
return true
else
return false
end
end
end