Skip to content

Commit

Permalink
Refactoring Downloading
Browse files Browse the repository at this point in the history
  • Loading branch information
songchenwen committed Apr 25, 2015
1 parent a67742c commit 3183838
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
1 change: 1 addition & 0 deletions lib/maid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

# Must be in this order:
require 'maid/version'
require 'maid/downloading'
require 'maid/tools'
require 'maid/rule_container'
require 'maid/maid'
Expand Down
19 changes: 19 additions & 0 deletions lib/maid/downloading.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Maid::Downloading
class << self
def downloading?(path)
!!(downloading_file_regexps.any? { |re| path.match(re) } || firefox_extra?(path) || aria2_extra?(path))
end

def downloading_file_regexps
[/\.crdownload$/, /\.download$/, /\.aria2$/, /\.td$/, /\.td.cfg$/, /\.part$/]
end

def firefox_extra?(path)
File.exist?("#{path}.part")
end

def aria2_extra?(path)
File.exist?("#{path}.aria2")
end
end
end
24 changes: 2 additions & 22 deletions lib/maid/tools.rb
Original file line number Diff line number Diff line change
Expand Up @@ -358,11 +358,12 @@ def downloaded_from(path)
mdls_to_array(path, 'kMDItemWhereFroms')
end

include Maid::Downloading
# Detect whether the path is currently being downloaded in Chrome, Firefox or Safari.
#
# See also: `dir_safe`
def downloading?(path)
!!(chrome_downloading?(path) || firefox_downloading?(path) || safari_downloading?(path) || aria2_downloading?(path) || thunder_downloading?(path))
Maid::Downloading.downloading?(path)
end

# Find all duplicate files in the given globs.
Expand Down Expand Up @@ -940,27 +941,6 @@ def added_at(path)

private

def firefox_downloading?(path)
path =~ /\.part$/ ||
File.exist?("#{path}.part")
end

def chrome_downloading?(path)
path =~ /\.crdownload$/
end

def safari_downloading?(path)
path =~ /\.download$/
end

def aria2_downloading?(path)
File.exist?("#{path}.aria2") || path =~ /\.aria2$/
end

def thunder_downloading?(path)
path =~ /\.td$/ || path =~ /\.td.cfg$/
end

def has_tag_available?()
return Maid::Platform.osx? && system("which -s tag")
end
Expand Down
3 changes: 2 additions & 1 deletion lib/maid/watch.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'listen'
class Maid::Watch
include Maid::RuleContainer
include Maid::Downloading

attr_reader :path, :listener, :logger

Expand All @@ -9,7 +10,7 @@ def initialize(maid, path, options = {}, &rules)
if options.nil?
@lazy = true
@options = { wait_for_delay: 10,
ignore: [/\.crdownload$/, /\.download$/, /\.aria2$/, /\.td$/, /\.td.cfg$/, /\.part$/] }
ignore: Maid::Downloading.downloading_file_regexps }
else
@lazy = options.delete(:lazy) { |key| true }
@options = options
Expand Down

0 comments on commit 3183838

Please sign in to comment.