Each Node of the repository can have multiple Attachments associated with it. This controller is used to handle REST operations for the attachments.
Create a new attachment for a give :node_id using a file that has been submitted using an HTML form POST request.
[ show source ]
# File app/controllers/attachments_controller.rb, line 18
18: def create
19: # TODO: what happens with files if they already exist?
20: @attachment = Attachment.new(params['attachment_file'].original_filename, :node_id => params[:node_id])
21: @attachment << params['attachment_file'].read
22: @attachment.save
23:
24: # Note: this breaks the basic html scaffolds, but is required for the FileTree
25: # extension
26: #redirect_to node_attachments_path(params[:node_id])
27: render :text => {:success => true}.to_json
28: end
Invoke this method to delete an Attachment from the server. It receives the attachment‘s file name in the :id parameter and the corresponding node in the :node_id parameter.
[ show source ]
# File app/controllers/attachments_controller.rb, line 77
77: def destroy
78: # we send the file name as the id, the rails parser however split the filename
79: # at the fullstop so we join it again
80: filename = params[:id]
81: @attachment = Attachment.find(filename, :conditions => {:node_id => Node.find(params[:node_id]).id})
82: @attachment.delete
83: redirect_to node_attachments_path(params[:node_id])
84: end
Retrieve all the associated attachments for a given :node_id
[ show source ]
# File app/controllers/attachments_controller.rb, line 7
7: def index
8: @attachments = Node.find(params[:node_id]).attachments
9: respond_to do |format|
10: format.html{ render :action => 'index'}
11: format.json{ render :json => @attachments }
12: end
13: @attachments.each do |a| a.close end
14: end
This function will send the Attachment file to the browser. It will try to figure out if the file is an image in which case the attachment will be displayed inline. By default the Content-disposition will be set to attachment.
[ show source ]
# File app/controllers/attachments_controller.rb, line 47
47: def show
48: # we send the file name as the id, the rails parser however split the filename
49: # at the fullstop so we join it again
50: filename = params[:id]
51: @attachment = Attachment.find(filename, :conditions => {:node_id => Node.find(params[:node_id]).id})
52:
53: # Figure out the best way of displaying the file (by default send the it as
54: # an attachment).
55: extname = File.extname(filename)[1..-1]
56: disposition = 'attachment'
57: if extname
58: # account for the possibility of this being an image and present the
59: # attachment inline
60: mime_type = Mime::Type.lookup_by_extension(extname)
61: content_type = mime_type.to_s unless mime_type.nil?
62: if content_type.match('image')
63: disposition = 'inline'
64: end
65: end
66:
67: send_data(@attachment.read, :type => content_type,
68: :filename => @attachment.filename,
69: :disposition => disposition)
70:
71: @attachment.close
72: end
It is possible to rename attachments and this function provides that functionality.
[ show source ]
# File app/controllers/attachments_controller.rb, line 32
32: def update
33: attachment = Attachment.find(params[:id], :conditions => {:node_id => Node.find(params[:node_id]).id})
34: attachment.close
35: new_name = CGI::unescape( params[:rename] )
36: destination = File.expand_path( File.join( Attachment.pwd, params[:node_id], new_name ) )
37: if !File.exist?(destination) && ( !destination.match(/^#{Attachment.pwd}/).nil? )
38: File.rename( attachment.fullpath, destination )
39: end
40: redirect_to :action => 'show', :id => params[:rename]
41: end