mirror of
https://github.com/discourse/discourse.git
synced 2024-11-30 10:24:49 +08:00
edbc356593
The following methods have long been deprecated in ruby due to flaws in their implementation per http://blade.nagaokaut.ac.jp/cgi-bin/vframe.rb/ruby/ruby-core/29293?29179-31097: URI.escape URI.unescape URI.encode URI.unencode escape/encode are just aliases for one another. This PR uses the Addressable gem to replace these methods with its own encode, unencode, and encode_component methods where appropriate. I have put all references to Addressable::URI here into the UrlHelper to keep them corralled in one place to make changes to this implementation easier. Addressable is now also an explicit gem dependency.
49 lines
922 B
Ruby
49 lines
922 B
Ruby
# frozen_string_literal: true
|
|
|
|
module HasUrl
|
|
extend ActiveSupport::Concern
|
|
|
|
class_methods do
|
|
def extract_url(url)
|
|
url.match(self::URL_REGEX)
|
|
end
|
|
|
|
def extract_sha1(path)
|
|
data = extract_url(path)
|
|
return if data.blank?
|
|
|
|
sha1 = data[2]
|
|
return if sha1&.length != Upload::SHA1_LENGTH
|
|
|
|
sha1
|
|
end
|
|
|
|
def get_from_url(url)
|
|
return if url.blank?
|
|
|
|
uri = begin
|
|
URI(UrlHelper.unencode(url))
|
|
rescue URI::Error
|
|
end
|
|
|
|
return if uri&.path.blank?
|
|
data = extract_url(uri.path)
|
|
|
|
if data.blank?
|
|
result = nil
|
|
result ||= self.find_by(url: uri.path)
|
|
return result
|
|
end
|
|
|
|
result = nil
|
|
|
|
if self.name == "Upload"
|
|
sha1 = data[2]
|
|
result = self.find_by(sha1: sha1) if sha1&.length == Upload::SHA1_LENGTH
|
|
end
|
|
|
|
result || self.find_by("url LIKE ?", "%#{data[1]}")
|
|
end
|
|
end
|
|
end
|