1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-11 22:24:21 +01:00
fab-manager/app/controllers/api/exports_controller.rb

47 lines
1.3 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
# API Controller for resources of type Export
# Export are used to download data tables in offline files
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'
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
2022-11-16 16:55:56 +01:00
when 'csv'
'text/csv'
else
'application/octet-stream'
end
if FileTest.exist?(@export.file)
2022-11-16 16:55:56 +01:00
send_file Rails.root.join(@export.file),
type: mime_type,
disposition: 'attachment'
else
render text: I18n.t('errors.messages.export_not_found'), status: :not_found
end
end
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])
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
def set_export
@export = Export.find(params[:id])
end
end