Class: AlFile

Inherits:
AlWidget show all
Defined in:
lib/al_form.rb,
lib/al_form/input_file.rb

Overview

ファイルウィジェット

Constant Summary collapse

@@dirname =

Returns テンポラリファイルを作成する場所(ディレクトリ).

Returns:

  • (String)

    テンポラリファイルを作成する場所(ディレクトリ)

AL_TEMPDIR

Instance Attribute Summary

Attributes inherited from AlWidget

#filter, #foreign, #hidden, #label, #message, #name, #required, #tag_attr, #tag_type, #value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AlWidget

#make_value, #set_attr, #set_value

Constructor Details

#initialize(name, arg = {}) ⇒ AlFile

(AlFile) constructor



1244
1245
1246
1247
1248
# File 'lib/al_form.rb', line 1244

def initialize( name, arg = {} )
  require 'al_form/input_file'

  super( name, arg )
end

Class Method Details

.dirnameString

dirnameのgetter

Returns:

  • (String)

    テンポラリファイルを作成する場所(ディレクトリ)



25
26
27
# File 'lib/al_form/input_file.rb', line 25

def self.dirname
  return @@dirname
end

.dirname=(dir) ⇒ Object

dirnameのsetter

Parameters:

  • dir (String)

    テンポラリファイルを作成する場所(ディレクトリ)



34
35
36
# File 'lib/al_form/input_file.rb', line 34

def self.dirname=( dir )
  @@dirname = dir
end

Instance Method Details

#make_tag(appendix_tag = {}) ⇒ String

Note:

(AlFile) HTMLタグの生成

hiddenフラグは未対応。type=“file”は動作が特殊なので、hiddenにする意味がない。

Parameters:

  • appendix_tag (Hash) (defaults to: {})

    htmlタグへ追加するアトリビュートを指定

Returns:

  • (String)

    htmlタグ



69
70
71
72
73
74
# File 'lib/al_form/input_file.rb', line 69

def make_tag( appendix_tag = {} )
  r = %Q(<input type="file" name="#{@name}" id="#{@name}")
  make_tag_attr(r, appendix_tag)
  r << ">"
  return r
end

#save_file(arg = {}) ⇒ Object

Note:

(AlFile) アップロードされたファイルの恒久的保存

アップロードされたファイルが保存されているテンポラリファイルを、恒久的なファイルにする。 引数で与えた basenameパラメータへ8文字のランダム数をつけたファイル名を生成して付与する。

Parameters:

  • arg (Hash) (defaults to: {})

    引数ハッシュ

Options Hash (arg):

  • :basename (String)

    付与するファイル名のプリフィックス (ex: “pic_”)

  • :extname (String)

    付与するファイル名の拡張子 指定しなければ、アップロードファイルに合わせる (ex: “.jpg”)

  • :permission (String)

    保存するファイルのパーミッション (ex: 0666)



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/al_form/input_file.rb', line 88

def save_file( arg = {} )
  1000.times do
    # make filename -  "dirname" + "base" + random-part + "extname"
    saved_name = File.join( AlFile.dirname,
      arg[:basename].to_s + "00000000#{rand(99999999)}"[-8,8] +
      (arg[:extname] ? arg[:extname] : File.extname( @value[:filename] )) )

    begin
      File.link( @value[:tmp_name], saved_name )
      File.chmod( arg[:permission], saved_name ) if arg[:permission]
      @value[:saved_name] = saved_name
      return                  # normal return.

    rescue =>ex
      # nothing to do. retry it.
    end
  end

  raise "Can't make persist file."
end

#validateBoolean

(AlFile) バリデート

Returns:

  • (Boolean)

    成否



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/al_form/input_file.rb', line 44

def validate()
  @message = ""
  if @value.class != Hash
    raise "AlFile needs \@value by hash obj. #{@value.class} given, now.  Maybe enctype is not multipart/form-data."
  end

  if @value[:size] == 0
    if @required
      @message = "#{@label}を指定してください。"
      return false
    end
  end

  return true
end