2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-04-08 16:37:35 -04:00
|
|
|
module HasUrl
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
class_methods do
|
|
|
|
def extract_url(url)
|
|
|
|
url.match(self::URL_REGEX)
|
|
|
|
end
|
|
|
|
|
2019-04-19 07:57:16 -04:00
|
|
|
def extract_sha1(path)
|
|
|
|
data = extract_url(path)
|
|
|
|
return if data.blank?
|
|
|
|
|
|
|
|
sha1 = data[2]
|
|
|
|
return if sha1&.length != Upload::SHA1_LENGTH
|
|
|
|
|
|
|
|
sha1
|
|
|
|
end
|
|
|
|
|
2019-04-08 16:37:35 -04:00
|
|
|
def get_from_url(url)
|
|
|
|
return if url.blank?
|
2019-04-08 16:55:26 -04:00
|
|
|
|
2019-04-08 16:37:35 -04:00
|
|
|
uri = begin
|
|
|
|
URI(URI.unescape(url))
|
|
|
|
rescue URI::Error
|
|
|
|
end
|
2019-04-08 16:55:26 -04:00
|
|
|
|
2019-04-08 16:37:35 -04:00
|
|
|
return if uri&.path.blank?
|
|
|
|
data = extract_url(uri.path)
|
2019-04-23 22:15:47 -04:00
|
|
|
|
|
|
|
if data.blank?
|
|
|
|
result = nil
|
|
|
|
result ||= self.find_by(url: uri.path)
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2019-04-08 21:47:11 -04:00
|
|
|
result = nil
|
2019-04-09 09:18:49 -04:00
|
|
|
|
|
|
|
if self.name == "Upload"
|
|
|
|
sha1 = data[2]
|
|
|
|
result = self.find_by(sha1: sha1) if sha1&.length == Upload::SHA1_LENGTH
|
|
|
|
end
|
|
|
|
|
2019-04-08 21:47:11 -04:00
|
|
|
result || self.find_by("url LIKE ?", "%#{data[1]}")
|
2019-04-08 16:37:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|