File: online_file.rb

package info (click to toggle)
mkvtoolnix 92.0-1
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 58,620 kB
  • sloc: cpp: 216,810; ruby: 11,403; xml: 8,058; ansic: 6,885; sh: 4,884; python: 1,041; perl: 191; makefile: 113; awk: 16; javascript: 4
file content (40 lines) | stat: -rw-r--r-- 903 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
module Mtx::OnlineFile
  @@to_unlink = []
  @@mutex     = Mutex.new

  def self.download url, file_name = nil
    @@mutex.synchronize do
      FileUtils.mkdir_p "tmp"

      file_name ||= url.gsub(%r{.*/}, '')
      file_name   = "tmp/#{file_name}"

      if !FileTest.exist?(file_name)
        @@to_unlink << file_name

        runq "wget", url, "wget --quiet -O #{file_name} #{url}"
      end

      if %r{\.zip$}.match(file_name)
        require "zip"
        Zip::File.open(file_name, :create => false) do |zip_file|
          return zip_file.entries.reject { |entry| %r{/$}.match entry.name }.first.get_input_stream.read
        end
      end

      return IO.read(file_name)
    end
  end

  def self.cleanup
    return if c?(:KEEP_DOWNLOADED_FILES)

    @@to_unlink.
      select { |fn| FileTest.exist? fn }.
      each   { |fn| File.unlink      fn }
  end
end

END {
  Mtx::OnlineFile.cleanup
}