Class: Uploader::UploadsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/uploader/uploads_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/uploader/uploads_controller.rb', line 8

def create
  # Standard, one-at-a-time, upload action       
  if request.env['HTTP_X_FILE_UPLOAD'] == 'true'        
    tempfile = Tempfile.new(request.env['HTTP_X_FILE_NAME'], Uploader.configuration.temp_dir)
    tempfile << request.env['rack.input'].read
    tempfile.flush
    tempfile.rewind
    @upload = Upload.new(:name => request.env['HTTP_X_FILE_NAME'], :hard_file_name => request.env['HTTP_X_FILE_NAME'])
    # NOTE hard_file_name MUST be set before setting local or paperclip will process the file before the hard_file_name is set
    @upload.local = tempfile
  else
    @upload = Upload.new(params[:upload])
  end    
  @upload.uploadable = @parent
  @upload.creator = get_creator
  @upload.save!    
  message = t('uploader.successful_upload')
  upload_json = basic_uploads_json(@upload)
  respond_to do |format|
    format.html do
      flash[:notice] = message
      redirect_to get_redirect(:create_success)
    end
    format.js { render :text => get_upload_text(@upload) }
    format.json { render :json => upload_json }
  end
rescue => ex
  message = t('uploader.standard_file_upload_error', :error => ex)
  respond_to do |format|
    format.html do
      flash[:notice] = message
      redirect_to get_redirect(:create_failure)
    end
    format.js { render :text => message }
    format.json do
      if Uploader.configuration.use_http_status_failures
        render :json => message, :status => :unprocessable_entity
      else
        render :json => { :success => false, :message => message }
      end
    end
  end
end

#destroyObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/controllers/uploader/uploads_controller.rb', line 77

def destroy
  @parent = @upload.uploadable # set this for redirect
  if @upload.can_edit?(current_user)
    @upload.destroy
    message = t('uploader.file_deleted')
    success = true
  else
    message = t("uploader.file_delete_permission_denied")
    success = false
  end
  respond_to do |format|
    format.html do
      flash[:notice] = message
      redirect_to get_redirect(:destroy_success)
    end
    format.js { render :text => message }
    format.json do
      if Uploader.configuration.use_http_status_failures
        render :json => @upload, :status => success ? :ok : :unprocessable_entity
      else
        render :json => { :success => success, :message => message }
      end
    end
  end
end

#multiuploadObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/controllers/uploader/uploads_controller.rb', line 52

def multiupload
  @upload = Upload.new
  @upload.is_public = true if params[:is_public] == true
  @upload.creator = get_creator
  @upload.uploadable = @parent
  @upload.multiupload_local = params[:Filedata]
  @upload.save!
  respond_to do |format|
    format.js { render :text => get_upload_text(@upload) }
    format.json { render :json => basic_uploads_json(@upload) }
  end
rescue => ex
  message = t('uploader.standard_file_upload_error', :error => ex)
  respond_to do |format|
    format.js { render :text => message }
    format.json do
      if Uploader.configuration.use_http_status_failures
        render :json => message, :status => :unprocessable_entity
      else
        render :json => { :success => false, :message => message }
      end
    end
  end
end