Class StorageObject
In: app/models/storage_object.rb
Parent: ActiveRecord::Base

サーバ上に保存された添付ファイルのモデル。

Methods

content   copy   location   path   store  

Public Instance methods

ファイルの内容を読み出す。

[Source]

    # File app/models/storage_object.rb, line 77
77:   def content
78:     File.open(path, "rb", &:read)
79:   end

複製操作のためにコピーする。失敗した場合は false を返す。

[Source]

    # File app/models/storage_object.rb, line 82
82:   def copy
83:     copied = self.class.new
84:     copied.attributes = attributes
85:     if copied.save
86:       if length == File.open(copied.path, "wb") {|f| f.write File.open(path, "rb", &:read)}
87:         return copied
88:       end
89:     end
90:     return false
91:   end

保存先としてのラベルの文字列を返す。

[Source]

    # File app/models/storage_object.rb, line 31
31:   def location
32:     s_("StorageObject|Server")
33:   end

保存先のパスを返す。

[Source]

    # File app/models/storage_object.rb, line 36
36:   def path
37:     storage = Storage.current
38:     storage.root + storage.separator + id.to_s
39:   end

ファイル x を保存する。

[Source]

    # File app/models/storage_object.rb, line 42
42:   def store(x)
43:     if Storage.current.available?
44:       if x.is_a?(Tempfile)
45:         File.rename(x.path, path)
46:       else
47:         File.open(path, "wb") do |f|
48:           f.write x.read
49:         end
50:       end
51:       File.chmod(0644, path)
52:       self.length = File.size(path)
53:       begin
54:         ConfigAttachment.current.validate_length(self.length)
55:       rescue ConfigAttachment::MaximumLengthError
56:         unlink
57:         raise
58:       end
59:     else
60:       self.length = x.length
61:     end
62:     # x has instance method 'original_filename'.
63:     # cf. http://www.ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI.html
64:     if x.respond_to?("original_path") # Rails 2.0.x -- http://dev.rubyonrails.org/changeset/7759
65:       fpath = x.original_path
66:     elsif x.respond_to?("full_original_filename") # Rails 1.2.6 -- http://dev.rubyonrails.org/changeset/2345
67:       fpath = x.full_original_filename
68:     else
69:       fpath = x.original_filename
70:     end
71:     epath = URI.encode(fpath.gsub('\\','/'))
72:     self.uri = "file:///#{epath}"
73:     return save
74:   end

[Validate]