2015-06-01 05:13:56 -04:00
|
|
|
require_dependency 'file_store/base_store'
|
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 LocalStore < BaseStore
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2015-05-29 12:39:47 -04:00
|
|
|
def store_file(file, path)
|
|
|
|
copy_file(file, "#{public_dir}#{path}")
|
|
|
|
"#{Discourse.base_uri}#{path}"
|
2013-11-05 13:04:47 -05:00
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2015-05-29 12:39:47 -04:00
|
|
|
def remove_file(url)
|
|
|
|
return unless is_relative?(url)
|
|
|
|
path = public_dir + url
|
2016-05-22 22:25:25 -04:00
|
|
|
return if !File.exists?(path)
|
2016-05-30 03:46:27 -04:00
|
|
|
tombstone = public_dir + url.sub("/uploads/", "/uploads/tombstone/")
|
|
|
|
FileUtils.mkdir_p(tombstone_dir)
|
|
|
|
FileUtils.move(path, tombstone, force: true)
|
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 has_been_uploaded?(url)
|
2015-06-01 11:49:58 -04:00
|
|
|
return false if url.blank?
|
|
|
|
return true if is_relative?(url)
|
|
|
|
return true if is_local?(url)
|
|
|
|
false
|
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 absolute_base_url
|
|
|
|
"#{Discourse.base_url_no_prefix}#{relative_base_url}"
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2015-06-01 11:49:58 -04:00
|
|
|
def absolute_base_cdn_url
|
|
|
|
"#{Discourse.asset_host}#{relative_base_url}"
|
|
|
|
end
|
|
|
|
|
2016-03-09 22:47:18 -05:00
|
|
|
def upload_path
|
|
|
|
"/uploads/#{RailsMultisite::ConnectionManagement.current_db}"
|
|
|
|
end
|
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def relative_base_url
|
2016-03-09 22:47:18 -05:00
|
|
|
"#{Discourse.base_uri}#{upload_path}"
|
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 external?
|
2015-05-29 12:39:47 -04:00
|
|
|
false
|
2013-11-05 13:04:47 -05:00
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2015-05-29 12:39:47 -04:00
|
|
|
def download_url(upload)
|
|
|
|
return unless upload
|
|
|
|
"#{relative_base_url}/#{upload.sha1}"
|
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 path_for(upload)
|
2015-06-01 11:49:58 -04:00
|
|
|
url = upload.try(:url)
|
|
|
|
"#{public_dir}#{upload.url}" if url && url[0] == "/" && url[1] != "/"
|
2013-11-05 13:04:47 -05:00
|
|
|
end
|
2013-08-13 16:08:29 -04:00
|
|
|
|
2013-11-27 16:01:41 -05:00
|
|
|
def purge_tombstone(grace_period)
|
|
|
|
`find #{tombstone_dir} -mtime +#{grace_period} -type f -delete`
|
|
|
|
end
|
|
|
|
|
2015-05-27 19:03:24 -04:00
|
|
|
def get_path_for(type, upload_id, sha, extension)
|
2016-03-09 22:47:18 -05:00
|
|
|
"#{upload_path}/#{super(type, upload_id, sha, extension)}"
|
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 copy_file(file, path)
|
|
|
|
FileUtils.mkdir_p(Pathname.new(path).dirname)
|
|
|
|
# move the file to the right location
|
2013-11-27 16:01:41 -05:00
|
|
|
# not using mv, cause permissions are no good on move
|
|
|
|
File.open(path, "wb") { |f| f.write(file.read) }
|
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 is_relative?(url)
|
2014-07-18 11:54:18 -04:00
|
|
|
url.present? && url.start_with?(relative_base_url)
|
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 is_local?(url)
|
2014-07-18 11:54:18 -04:00
|
|
|
return false if url.blank?
|
2013-12-16 05:44:59 -05:00
|
|
|
absolute_url = url.start_with?("//") ? SiteSetting.scheme + ":" + url : url
|
2013-11-05 13:04:47 -05:00
|
|
|
absolute_url.start_with?(absolute_base_url) || absolute_url.start_with?(absolute_base_cdn_url)
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def public_dir
|
|
|
|
"#{Rails.root}/public"
|
|
|
|
end
|
2013-07-31 17:26:34 -04:00
|
|
|
|
2013-11-27 16:01:41 -05:00
|
|
|
def tombstone_dir
|
2016-05-30 03:46:27 -04:00
|
|
|
public_dir + relative_base_url.sub("/uploads/", "/uploads/tombstone/")
|
2013-11-27 16:01:41 -05:00
|
|
|
end
|
|
|
|
|
2013-07-31 17:26:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|