Class: DownloadsController

Inherits:
ApplicationController show all
Includes:
DataControllerConfiguration::ProjectDataControllerConfiguration
Defined in:
app/controllers/downloads_controller.rb

Constant Summary

Constants included from ProjectsHelper

ProjectsHelper::CLASSIFIER, ProjectsHelper::CLASSIFIER_ANNOTATION

Instance Method Summary collapse

Methods included from DataControllerConfiguration::ProjectDataControllerConfiguration

#annotator_params

Methods included from RedirectHelper

#destroy_redirect

Methods included from RequestType

#json_request?

Methods included from LogRecent

#log_user_recent_route

Methods included from Cookies

#digest_cookie, #digested_cookie_exists?

Methods included from Whitelist

#whitelist_constantize

Methods included from ProjectsHelper

#cumulative_gb_per_year, #cumulative_projects_created_per_year, #document_cumulative_gb_per_year, #document_gb_per_year, #gb_per_year, #image_cumulative_gb_per_year, #image_gb_per_year, #invalid_object, #project_classification, #project_link, #project_login_link, #project_matches, #project_tag, #projects_list, #projects_search_form, #sound_cumulative_gb_per_year, #sound_gb_per_year, #taxonworks_classification, #week_in_review_graphs

Methods included from Api::Intercept

#intercept_api

Methods included from TokenAuthentication

#intercept_project, #intercept_user, #intercept_user_or_project, #project_token_authenticate, #token_authenticate

Instance Method Details

#api_dwc_archive_completeObject

POST /api/v1/downloads/api_dwc_archive_complete?project_token=<>



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/controllers/downloads_controller.rb', line 105

def api_dwc_archive_complete
  project = Project.find(sessions_current_project_id)

  if !project.complete_dwc_download_is_public?
    render json: { success: false }, status: :forbidden
    return
  end

  begin
    if download = Download::DwcArchive::Complete.process_complete_download_request(project)
      send_file download.file_path
      return
    end
  rescue TaxonWorks::Error => e
    render json: { status: e.to_s }, status: :unprocessable_entity
    return
  end

  # Complete project download doesn't exist yet, spin one up.
  # *All* options for complete downloads are determined from project
  # preferences, not from public request via api.
  # !! Publicly explodes if EML prefs contain 'STUB' text.
  # If no user token or user session then create as the complete download
  # user.
  by_id = Current.user_id || project.complete_dwc_download_default_user_id
  Download::DwcArchive::Complete.create!(by: by_id)
  render json: { status: 'A download is being created' }, status: :unprocessable_entity
end

#api_fileObject

GET /api/v1/downloads/123/file.json



91
92
93
94
95
96
97
98
# File 'app/controllers/downloads_controller.rb', line 91

def api_file
  if @download.ready?
    @download.increment!(:times_downloaded)
    send_file @download.file_path
  else
    render json: { success: false }
  end
end

#api_indexObject



83
84
85
86
87
88
# File 'app/controllers/downloads_controller.rb', line 83

def api_index
  # If default scope is removed return here
  @downloads = Download.where(project_id: sessions_current_project_id)
    .order('downloads.id').page(params[:page]).per(params[:per])
  render '/downloads/api/v1/index'
end

#api_showObject



100
101
102
# File 'app/controllers/downloads_controller.rb', line 100

def api_show
  render '/downloads/api/v1/show'
end

#destroyObject

DELETE /downloads/1 DELETE /downloads/1.json



38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/downloads_controller.rb', line 38

def destroy
  @download.destroy
  respond_to do |format|
    if @download.destroyed?
      format.html { destroy_redirect @download, notice: 'Download was successfully destroyed.' }
      format.json { head :no_content}
    else
      format.html { destroy_redirect @download, notice: 'Download was not destroyed, ' + @download.errors.full_messages.join('; ') }
      format.json { render json: @download.errors, status: :unprocessable_entity }
    end
  end
end

#download_paramsObject (private)



145
146
147
# File 'app/controllers/downloads_controller.rb', line 145

def download_params
  params.require(:download).permit(:is_public, :name, :expires )
end

#editObject

GET /downloads/1/edit



33
34
# File 'app/controllers/downloads_controller.rb', line 33

def edit
end

#fileObject

GET /downloads/1/file



73
74
75
76
77
78
79
80
81
# File 'app/controllers/downloads_controller.rb', line 73

def file
  if @download.ready?
    @download.increment!(:times_downloaded)
    response.headers["Content-Length"] = File.size(@download.file_path).to_s
    send_file @download.file_path
  else
    redirect_to download_url
  end
end

#indexObject

GET /downloads GET /downloads.json



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/downloads_controller.rb', line 13

def index
  respond_to do |format|
    format.html do
      @recent_objects = Download.unscoped.recent_from_project_id(sessions_current_project_id).order(updated_at: :desc).limit(10)
      render '/shared/data/all/index'
    end
    format.json {
      @downloads = Queries::Download::Filter.new(params)
        .all
        .page(params[:page])
        .per(params[:per])
    }
  end
end

#listObject

GET /downloads/list GET /downloads/list.json



67
68
69
70
# File 'app/controllers/downloads_controller.rb', line 67

def list
  # has a default scope
  @downloads = Download.unscoped.where(project_id: sessions_current_project_id).order(:id).page(params[:page]).per(params[:per])
end

#set_downloadObject (private)



136
137
138
139
# File 'app/controllers/downloads_controller.rb', line 136

def set_download
  # Why .unscoped ?
  @download = Download.unscoped.where(project_id: sessions_current_project_id).find(params[:id])
end

#set_download_apiObject (private)



141
142
143
# File 'app/controllers/downloads_controller.rb', line 141

def set_download_api
  @download = Download.unscoped.where(is_public: true, project_id: sessions_current_project_id).find(params[:id])
end

#showObject

GET /downloads/1



29
30
# File 'app/controllers/downloads_controller.rb', line 29

def show
end

#updateObject

PATCH /downloads/1 PATCH /downloads/1.json



53
54
55
56
57
58
59
60
61
62
63
# File 'app/controllers/downloads_controller.rb', line 53

def update
  respond_to do |format|
    if @download.update(download_params)
      format.html { redirect_to @download.metamorphosize, notice: 'Download was successfully updated.' }
      format.json { render :show, location: @download.metamorphosize }
    else
      format.html { render action: 'edit' }
      format.json { render json: @download.errors, status: :unprocessable_entity }
    end
  end
end