Sha256: 5343f4d1cf23c5c55f32f8bfd09c4300aacfb01a7b42d32ab42c0a6280e22b79

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

=begin
  filemanager_helper.rb
  Copyright (C) 2008  Leon Li

  You may redistribute it and/or modify it under the same
  license terms as Ruby.
=end

module Fm
  module FilemanagerHelper

    def get_file_type(file)
      type = File.extname(file)
      
      unless type.blank?
        type = type.downcase[1..-1]
        return type if FM_SUPPORT_TYPES.include?(type)
      end
      FM_UNKNOWN_TYPE
    end
    
    def get_file_content(path, to=FM_ENCODING_TO, from=FM_ENCODING_FROM)
      if File.new(path).size > 1000000
        'File too big'
      else
        str = File.read(path)
        to.nil? ? str : Iconv.conv(to, from, str)
      end
    end
    
    def transfer(from, to, target)
      if FM_ENCODING_TO.nil?
        target
      else
        if target.is_a?(Array)
          target.map{|i| to.nil? ? i : Iconv.conv(to, from, i)}
        else
          Iconv.conv(to, from, target)
        end
      end
    end
  
    def encode(target)
      transfer(FM_ENCODING_FROM, FM_ENCODING_TO, target);
    end
    
    def decode(target)
      transfer(FM_ENCODING_TO, FM_ENCODING_FROM, target);
    end
    
    def hsize(size)
      size = size/1024
      if size > 1024
        size = size/1024
        size = format('%0.2f',(size)) + ' mb'
      else
        size = format('%0.2f', size) + ' kb'
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
filemanager-0.1.0 rails/app/helpers/fm/filemanager_helper.rb
filemanager-0.1.1 rails/app/helpers/fm/filemanager_helper.rb