2019-01-16 16:28:25 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# API Controller for resources of type Export
|
|
|
|
# Export are used to download data tables in offline files
|
2016-07-27 11:28:54 +02:00
|
|
|
class API::ExportsController < API::ApiController
|
|
|
|
before_action :authenticate_user!
|
|
|
|
before_action :set_export, only: [:download]
|
|
|
|
|
|
|
|
def download
|
|
|
|
authorize @export
|
2022-11-16 16:55:56 +01:00
|
|
|
mime_type = case @export.extension
|
|
|
|
when 'xlsx'
|
2019-07-30 11:43:51 +02:00
|
|
|
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
|
2022-11-16 16:55:56 +01:00
|
|
|
when 'csv'
|
2019-07-30 11:43:51 +02:00
|
|
|
'text/csv'
|
|
|
|
else
|
|
|
|
'application/octet-stream'
|
|
|
|
end
|
2016-07-27 15:42:29 +02:00
|
|
|
|
2016-11-09 15:01:41 +01:00
|
|
|
if FileTest.exist?(@export.file)
|
2022-11-16 16:55:56 +01:00
|
|
|
send_file Rails.root.join(@export.file),
|
2019-07-30 11:43:51 +02:00
|
|
|
type: mime_type,
|
2019-01-16 16:28:25 +01:00
|
|
|
disposition: 'attachment'
|
2016-11-09 15:01:41 +01:00
|
|
|
else
|
|
|
|
render text: I18n.t('errors.messages.export_not_found'), status: :not_found
|
|
|
|
end
|
2016-07-27 11:28:54 +02:00
|
|
|
end
|
|
|
|
|
2016-07-27 15:42:29 +02:00
|
|
|
def status
|
|
|
|
authorize Export
|
|
|
|
|
2022-11-16 16:55:56 +01:00
|
|
|
export = ExportService.last_export("#{params[:category]}/#{params[:type]}", params[:query], params[:key], params[:extension])
|
2016-07-28 10:28:43 +02:00
|
|
|
|
2019-07-30 11:43:51 +02:00
|
|
|
if export.nil? || !FileTest.exist?(export.file)
|
|
|
|
render json: { exists: false, id: nil }, status: :ok
|
|
|
|
else
|
|
|
|
render json: { exists: true, id: export.id }, status: :ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-07-27 11:28:54 +02:00
|
|
|
def set_export
|
|
|
|
@export = Export.find(params[:id])
|
|
|
|
end
|
2019-01-16 16:28:25 +01:00
|
|
|
end
|