Module: Sufia::FilesControllerBehavior

Extended by:
ActiveSupport::Concern
Included in:
GenericFilesController
Defined in:
lib/sufia/files_controller_behavior.rb

Instance Method Summary collapse

Instance Method Details

#auditObject

routed to /files/:id/audit (POST)


131
132
133
# File 'lib/sufia/files_controller_behavior.rb', line 131

def audit
  render :json=>@generic_file.audit
end

#citationObject

routed to /files/:id/citation


117
118
# File 'lib/sufia/files_controller_behavior.rb', line 117

def citation
end

#createObject

routed to /files (POST)


70
71
72
73
74
75
76
77
# File 'lib/sufia/files_controller_behavior.rb', line 70

def create
  case params['file_coming_from']
  when 'dropbox'
    create_from_url(params)
  else
    create_from_local(params)
  end
end

#create_from_local(params) ⇒ Object


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/sufia/files_controller_behavior.rb', line 92

def create_from_local(params)
  begin
    # check error condition No files
    return json_error("Error! No file to save") if !params.has_key?(:files)

    file = params[:files].detect {|f| f.respond_to?(:original_filename) }
    if !file
      json_error "Error! No file for upload", 'unknown file', :status => :unprocessable_entity
    elsif (empty_file?(file))
      json_error "Error! Zero Length File!", file.original_filename
    elsif (!terms_accepted?)
      json_error "You must accept the terms of service!", file.original_filename
    else
      process_file(file)
    end
  rescue => error
    logger.error "GenericFilesController::create rescued #{error.class}\n\t#{error.to_s}\n #{error.backtrace.join("\n")}\n\n"
    json_error "Error occurred while creating generic file."
  ensure
    # remove the tempfile (only if it is a temp file)
    file.tempfile.delete if file.respond_to?(:tempfile)
  end
end

#create_from_url(params) ⇒ Object


79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/sufia/files_controller_behavior.rb', line 79

def create_from_url(params)
  params[:dropbox_urls].each do |db_file|
    next if db_file.blank?
    # do not remove :: 
    @generic_file = ::GenericFile.new
    @generic_file.import_url = db_file
    @generic_file.label = File.basename(db_file)
    Sufia::GenericFile::Actions.(@generic_file, current_user, params[:batch_id])
    Sufia.queue.push(ImportUrlJob.new(@generic_file.pid)) 
  end
  redirect_to sufia.batch_edit_path(params[:batch_id])
end

#destroyObject

routed to /files/:id (DELETE)


62
63
64
65
66
67
# File 'lib/sufia/files_controller_behavior.rb', line 62

def destroy
  pid = @generic_file.noid
  @generic_file.destroy
  Sufia.queue.push(ContentDeleteEventJob.new(pid, current_user.user_key))
  redirect_to sufia.dashboard_index_path, :notice => render_to_string(:partial=>'generic_files/asset_deleted_flash', :locals => { :generic_file => @generic_file })
end

#editObject

routed to /files/:id/edit


56
57
58
59
# File 'lib/sufia/files_controller_behavior.rb', line 56

def edit
  @generic_file.initialize_fields
  @groups = current_user.groups
end

#newObject

routed to /files/new


50
51
52
53
# File 'lib/sufia/files_controller_behavior.rb', line 50

def new
  @generic_file = ::GenericFile.new
  @batch_noid = Sufia::Noid.noidify(Sufia::IdService.mint)
end

#showObject

routed to /files/:id


121
122
123
124
125
126
127
128
# File 'lib/sufia/files_controller_behavior.rb', line 121

def show
  respond_to do |format|
    format.html {
      @events = @generic_file.events(100)
    }
    format.endnote { render :text => @generic_file.export_as_endnote }
  end
end

#updateObject

routed to /files/:id (PUT)


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/sufia/files_controller_behavior.rb', line 136

def update
  version_event = false

  if params.has_key?(:revision) and params[:revision] !=  @generic_file.content.latest_version.versionID
    revision = @generic_file.content.get_version(params[:revision])
    @generic_file.add_file_datastream(revision.content, :dsid => 'content', :mimeType => revision.mimeType)
    version_event = true
    Sufia.queue.push(ContentRestoredVersionEventJob.new(@generic_file.pid, current_user.user_key, params[:revision]))
  end

  if params.has_key?(:filedata)
    file = params[:filedata]
    return unless virus_check(file) == 0
    @generic_file.add_file(file, datastream_id, file.original_filename)
    version_event = true
    Sufia.queue.push(ContentNewVersionEventJob.new(@generic_file.pid, current_user.user_key))
  end

  # only update metadata if there is a generic_file object which is not the case for version updates
   if params[:generic_file]

  #always save the file so the new version or metadata gets recorded
  if @generic_file.save
    # do not trigger an update event if a version event has already been triggered
    Sufia.queue.push(ContentUpdateEventJob.new(@generic_file.pid, current_user.user_key)) unless version_event
    @generic_file.record_version_committer(current_user)
    redirect_to sufia.edit_generic_file_path(:tab => params[:redirect_tab]), :notice => render_to_string(:partial=>'generic_files/asset_updated_flash', :locals => { :generic_file => @generic_file })
  else
    render action: 'edit'
  end
end