trabajo-afectivo/app/controllers/ticket_articles_controller.rb

198 lines
4.6 KiB
Ruby
Raw Normal View History

2014-02-03 19:24:49 +00:00
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
2012-04-10 14:06:46 +00:00
class TicketArticlesController < ApplicationController
before_action :authentication_check
2012-04-10 14:06:46 +00:00
# GET /articles
def index
@articles = Ticket::Article.all
render json: @articles
2012-04-10 14:06:46 +00:00
end
# GET /articles/1
def show
2016-05-10 22:09:10 +00:00
@article = Ticket::Article.find(params[:id])
2012-04-10 14:06:46 +00:00
render json: @article
2012-04-10 14:06:46 +00:00
end
# POST /articles
def create
form_id = params[:form_id]
clean_params = Ticket::Article.param_association_lookup(params)
clean_params = Ticket::Article.param_cleanup(clean_params, true)
article = Ticket::Article.new(clean_params)
# permission check
return if !article_permission(article)
2012-04-10 14:06:46 +00:00
# find attachments in upload cache
2012-12-02 10:18:55 +00:00
if form_id
article.attachments = Store.list(
object: 'UploadCache',
o_id: form_id,
2012-12-02 10:18:55 +00:00
)
end
2012-04-10 14:06:46 +00:00
if article.save
# remove attachments from upload cache
Store.remove(
object: 'UploadCache',
o_id: form_id,
)
render json: article, status: :created
else
render json: article.errors, status: :unprocessable_entity
2012-04-10 14:06:46 +00:00
end
end
# PUT /articles/1
def update
# permission check
article = Ticket::Article.find(params[:id])
return if !article_permission(article)
clean_params = Ticket::Article.param_association_lookup(params)
clean_params = Ticket::Article.param_cleanup(clean_params, true)
if article.update_attributes(clean_params)
render json: article, status: :ok
else
render json: article.errors, status: :unprocessable_entity
2012-04-10 14:06:46 +00:00
end
end
# DELETE /articles/1
def destroy
article = Ticket::Article.find(params[:id])
return if !article_permission(article)
article.destroy
2012-04-10 14:06:46 +00:00
head :ok
2012-04-10 14:06:46 +00:00
end
2014-10-06 20:24:21 +00:00
# DELETE /ticket_attachment_upload
def ticket_attachment_upload_delete
if params[:store_id]
Store.remove_item(params[:store_id])
render json: {
success: true,
}
return
elsif params[:form_id]
Store.remove(
object: 'UploadCache',
o_id: params[:form_id],
)
render json: {
success: true,
}
return
end
2014-10-06 20:24:21 +00:00
render json: { message: 'No such store_id or form_id!' }, status: :unprocessable_entity
2014-10-06 20:24:21 +00:00
end
# POST /ticket_attachment_upload
def ticket_attachment_upload_add
# store file
2014-10-06 20:24:21 +00:00
file = params[:File]
content_type = file.content_type
if !content_type || content_type == 'application/octet-stream'
content_type = if MIME::Types.type_for(file.original_filename).first
MIME::Types.type_for(file.original_filename).first.content_type
else
'application/octet-stream'
end
end
headers_store = {
'Content-Type' => content_type
}
2014-10-06 20:24:21 +00:00
store = Store.add(
object: 'UploadCache',
o_id: params[:form_id],
data: file.read,
filename: file.original_filename,
preferences: headers_store
)
# return result
render json: {
success: true,
data: {
store_id: store.id,
filename: file.original_filename,
size: store.size,
2014-10-06 20:24:21 +00:00
}
}
end
2012-12-02 10:18:55 +00:00
# GET /ticket_attachment/:ticket_id/:article_id/:id
def attachment
2016-01-27 18:26:10 +00:00
# permission check
2016-05-10 22:09:10 +00:00
ticket = Ticket.lookup(id: params[:ticket_id])
if !ticket_permission(ticket)
render json: 'No such ticket.', status: :unauthorized
return
end
2016-05-10 22:09:10 +00:00
article = Ticket::Article.find(params[:article_id])
if ticket.id != article.ticket_id
render json: 'No access, article_id/ticket_id is not matching.', status: :unauthorized
return
end
list = article.attachments || []
access = false
list.each {|item|
if item.id.to_i == params[:id].to_i
access = true
end
}
if !access
render json: 'Requested file id is not linked with article_id.', status: :unauthorized
return
end
# find file
file = Store.find(params[:id])
send_data(
2014-04-28 07:44:36 +00:00
file.content,
filename: file.filename,
type: file.preferences['Content-Type'] || file.preferences['Mime-Type'],
disposition: 'inline'
)
end
# GET /ticket_article_plain/1
def article_plain
2016-01-27 18:26:10 +00:00
# permission check
2016-05-10 22:09:10 +00:00
article = Ticket::Article.find(params[:id])
return if !article_permission(article)
list = Store.list(
object: 'Ticket::Article::Mail',
o_id: params[:id],
)
# find file
return if !list
file = Store.find(list.first)
send_data(
file.content,
filename: file.filename,
type: 'message/rfc822',
disposition: 'inline'
)
end
2012-04-10 14:06:46 +00:00
end