2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-04-09 04:37:35 +08:00
|
|
|
module HasUrl
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
class_methods do
|
|
|
|
def extract_url(url)
|
|
|
|
url.match(self::URL_REGEX)
|
|
|
|
end
|
|
|
|
|
2019-04-19 19:57:16 +08: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-09 04:37:35 +08:00
|
|
|
def get_from_url(url)
|
|
|
|
return if url.blank?
|
2019-04-09 04:55:26 +08:00
|
|
|
|
2019-04-09 04:37:35 +08:00
|
|
|
uri = begin
|
2019-12-12 10:49:21 +08:00
|
|
|
URI(UrlHelper.unencode(url))
|
2019-04-09 04:37:35 +08:00
|
|
|
rescue URI::Error
|
|
|
|
end
|
2019-04-09 04:55:26 +08:00
|
|
|
|
2019-04-09 04:37:35 +08:00
|
|
|
return if uri&.path.blank?
|
|
|
|
data = extract_url(uri.path)
|
2019-04-24 10:15:47 +08:00
|
|
|
|
|
|
|
if data.blank?
|
|
|
|
result = nil
|
|
|
|
result ||= self.find_by(url: uri.path)
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2019-04-09 09:47:11 +08:00
|
|
|
result = nil
|
2019-04-09 21:18:49 +08:00
|
|
|
|
|
|
|
if self.name == "Upload"
|
|
|
|
sha1 = data[2]
|
|
|
|
result = self.find_by(sha1: sha1) if sha1&.length == Upload::SHA1_LENGTH
|
|
|
|
end
|
|
|
|
|
2019-04-09 09:47:11 +08:00
|
|
|
result || self.find_by("url LIKE ?", "%#{data[1]}")
|
2019-04-09 04:37:35 +08:00
|
|
|
end
|
2020-10-13 21:17:06 +08:00
|
|
|
|
|
|
|
def get_from_urls(upload_urls)
|
|
|
|
urls = []
|
|
|
|
sha1s = []
|
|
|
|
|
|
|
|
upload_urls.each do |url|
|
|
|
|
next if url.blank?
|
|
|
|
|
|
|
|
uri = begin
|
|
|
|
URI(UrlHelper.unencode(url))
|
|
|
|
rescue URI::Error
|
|
|
|
end
|
|
|
|
|
|
|
|
next if uri&.path.blank?
|
|
|
|
urls << uri.path
|
|
|
|
|
|
|
|
if data = extract_url(uri.path).presence
|
|
|
|
urls << data[1]
|
|
|
|
sha1s << data[2] if self.name == "Upload"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
self.where(url: urls).or(self.where(sha1: sha1s))
|
|
|
|
end
|
2019-04-09 04:37:35 +08:00
|
|
|
end
|
|
|
|
end
|