2016-04-28 13:46:13 +02:00
|
|
|
require 'filemagic'
|
|
|
|
|
|
|
|
class DocumentsController < ApplicationController
|
2019-10-28 21:11:35 +01:00
|
|
|
before_action -> { require_plugin_enabled FoodsoftDocuments }
|
2016-04-28 13:46:13 +02:00
|
|
|
|
|
|
|
def index
|
|
|
|
if params["sort"]
|
|
|
|
sort = case params["sort"]
|
2021-03-01 15:27:26 +01:00
|
|
|
when "name" then "data IS NULL DESC, name"
|
|
|
|
when "created_at" then "created_at"
|
|
|
|
when "name_reverse" then "data IS NULL, name DESC"
|
|
|
|
when "created_at_reverse" then "created_at DESC"
|
2016-04-28 13:46:13 +02:00
|
|
|
end
|
|
|
|
else
|
2017-11-08 22:35:12 +01:00
|
|
|
sort = "data IS NULL DESC, name"
|
2016-04-28 13:46:13 +02:00
|
|
|
end
|
|
|
|
|
2017-11-08 22:35:12 +01:00
|
|
|
@documents = Document.where(parent: @document).page(params[:page]).per(@per_page).order(sort)
|
2016-04-28 13:46:13 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
2017-11-08 22:35:12 +01:00
|
|
|
@document = Document.new parent_id: params[:document_id]
|
|
|
|
@document.mime = '' unless params[:type] == 'folder'
|
2016-04-28 13:46:13 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2017-11-08 22:35:12 +01:00
|
|
|
@document = Document.new name: params[:document][:name]
|
|
|
|
@document.parent = Document.find_by_id(params[:document][:parent_id])
|
|
|
|
data = params[:document][:data]
|
|
|
|
if data
|
|
|
|
@document.data = data.read
|
|
|
|
@document.mime = FileMagic.new(FileMagic::MAGIC_MIME).buffer(@document.data)
|
|
|
|
raise t('.not_allowed_mime', mime: @document.mime) unless allowed_mime? @document.mime
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2017-11-08 22:35:12 +01:00
|
|
|
if @document.name.empty?
|
|
|
|
name = File.basename(data.original_filename)
|
|
|
|
@document.name = name.gsub(/[^\w\.\-]/, '_')
|
|
|
|
end
|
2016-04-28 13:46:13 +02:00
|
|
|
end
|
|
|
|
@document.created_by = current_user
|
|
|
|
@document.save!
|
2017-11-08 22:35:12 +01:00
|
|
|
redirect_to @document.parent || documents_path, notice: t('.notice')
|
2016-04-28 13:46:13 +02:00
|
|
|
rescue => error
|
2017-11-08 22:35:12 +01:00
|
|
|
redirect_to @document.parent || documents_path, alert: t('.error', error: error.message)
|
2016-04-28 13:46:13 +02:00
|
|
|
end
|
|
|
|
|
2020-03-06 14:30:27 +01:00
|
|
|
def update
|
|
|
|
@document = Document.find(params[:id])
|
|
|
|
@document.update_attribute(:parent_id, params[:parent_id])
|
|
|
|
redirect_to @document.parent || documents_path, notice: t('.notice')
|
|
|
|
rescue => error
|
|
|
|
redirect_to @document.parent || documents_path, alert: t('errors.general_msg', msg: error.message)
|
|
|
|
end
|
|
|
|
|
2016-04-28 13:46:13 +02:00
|
|
|
def destroy
|
|
|
|
@document = Document.find(params[:id])
|
|
|
|
if @document.created_by == current_user or current_user.role_admin?
|
|
|
|
@document.destroy
|
2017-10-11 01:37:40 +02:00
|
|
|
redirect_to documents_path, notice: t('.notice')
|
2016-04-28 13:46:13 +02:00
|
|
|
else
|
2017-10-11 01:37:40 +02:00
|
|
|
redirect_to documents_path, alert: t('.no_right')
|
2016-04-28 13:46:13 +02:00
|
|
|
end
|
|
|
|
rescue => error
|
2017-10-11 01:37:40 +02:00
|
|
|
redirect_to documents_path, alert: t('.error', error: error.message)
|
2016-04-28 13:46:13 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
@document = Document.find(params[:id])
|
2017-11-08 22:35:12 +01:00
|
|
|
if @document.file?
|
|
|
|
send_data(@document.data, filename: @document.filename, type: @document.mime)
|
|
|
|
else
|
|
|
|
index
|
|
|
|
render :index
|
|
|
|
end
|
2016-04-28 13:46:13 +02:00
|
|
|
end
|
2017-10-11 02:39:35 +02:00
|
|
|
|
2020-03-06 14:30:27 +01:00
|
|
|
def move
|
|
|
|
@document = Document.find(params[:document_id])
|
|
|
|
end
|
|
|
|
|
2017-10-11 02:39:35 +02:00
|
|
|
def allowed_mime?(mime)
|
|
|
|
whitelist = FoodsoftConfig[:documents_allowed_extension].split
|
|
|
|
MIME::Types.type_for(whitelist).each do |type|
|
|
|
|
return true if type.like? mime
|
|
|
|
end
|
|
|
|
false
|
|
|
|
end
|
2016-04-28 13:46:13 +02:00
|
|
|
end
|