root/trunk/lib/webdav_adapter.rb

Revision 860, 3.8 kB (checked in by gaspard, 1 year ago)

Replaced 'secure' by 'secure!' as it raises exceptions.
Added 'secure' which returns nil on failure.
<style> tags are now parsed for assets (url translation). Refs #128.

Line 
1 module Zena
2   module WebDav
3    
4     private
5       def mkcol_for_path(path)
6       end
7      
8       def write_content_to_path(path, content)
9         puts "WRITE:#{path}"
10         node = get_node_for_path(path)
11         puts "NODE_WRITE: #{node.name}"
12         if node
13           # update
14           node.update_attributes(:v_text => content)
15         else
16           # create
17           parent_path = path.split('/')[0..-2]
18           parent = secure!(Node) { Node.find_by_path(parent_path.join('/')) }
19           node   = secure!(Page) { Page.create(:parent_id => parent[:id], :name => path.split('/').last, :v_text => content ) }
20         end
21       end
22      
23       def copy_to_path(resource, dest_path, depth)
24         resource.copy!(dest_path, depth)
25       end
26      
27       def move_to_path(resource, dest_path, depth)
28         resource.move!(dest_path, depth)
29       end
30      
31       def get_resource_for_path(path)
32         ZenaNodeResource.new(get_node_for_path(path))
33       end
34      
35       def get_node_for_path(path)
36         raise WebDavErrors::NotFoundError if path =~ /\.DS_Store/
37         puts "FIND: #{path.inspect}"
38         return visitor.site.root_node if path.blank? or path.eql?("/")
39         if path =~ /(.*)\./
40           node = secure!(Node) { Node.find_by_path($1) }
41         else
42           node = secure!(Node) { Node.find_by_path(path) }
43         end 
44         raise WebDavErrors::NotFoundError if node.nil?
45         node
46       rescue ActiveRecord::RecordNotFound
47         raise WebDavErrors::NotFoundError
48       end
49   end
50 end
51
52 class ZenaNodeResource
53   attr_accessor :node
54   include Zena::Acts::Secure
55   include WebDavResource
56   def visitor
57     @node.visitor
58   end
59  
60   def initialize(node)
61     @node   = node
62   end
63  
64   def displayname
65     if @node.kind_of?(Note)
66       @node.name + '.txt'
67     elsif @node.kind_of?(Document)
68       @node.name + '.' + @node.c_ext
69     else
70       @node.name
71     end
72   end
73  
74   def href
75     return '/' if @node.fullpath.blank?
76     '/' + (@node.fullpath.split('/')[0..-2] + [self.displayname]).join('/')
77   end
78
79   def delete!
80     # FIXME
81   end
82
83   def move! (dest_path, depth)
84     parent_path = dest_path.split('/')[0..-2]
85     parent = secure!(Node) { Node.find_by_path(parent_path.join('/')) }
86     raise WebDavErrors::ConflictError unless @node.update_attributes(:parent_id => parent[:id])
87   rescue ActiveRecord::RecordNotFound
88     raise WebDavErrors::ForbiddenError
89   end
90
91   def copy! (dest_path, depth)
92     # FIXME
93   end
94
95   def status
96     gen_status(200, "OK").to_s
97   end
98
99   def collection?
100     return !@node.kind_of?(Document) && !@node.kind_of?(Note)
101   end
102
103   def children
104     res = (@node.children || []).map { |p| ZenaNodeResource.new(p) }
105     puts "CHILDREN OF #{@node.fullpath}: #{res.map { |r| r.get_href }.inspect }"
106    
107     res
108   end
109
110   def set_displayname(value)
111     if @node.update_attributes(:name => value)
112      gen_status(200, "OK").to_s
113     else
114       gen_status(409, "Conflict").to_s
115     end
116   end
117
118   def creationdate
119     @node.created_at.httpdate
120   end
121
122   def getlastmodified
123     @node.updated_at.httpdate
124   end
125
126   def set_getlastmodified(value)
127     if @node.update_attributes(:updated_at => value)
128       gen_status(200, "OK").to_s
129     else
130       gen_status(409, "Conflict").to_s
131     end
132   end
133
134   # ???
135   def getetag
136      # ??sprintf('%x-%x-%x', @st.ino, @st.size, @st.mtime.to_i) unless @file.nil?
137   end
138
139   def getcontenttype
140     if @node.kind_of?(Note)
141       "text/plain"
142     elsif @node.kind_of?(Document)
143       @node.c_content_type
144     else
145       "httpd/unix-directory"
146     end
147   end
148
149   def getcontentlength
150     if @node.kind_of?(Note)
151       @node.v_text.length
152     elsif @node.kind_of?(Document)
153       s = @node.c_file.stat.size
154       puts "SIZE #{@node.name} = #{s}"
155       s
156     else
157       0
158     end
159   end
160
161   def data
162     if @node.kind_of?(Note)
163       @node.v_text
164     elsif @node.kind_of?(Document)
165       f = @node.c_file
166       puts f
167       f
168     else
169       0
170     end
171   end
172 end
Note: See TracBrowser for help on using the browser.