Sha256: 72ddfd5e4e18fae14a2584fdbb7819f7467fd30a648b5fd8aff32ca36f13ed4d

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

module Tehportal
  module Attachment
    module_function

    def file_list(path)
      file_list = []
      if path.present? and File.directory?(path)
        file_list = Dir(path).content.select {|f| File.file?(f)}.map {|f| File.basename(f)}
      end
      file_list
    end

    def path_for(request_params, user)
      if request_params[:commentable]
        subject, id = request_params[:commentable]/'/'
      else
        subject = request_params[:controller]
        if request_params[:action] == 'create'
          id = 'NEW'
        else
          id = request_params[:id]
        end
      end
      if subject
        "#{Rails.root}/tmp/#{subject}/#{id}/#{user.login}"
      end
    end

    def file_list_for(request_params, user)
      path = path_for(request_params, user)
      Attachment.file_list path
    end

    def collect_files_at(path)
      files = {dir: path}

      if File.directory?(path)
        file_list = Attachment.file_list(path)
        file_list[0..2].each_with_index do |file_name, index|
          i = index+1
          if content = read("#{path}/#{file_name}")
            files.merge! :"name_#{i}" => file_name,
              :"data_#{i}" => Base64.encode64(content),
              :"size_#{i}" => content.size
          end
        end
      end

      files
    end

    def collect_files_for(request_params, user)
      collect_files_at path_for(request_params, user)
    end

    def clear(params, user)
      path_tmp = path_for(params, user)
      FileUtils.rm_rf(path_tmp) if File.directory?(path_tmp)
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tehportal-0.1.3 lib/tehportal/attachment.rb
tehportal-0.1.0 lib/tehportal/attachment.rb