mirror of
https://github.com/discourse/discourse.git
synced 2024-11-26 19:00:32 -06:00
a8baa6d9e7
This is for backwards compatibility purposes. Even if `Upload#url` has a format that we don't recognize, we should still return the upload object as long as the upload record is present.
47 lines
885 B
Ruby
47 lines
885 B
Ruby
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(URI.unescape(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
|