mirror of
https://github.com/discourse/discourse.git
synced 2025-05-28 23:49:34 +08:00
custom avatar support
This commit is contained in:
@ -1,41 +1,31 @@
|
||||
class LocalStore
|
||||
|
||||
def store_upload(file, upload)
|
||||
unique_sha1 = Digest::SHA1.hexdigest("#{Time.now.to_s}#{file.original_filename}")[0,16]
|
||||
extension = File.extname(file.original_filename)
|
||||
clean_name = "#{unique_sha1}#{extension}"
|
||||
path = "#{relative_base_url}/#{upload.id}/#{clean_name}"
|
||||
# copy the file to the right location
|
||||
copy_file(file, "#{public_dir}#{path}")
|
||||
# url
|
||||
Discourse.base_uri + path
|
||||
path = get_path_for_upload(file, upload)
|
||||
store_file(file, path)
|
||||
end
|
||||
|
||||
def store_optimized_image(file, optimized_image)
|
||||
# 1234567890ABCDEF_100x200.jpg
|
||||
filename = [
|
||||
optimized_image.sha1[6..16],
|
||||
"_#{optimized_image.width}x#{optimized_image.height}",
|
||||
optimized_image.extension,
|
||||
].join
|
||||
# <rails>/public/uploads/site/_optimized/123/456/<filename>
|
||||
path = File.join(
|
||||
relative_base_url,
|
||||
"_optimized",
|
||||
optimized_image.sha1[0..2],
|
||||
optimized_image.sha1[3..5],
|
||||
filename
|
||||
)
|
||||
# copy the file to the right location
|
||||
copy_file(file, "#{public_dir}#{path}")
|
||||
# url
|
||||
Discourse.base_uri + path
|
||||
path = get_path_for_optimized_image(file, optimized_image)
|
||||
store_file(file, path)
|
||||
end
|
||||
|
||||
def remove_file(url)
|
||||
File.delete("#{public_dir}#{url}") if has_been_uploaded?(url)
|
||||
rescue Errno::ENOENT
|
||||
# don't care if the file isn't there
|
||||
def store_avatar(file, upload, size)
|
||||
path = get_path_for_avatar(file, upload, size)
|
||||
store_file(file, path)
|
||||
end
|
||||
|
||||
def remove_upload(upload)
|
||||
remove_file(upload.url)
|
||||
end
|
||||
|
||||
def remove_optimized_image(optimized_image)
|
||||
remove_file(optimized_image.url)
|
||||
end
|
||||
|
||||
def remove_avatars(upload)
|
||||
return unless upload.url =~ /avatars/
|
||||
remove_directory(File.dirname(upload.url))
|
||||
end
|
||||
|
||||
def has_been_uploaded?(url)
|
||||
@ -63,8 +53,63 @@ class LocalStore
|
||||
"#{public_dir}#{upload.url}"
|
||||
end
|
||||
|
||||
def absolute_avatar_template(upload)
|
||||
avatar_template(upload, absolute_base_url)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def get_path_for_upload(file, upload)
|
||||
unique_sha1 = Digest::SHA1.hexdigest("#{Time.now.to_s}#{file.original_filename}")[0..15]
|
||||
extension = File.extname(file.original_filename)
|
||||
clean_name = "#{unique_sha1}#{extension}"
|
||||
# path
|
||||
"#{relative_base_url}/#{upload.id}/#{clean_name}"
|
||||
end
|
||||
|
||||
def get_path_for_optimized_image(file, optimized_image)
|
||||
# 1234567890ABCDEF_100x200.jpg
|
||||
filename = [
|
||||
optimized_image.sha1[6..15],
|
||||
"_#{optimized_image.width}x#{optimized_image.height}",
|
||||
optimized_image.extension,
|
||||
].join
|
||||
# /uploads/<site>/_optimized/<1A3>/<B5C>/<filename>
|
||||
File.join(
|
||||
relative_base_url,
|
||||
"_optimized",
|
||||
optimized_image.sha1[0..2],
|
||||
optimized_image.sha1[3..5],
|
||||
filename
|
||||
)
|
||||
end
|
||||
|
||||
def get_path_for_avatar(file, upload, size)
|
||||
relative_avatar_template(upload).gsub(/\{size\}/, size.to_s)
|
||||
end
|
||||
|
||||
def relative_avatar_template(upload)
|
||||
avatar_template(upload, relative_base_url)
|
||||
end
|
||||
|
||||
def avatar_template(upload, base_url)
|
||||
File.join(
|
||||
base_url,
|
||||
"avatars",
|
||||
upload.sha1[0..2],
|
||||
upload.sha1[3..5],
|
||||
upload.sha1[6..15],
|
||||
"{size}#{upload.extension}"
|
||||
)
|
||||
end
|
||||
|
||||
def store_file(file, path)
|
||||
# copy the file to the right location
|
||||
copy_file(file, "#{public_dir}#{path}")
|
||||
# url
|
||||
Discourse.base_uri + path
|
||||
end
|
||||
|
||||
def copy_file(file, path)
|
||||
FileUtils.mkdir_p Pathname.new(path).dirname
|
||||
# move the file to the right location
|
||||
@ -74,6 +119,17 @@ class LocalStore
|
||||
end
|
||||
end
|
||||
|
||||
def remove_file(url)
|
||||
File.delete("#{public_dir}#{url}") if has_been_uploaded?(url)
|
||||
rescue Errno::ENOENT
|
||||
# don't care if the file isn't there
|
||||
end
|
||||
|
||||
def remove_directory(path)
|
||||
directory = "#{public_dir}/#{path}"
|
||||
FileUtils.rm_rf(directory)
|
||||
end
|
||||
|
||||
def is_relative?(url)
|
||||
url.start_with?(relative_base_url)
|
||||
end
|
||||
|
@ -4,34 +4,60 @@ require 'open-uri'
|
||||
class S3Store
|
||||
|
||||
def store_upload(file, upload)
|
||||
extension = File.extname(file.original_filename)
|
||||
remote_filename = "#{upload.id}#{upload.sha1}#{extension}"
|
||||
# <id><sha1><extension>
|
||||
path = "#{upload.id}#{upload.sha1}#{upload.extension}"
|
||||
|
||||
# if this fails, it will throw an exception
|
||||
upload(file.tempfile, remote_filename, file.content_type)
|
||||
upload(file.tempfile, path, file.content_type)
|
||||
|
||||
# returns the url of the uploaded file
|
||||
"#{absolute_base_url}/#{remote_filename}"
|
||||
"#{absolute_base_url}/#{path}"
|
||||
end
|
||||
|
||||
def store_optimized_image(file, optimized_image)
|
||||
extension = File.extname(file.path)
|
||||
remote_filename = [
|
||||
# <id><sha1>_<width>x<height><extension>
|
||||
path = [
|
||||
optimized_image.id,
|
||||
optimized_image.sha1,
|
||||
"_#{optimized_image.width}x#{optimized_image.height}",
|
||||
extension
|
||||
optimized_image.extension
|
||||
].join
|
||||
|
||||
# if this fails, it will throw an exception
|
||||
upload(file, remote_filename)
|
||||
upload(file, path)
|
||||
|
||||
# returns the url of the uploaded file
|
||||
"#{absolute_base_url}/#{remote_filename}"
|
||||
"#{absolute_base_url}/#{path}"
|
||||
end
|
||||
|
||||
def store_avatar(file, upload, size)
|
||||
# /avatars/<sha1>/200.jpg
|
||||
path = File.join(
|
||||
"avatars",
|
||||
upload.sha1,
|
||||
"#{size}#{upload.extension}"
|
||||
)
|
||||
|
||||
# if this fails, it will throw an exception
|
||||
upload(file, path)
|
||||
|
||||
# returns the url of the avatar
|
||||
"#{absolute_base_url}/#{path}"
|
||||
end
|
||||
|
||||
def remove_upload(upload)
|
||||
remove_file(upload.url)
|
||||
end
|
||||
|
||||
def remove_optimized_image(optimized_image)
|
||||
remove_file(optimized_image.url)
|
||||
end
|
||||
|
||||
def remove_avatars(upload)
|
||||
|
||||
end
|
||||
|
||||
def remove_file(url)
|
||||
check_missing_site_settings
|
||||
return unless has_been_uploaded?(url)
|
||||
name = File.basename(url)
|
||||
remove(name)
|
||||
|
Reference in New Issue
Block a user