2012-04-10 14:06:46 +00:00
|
|
|
class TicketArticlesController < ApplicationController
|
|
|
|
before_filter :authentication_check
|
|
|
|
|
|
|
|
# GET /articles
|
|
|
|
def index
|
|
|
|
@articles = Ticket::Article.all
|
|
|
|
|
2012-04-12 11:27:01 +00:00
|
|
|
render :json => @articles
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# GET /articles/1
|
|
|
|
def show
|
2012-11-13 10:34:45 +00:00
|
|
|
@article = Ticket::Article.find( params[:id] )
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2012-04-12 11:27:01 +00:00
|
|
|
render :json => @article
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# POST /articles
|
|
|
|
def create
|
2012-12-02 10:18:55 +00:00
|
|
|
form_id = params[:ticket_article][:form_id]
|
|
|
|
params[:ticket_article].delete(:form_id)
|
|
|
|
@article = Ticket::Article.new( params[:ticket_article] )
|
2012-11-13 10:34:45 +00:00
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
# find attachments in upload cache
|
2012-12-02 10:18:55 +00:00
|
|
|
if form_id
|
2013-03-28 23:13:15 +00:00
|
|
|
@article.attachments = Store.list(
|
2012-12-02 10:18:55 +00:00
|
|
|
:object => 'UploadCache',
|
|
|
|
:o_id => form_id,
|
|
|
|
)
|
|
|
|
end
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2012-04-12 11:27:01 +00:00
|
|
|
if @article.save
|
|
|
|
|
|
|
|
# remove attachments from upload cache
|
|
|
|
Store.remove(
|
2012-12-02 10:18:55 +00:00
|
|
|
:object => 'UploadCache',
|
|
|
|
:o_id => form_id,
|
2012-04-12 11:27:01 +00:00
|
|
|
)
|
2012-11-13 10:34:45 +00:00
|
|
|
|
2012-04-16 12:56:04 +00:00
|
|
|
render :json => @article, :status => :created
|
2012-04-12 11:27:01 +00:00
|
|
|
else
|
|
|
|
render :json => @article.errors, :status => :unprocessable_entity
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# PUT /articles/1
|
|
|
|
def update
|
2012-11-13 10:34:45 +00:00
|
|
|
@article = Ticket::Article.find( params[:id] )
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2012-04-12 11:27:01 +00:00
|
|
|
if @article.update_attributes(params[:ticket_article])
|
|
|
|
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
|
2012-11-13 10:34:45 +00:00
|
|
|
@article = Ticket::Article.find( params[:id] )
|
2012-04-10 14:06:46 +00:00
|
|
|
@article.destroy
|
|
|
|
|
2012-04-12 11:27:01 +00:00
|
|
|
head :ok
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2012-09-20 12:08:02 +00:00
|
|
|
|
|
|
|
# POST /ticket_attachment/new
|
|
|
|
def attachment_new
|
|
|
|
|
|
|
|
# store file
|
|
|
|
# content_type = request.content_type
|
|
|
|
content_type = request[:content_type]
|
|
|
|
puts 'content_type: ' + content_type.inspect
|
|
|
|
if !content_type || content_type == 'application/octet-stream'
|
|
|
|
if MIME::Types.type_for(params[:qqfile]).first
|
|
|
|
content_type = MIME::Types.type_for(params[:qqfile]).first.content_type
|
|
|
|
else
|
|
|
|
content_type = 'application/octet-stream'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
headers_store = {
|
|
|
|
'Content-Type' => content_type
|
|
|
|
}
|
|
|
|
Store.add(
|
2012-12-02 10:18:55 +00:00
|
|
|
:object => 'UploadCache',
|
2012-09-20 12:08:02 +00:00
|
|
|
:o_id => params[:form_id],
|
|
|
|
:data => request.body.read,
|
|
|
|
:filename => params[:qqfile],
|
|
|
|
:preferences => headers_store
|
|
|
|
)
|
|
|
|
|
|
|
|
# return result
|
|
|
|
render :json => {
|
|
|
|
:success => true,
|
|
|
|
}
|
|
|
|
end
|
2012-12-02 10:18:55 +00:00
|
|
|
|
2012-09-20 12:08:02 +00:00
|
|
|
# GET /ticket_attachment/1
|
|
|
|
def attachment
|
|
|
|
|
|
|
|
# permissin check
|
|
|
|
ticket = Ticket.find( params[:ticket_id] )
|
|
|
|
if !ticket_permission(ticket)
|
|
|
|
render( :json => 'No such ticket.', :status => :unauthorized )
|
|
|
|
return
|
|
|
|
end
|
|
|
|
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 = Store.list( :object => 'Ticket::Article', :o_id => params[:article_id] ) || []
|
|
|
|
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(
|
|
|
|
file.store_file.data,
|
|
|
|
:filename => file.filename,
|
|
|
|
:type => file.preferences['Content-Type'] || file.preferences['Mime-Type'],
|
|
|
|
:disposition => 'inline'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
# GET /ticket_article_plain/1
|
|
|
|
def article_plain
|
|
|
|
|
|
|
|
# permissin check
|
|
|
|
article = Ticket::Article.find( params[:id] )
|
|
|
|
return if !ticket_permission( article.ticket )
|
|
|
|
|
|
|
|
list = Store.list(
|
|
|
|
:object => 'Ticket::Article::Mail',
|
|
|
|
:o_id => params[:id],
|
|
|
|
)
|
|
|
|
|
|
|
|
# find file
|
|
|
|
if list
|
|
|
|
file = Store.find(list.first)
|
|
|
|
send_data(
|
|
|
|
file.store_file.data,
|
|
|
|
:filename => file.filename,
|
|
|
|
:type => 'message/rfc822',
|
|
|
|
:disposition => 'inline'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|