Class: GridFSRackDAV::GridFSResource

Inherits:
RackDAV::Resource
  • Object
show all
Defined in:
lib/gridfs-rackdav/gridfs_resource.rb

Constant Summary collapse

DIR_FILE =
"<tr><td class='name'><a href='%s'>%s</a></td><td class='size'>%s</td><td class='type'>%s</td><td class='mtime'>%s</td></tr>"
DIR_PAGE =
<<-PAGE
<html><head>
<title>%s</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<style type='text/css'>
table { width:100%%; }
.name { text-align:left; }
.size, .mtime { text-align:right; }
.type { width:11em; }
.mtime { width:15em; }
</style>
</head><body>
<h1>%s</h1>
<hr />
<table>
<tr>
<th class='name'>Name</th>
<th class='size'>Size</th>
<th class='type'>Type</th>
<th class='mtime'>Last Modified</th>
</tr>
%s
</table>
<hr />
</body></html>
PAGE

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options) ⇒ GridFSResource

Returns a new instance of GridFSResource.



35
36
37
38
39
40
# File 'lib/gridfs-rackdav/gridfs_resource.rb', line 35

def initialize(path, options)
  raise 'You must provide MongoDB connection in options[:connection]' if options[:connection].nil?
  raise 'You must provide root of collection in options[:root]' if options[:root].nil?
  super(path, options)
  @gridfs_model = GridFSModel.new(path, options)
end

Instance Attribute Details

#gridfs_modelObject (readonly)

Returns the value of attribute gridfs_model.



3
4
5
# File 'lib/gridfs-rackdav/gridfs_resource.rb', line 3

def gridfs_model
  @gridfs_model
end

Instance Method Details

#childrenObject

If this is a collection, return the child resources.



43
44
45
# File 'lib/gridfs-rackdav/gridfs_resource.rb', line 43

def children
  @gridfs_model.children_names.map { |filename| self.class.new(filename, @options)}
end

#collection?Boolean

Is this resource a collection?

Returns:

  • (Boolean)


48
49
50
# File 'lib/gridfs-rackdav/gridfs_resource.rb', line 48

def collection?
  @gridfs_model.collection?
end

#content_lengthObject

Return the size in bytes for this resource.



96
97
98
# File 'lib/gridfs-rackdav/gridfs_resource.rb', line 96

def content_length
  @gridfs_model.item['length']
end

#content_typeObject

Return the mime type of this resource.



91
92
93
# File 'lib/gridfs-rackdav/gridfs_resource.rb', line 91

def content_type
  @gridfs_model.item['contentType']
end

#copy(dest) ⇒ Object

HTTP COPY request.

Copy this resource to given destination resource.



148
149
150
151
152
153
154
155
# File 'lib/gridfs-rackdav/gridfs_resource.rb', line 148

def copy(dest)
  if collection?
    dest.gridfs_model.path = dest.gridfs_model.path + '/'
    dest.make_collection
  else
    dest.write(StringIO.new(@gridfs_model.get_file_contents)) unless @gridfs_model.item.nil?
  end
end

#creation_dateObject

Return the creation time.



58
59
60
# File 'lib/gridfs-rackdav/gridfs_resource.rb', line 58

def creation_date
  @gridfs_model.item['metadata'] ? Time.at(@gridfs_model.item['metadata']['mtime'].to_i) : Time.now
end

#deleteObject

HTTP DELETE request.

Delete this resource.



141
142
143
# File 'lib/gridfs-rackdav/gridfs_resource.rb', line 141

def delete
  @gridfs_model.delete
end

#etagObject

Return an Etag, an unique hash value for this resource.



74
75
76
77
78
# File 'lib/gridfs-rackdav/gridfs_resource.rb', line 74

def etag
  Digest::MD5.hexdigest(
    sprintf('%s-%x-%x', @gridfs_model.item['filename'], @gridfs_model.item['length'], @gridfs_model.item['metadata']['mtime'].to_i)
  )
end

#exist?Boolean

Does this recource exist?

Returns:

  • (Boolean)


53
54
55
# File 'lib/gridfs-rackdav/gridfs_resource.rb', line 53

def exist?
  !@gridfs_model.item.nil?
end

#get(request, response) ⇒ Object

HTTP GET request.

Write the content of the resource to the response.body. TODO: Write test for get method for collections



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/gridfs-rackdav/gridfs_resource.rb', line 104

def get(request, response)
  if @gridfs_model.collection?
    files = []
    if @gridfs_model.path != @options[:root] + '/'
      files << DIR_FILE % [@gridfs_model.path_without_root.split('/')[0..-2].join('/') + "/", '../', "", "", ""]
    end
    @gridfs_model.children.each do |f|
      time = Time.at(f.item['metadata']['mtime']).to_s if f.item['metadata'] and f.item['metadata']['mtime']
      files << DIR_FILE % [f.path_without_root, File.basename(f.item['filename']), f.item['length'], f.item['contentType'], time]        
    end
    response.body = DIR_PAGE % [@gridfs_model.path_without_root, @gridfs_model.path_without_root, files.join('')]
    response.body.strip!
    
    response['Content-Type'] = 'text/html'
    response['Content-Length'] = response.body.size.to_s
  else
    response.body = @gridfs_model.get_file_contents
  end
end

#last_modifiedObject

Return the time of last modification.



63
64
65
# File 'lib/gridfs-rackdav/gridfs_resource.rb', line 63

def last_modified
  @gridfs_model.item['metadata'] ? Time.at(@gridfs_model.item['metadata']['mtime'].to_i) : Time.now
end

#last_modified=(time) ⇒ Object

Set the time of last modification.



68
69
70
71
# File 'lib/gridfs-rackdav/gridfs_resource.rb', line 68

def last_modified=(time)
  @gridfs_model.item['metadata']['mtime'] = Time.parse(time).to_i
  @gridfs_model.save
end

#make_collectionObject

HTTP MKCOL request.

Create this resource as collection.



168
169
170
171
# File 'lib/gridfs-rackdav/gridfs_resource.rb', line 168

def make_collection
  @gridfs_model.path = @gridfs_model.path + '/'
  @gridfs_model.write
end

#move(dest) ⇒ Object

HTTP MOVE request.

Move this resource to given destination resource.



160
161
162
163
# File 'lib/gridfs-rackdav/gridfs_resource.rb', line 160

def move(dest)
  copy(dest)
  delete
end

#post(request, response) ⇒ Object

HTTP POST request.

Usually forbidden.

Raises:

  • (HTTPStatus::Forbidden)


134
135
136
# File 'lib/gridfs-rackdav/gridfs_resource.rb', line 134

def post(request, response)
  raise HTTPStatus::Forbidden
end

#put(request, response) ⇒ Object

HTTP PUT request.

Save the content of the request.body.



127
128
129
# File 'lib/gridfs-rackdav/gridfs_resource.rb', line 127

def put(request, response)
  write(request.body)
end

#resource_typeObject

Return the resource type.

If this is a collection, return REXML::Element.new(‘D:collection’)



84
85
86
87
88
# File 'lib/gridfs-rackdav/gridfs_resource.rb', line 84

def resource_type
  if @gridfs_model.collection?
    REXML::Element.new('D:collection')
  end
end

#write(io) ⇒ Object

Write to this resource from given IO.



174
175
176
177
178
179
180
# File 'lib/gridfs-rackdav/gridfs_resource.rb', line 174

def write(io)
  file = ''
  while part = io.read(8192)
    file << part
  end
  @gridfs_model.write(file)
end