Class: Pedophile::BigFiles

Inherits:
Object
  • Object
show all
Defined in:
lib/pedophile/big_files.rb

Constant Summary collapse

USE_MIME =
false
TMP_STRUCTURE_PATH =
File.absolute_path(File.join(Wget::TMP_PATH, "big_files.yaml"))

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(downloader) ⇒ BigFiles

Returns a new instance of BigFiles.



9
10
11
12
# File 'lib/pedophile/big_files.rb', line 9

def initialize(downloader)
  @downloader = downloader
  @files = Array.new
end

Instance Attribute Details

#downloaderObject (readonly)

Returns the value of attribute downloader.



14
15
16
# File 'lib/pedophile/big_files.rb', line 14

def downloader
  @downloader
end

#filesObject (readonly)

Returns the value of attribute files.



14
15
16
# File 'lib/pedophile/big_files.rb', line 14

def files
  @files
end

#files_pathObject (readonly)

Returns the value of attribute files_path.



14
15
16
# File 'lib/pedophile/big_files.rb', line 14

def files_path
  @files_path
end

#full_pathObject (readonly)

Returns the value of attribute full_path.



14
15
16
# File 'lib/pedophile/big_files.rb', line 14

def full_path
  @full_path
end

Instance Method Details

#analyzeObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/pedophile/big_files.rb', line 32

def analyze
  glob_path = "#{full_path}/**/**"
  puts "big files path #{full_path.to_s.cyan}"

  Dir.glob(glob_path) do |item|
    next if item == '.' or item == '..' or File.directory?(item)

    puts "analyze file #{item.to_s.yellow}"

    h = Hash.new
    h[:path] = item

    if USE_MIME
      mime = `file --mime #{item}`
      if mime =~ /(\w+\/\w+);/
        mime = $1
      else
        mime = nil
      end
      h[:mime] = mime
    end

    @files << h
  end

  save_analyzed
end

#big_files_path=(path) ⇒ Object



27
28
29
30
# File 'lib/pedophile/big_files.rb', line 27

def big_files_path=(path)
  @files_path = path
  @full_path = File.join(offline_path, path)
end

#copy_folder(path) ⇒ Object



20
21
22
23
24
25
# File 'lib/pedophile/big_files.rb', line 20

def copy_folder(path)
  puts "copying big files path #{path.to_s.cyan}"
  FileUtils.cp_r(path, offline_path)
  puts "done copying path #{path.to_s.cyan}"
  big_files_path = path
end

#gsub_big_file(smaller_path) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/pedophile/big_files.rb', line 81

def gsub_big_file(smaller_path)
  puts "process big file #{smaller_path.to_s.green}"

  self.downloader.offline_tree.files.each do |f|
    if f[:inside]
      to_rename = f[:inside].select do |fi|
        fi[:path].index(smaller_path)
      end

      # TODO gsub path issue with html files inside
      to_rename.each do |fi|
        original_string = fi[:path]
        new_string = File.join(files_path, smaller_path)

        puts "rename big file #{original_string.to_s.blue} to #{new_string.to_s.green}"

        self.downloader.offline_tree.process_massive_gsub(original_string, new_string, true)
      end


    end
  end

end


70
71
72
73
74
75
76
77
78
79
# File 'lib/pedophile/big_files.rb', line 70

def gsub_links
  files.each do |f|
    file_path = f[:path].clone
    smaller_path = file_path.gsub(full_path, "")
    smaller_path.gsub!(/^\//, '')

    gsub_big_file(smaller_path)
    f[:done] = true
  end
end

#load_analyzedObject



66
67
68
# File 'lib/pedophile/big_files.rb', line 66

def load_analyzed
  @files = YAML.load_file(TMP_STRUCTURE_PATH)
end

#offline_pathObject



16
17
18
# File 'lib/pedophile/big_files.rb', line 16

def offline_path
  self.downloader.wget.offline_path
end

#save_analyzedObject



60
61
62
63
64
# File 'lib/pedophile/big_files.rb', line 60

def save_analyzed
  f = File.new(TMP_STRUCTURE_PATH, "w")
  f.puts @files.to_yaml
  f.close
end