• the progressbar and Tempfile


    https://github.com/peleteiro/progressbar

    gem install progressbar

    gem 'progressbar', '~> 0.21.0'

    % irb --simple-prompt -r progressbar
    >> pbar = ProgressBar.new("test", 100)
    => (ProgressBar: 0/100)
    >> 100.times {sleep(0.1); pbar.inc}; pbar.finish
    test:          100% |oooooooooooooooooooooooooooooooooooooooo| Time: 00:00:10
    => nil
    
    >> pbar = ProgressBar.new("test", 100)
    => (ProgressBar: 0/100)
    >> (1..100).each{|x| sleep(0.1); pbar.set(x)}; pbar.finish
    test:           67% |oooooooooooooooooooooooooo              | ETA:  00:00:03
    
    >> ProgressBar.new("test", 100) do |pbar|
    >>   100.times { sleep(0.1); pbar.inc }
    >> end
    test:          100% |oooooooooooooooooooooooooooooooooooooooo| Time: 00:00:10

    def download_apk!
      require 'open-uri'
      require 'digest'
      require 'fileutils'

      return false if is_dead_download_url?
      return false if is_apk_downloaded?

      tmp_file = Tempfile.new [package_name, ".apk"]
      File.open(tmp_file, 'wb') do |file_to_save|
        pbar = nil

        open(download_url, "rb", :content_length_proc => lambda {|t|
          if t && 0 < t

            pbar = ProgressBar.new("...", t)
         pbar.file_transfer_mode
          end
        },
        :progress_proc => lambda {|s|
          pbar.set s if pbar
        }) { |file_to_download| file_to_save.write(file_to_download.read) }  
      end

      apk_md5 = Digest::MD5.file(tmp_file).hexdigest
      apk_path = File.join Settings.apks_dir, "#{package_name}.#{apk_md5}.apk"

      FileUtils.mkdir_p Settings.apks_dir
      FileUtils.mv tmp_file, apk_path

      package_info = dump_package_info apk_path
      update_attributes package_info.merge(md5: apk_md5)
      apk_path
    rescue OpenURI::HTTPError, OpenURI::HTTPRedirect
      false
    end

     

    http://www.ruby-doc.org/stdlib-1.9.3/libdoc/tempfile/rdoc/Tempfile.html

    Tempfile

    默认存储路径是 /tmp
    A utility class for managing temporary files. When you create a Tempfile object, it will create a temporary file with a unique filename. A Tempfile objects behaves just like a File object, and you can perform all the usual file operations on it: reading data, writing data, changing its permissions, etc. So although this class does not explicitly document all instance methods supported by File, you can in fact call any File instance method on a Tempfile object.


    require 'tempfile'
    
    file = Tempfile.new('foo')
    file.path      # => A unique filename in the OS's temp directory,
                   #    e.g.: "/tmp/foo.24722.0"
                   #    This filename contains 'foo' in its basename.
    file.write("hello world")
    file.rewind
    file.read      # => "hello world"
    file.close
    file.unlink    # deletes the temp file





  • 相关阅读:
    Python基础教程【读书笔记】
    Python基础教程【读书笔记】
    Python基础教程【读书笔记】
    Python基础教程【读书笔记】
    JS实现焦点图轮播效果
    JQuery+CSS3实现Ajax加载时loading效果
    JQuery实现锚点平滑滚动
    CSS3之嵌入Web字体
    HTML5本地存储
    impress.js初体验——前端装X利器
  • 原文地址:https://www.cnblogs.com/iwangzheng/p/3795259.html
Copyright © 2020-2023  润新知