2013-11-05 13:04:47 -05:00
|
|
|
require 'file_store/base_store'
|
2014-09-24 16:52:09 -04:00
|
|
|
require_dependency "s3_helper"
|
2014-04-15 07:04:14 -04:00
|
|
|
require_dependency "file_helper"
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
module FileStore
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
class S3Store < BaseStore
|
2014-09-24 16:52:09 -04:00
|
|
|
|
|
|
|
def initialize(s3_helper = nil)
|
|
|
|
@s3_helper = s3_helper || S3Helper.new(s3_bucket, tombstone_prefix)
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2014-04-15 07:04:14 -04:00
|
|
|
def store_upload(file, upload, content_type = nil)
|
2013-11-05 13:04:47 -05:00
|
|
|
path = get_path_for_upload(file, upload)
|
2014-04-15 07:04:14 -04:00
|
|
|
store_file(file, path, upload.original_filename, content_type)
|
2013-11-05 13:04:47 -05:00
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def store_optimized_image(file, optimized_image)
|
|
|
|
path = get_path_for_optimized_image(file, optimized_image)
|
|
|
|
store_file(file, path)
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def remove_upload(upload)
|
|
|
|
remove_file(upload.url)
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def remove_optimized_image(optimized_image)
|
|
|
|
remove_file(optimized_image.url)
|
|
|
|
end
|
2013-08-13 16:08:29 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def has_been_uploaded?(url)
|
2014-07-18 11:54:18 -04:00
|
|
|
url.present? && url.start_with?(absolute_base_url)
|
2013-11-05 13:04:47 -05:00
|
|
|
end
|
2013-08-13 16:08:29 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def absolute_base_url
|
|
|
|
"//#{s3_bucket}.s3.amazonaws.com"
|
|
|
|
end
|
2013-08-13 16:08:29 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def external?
|
|
|
|
true
|
|
|
|
end
|
2013-08-13 16:08:29 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def internal?
|
|
|
|
!external?
|
|
|
|
end
|
2013-08-13 16:08:29 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def download(upload)
|
2014-09-24 16:52:09 -04:00
|
|
|
return unless has_been_uploaded?(upload.url)
|
2013-12-16 05:44:59 -05:00
|
|
|
url = SiteSetting.scheme + ":" + upload.url
|
2014-04-15 07:04:14 -04:00
|
|
|
max_file_size = [SiteSetting.max_image_size_kb, SiteSetting.max_attachment_size_kb].max.kilobytes
|
2014-09-24 16:52:09 -04:00
|
|
|
FileHelper.download(url, max_file_size, "discourse-s3", true)
|
2013-11-05 13:04:47 -05:00
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2014-01-07 11:45:06 -05:00
|
|
|
def avatar_template(avatar)
|
|
|
|
template = relative_avatar_template(avatar)
|
|
|
|
"#{absolute_base_url}/#{template}"
|
2013-11-05 13:04:47 -05:00
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-27 16:01:41 -05:00
|
|
|
def purge_tombstone(grace_period)
|
2014-09-24 16:52:09 -04:00
|
|
|
@s3_helper.update_tombstone_lifecycle(grace_period)
|
2013-11-27 16:01:41 -05:00
|
|
|
end
|
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
private
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2014-09-24 16:52:09 -04:00
|
|
|
def get_path_for_upload(file, upload)
|
|
|
|
"#{upload.id}#{upload.sha1}#{upload.extension}"
|
2014-07-05 15:33:39 -04:00
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2014-09-24 16:52:09 -04:00
|
|
|
def get_path_for_optimized_image(file, optimized_image)
|
|
|
|
"#{optimized_image.id}#{optimized_image.sha1}_#{optimized_image.width}x#{optimized_image.height}#{optimized_image.extension}"
|
2014-07-05 15:33:39 -04:00
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2014-09-24 16:52:09 -04:00
|
|
|
def get_path_for_avatar(file, avatar, size)
|
|
|
|
relative_avatar_template(avatar).gsub("{size}", size.to_s)
|
2014-09-09 12:43:02 -04:00
|
|
|
end
|
|
|
|
|
2014-09-24 16:52:09 -04:00
|
|
|
def relative_avatar_template(avatar)
|
|
|
|
"avatars/#{avatar.sha1}/{size}#{avatar.extension}"
|
|
|
|
end
|
2013-11-05 13:04:47 -05:00
|
|
|
|
2014-09-24 16:52:09 -04:00
|
|
|
def store_file(file, path, filename=nil, content_type=nil)
|
|
|
|
# stored uploaded are public by default
|
2015-05-25 03:57:06 -04:00
|
|
|
options = { acl: 'public-read' }
|
2014-09-24 16:52:09 -04:00
|
|
|
# add a "content disposition" header for "attachments"
|
|
|
|
options[:content_disposition] = "attachment; filename=\"#{filename}\"" if filename && !FileHelper.is_image?(filename)
|
|
|
|
# add a "content type" header when provided (ie. for "attachments")
|
|
|
|
options[:content_type] = content_type if content_type
|
|
|
|
# if this fails, it will throw an exception
|
|
|
|
@s3_helper.upload(file, path, options)
|
|
|
|
# return the upload url
|
|
|
|
"#{absolute_base_url}/#{path}"
|
|
|
|
end
|
2013-11-27 16:01:41 -05:00
|
|
|
|
2014-09-24 16:52:09 -04:00
|
|
|
def remove_file(url)
|
|
|
|
return unless has_been_uploaded?(url)
|
|
|
|
filename = File.basename(url)
|
|
|
|
# copy the removed file to tombstone
|
|
|
|
@s3_helper.remove(filename, true)
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2014-09-24 16:52:09 -04:00
|
|
|
def s3_bucket
|
|
|
|
return @s3_bucket if @s3_bucket
|
|
|
|
raise Discourse::SiteSettingMissing.new("s3_upload_bucket") if SiteSetting.s3_upload_bucket.blank?
|
|
|
|
@s3_bucket = SiteSetting.s3_upload_bucket.downcase
|
2013-12-16 05:44:59 -05:00
|
|
|
end
|
|
|
|
|
2014-09-24 16:52:09 -04:00
|
|
|
def tombstone_prefix
|
|
|
|
"tombstone/"
|
|
|
|
end
|
2013-12-16 05:44:59 -05:00
|
|
|
|
2013-07-31 17:26:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|