class UploadsController < ApplicationController before_filter :login_required # GET /uploads # GET /uploads.xml def index @uploads = Upload.find(:all) respond_to do |format| format.html # index.html.erb format.xml { render :xml => @uploads } end end # GET /uploads/1 # GET /uploads/1.xml def show @upload = Upload.find(params[:id]) respond_to do |format| format.html # show.html.erb format.xml { render :xml => @upload } end end # GET /uploads/new # GET /uploads/new.xml def new @upload = Upload.new @ebooks = respond_to do |format| format.html # new.html.erb format.xml { render :xml => @upload } end end # GET /uploads/1/edit def edit @upload = Upload.find(params[:id]) end # POST /uploads # POST /uploads.xml def create unless params[:upload][:file].blank? datafile=params[:upload][:file] unless params[:upload][:file].original_filename.empty? d = RAILS_ROOT + '/public/uploads/files/' + datafile.original_filename.rstrip.lstrip @upload = Upload.new @upload.filename = datafile.original_filename @upload.content_type = datafile.content_type #@upload.size = datafile.size.blank? ? 0 : datafile.size f=File.new(d,'wb') f.write datafile.read f.close @upload.size = File.size(d) if @upload.save && File.exists?(d) flash[:notice] = 'The file was successfully uploaded.' redirect_to :action => 'index' else File.delete(d) if File.exists?(d) Upload.destroy(@upload.id) if Upload.exists?(@upload.id) flash[:notice] = 'A problem occured while saving the file.' render :action => "new" end else flash[:notice] = 'The filename was invalid.' render :action => 'new' end else flash[:notice] = 'The filename was invalid.' render :action => 'new' end end # PUT /uploads/1 # PUT /uploads/1.xml def update @upload = Upload.find(params[:id]) respond_to do |format| if @upload.update_attributes(params[:upload]) flash[:notice] = 'Upload was successfully updated.' format.html { redirect_to(@upload) } format.xml { head :ok } else format.html { render :action => "edit" } format.xml { render :xml => @upload.errors, :status => :unprocessable_entity } end end end # DELETE /uploads/1 # DELETE /uploads/1.xml def destroy @upload = Upload.find(params[:id]) d=RAILS_ROOT + '/public/uploads/files/' + @upload.filename File.delete(d) if File.exists?(d) @upload.destroy respond_to do |format| format.html { redirect_to(uploads_url) } format.xml { head :ok } end end end