add folder-direct file and folder creation apis fixes #9645, #9646

test plan:
 * create files and folders with only a parent folder id (no context)

Change-Id: I76420c4481e1ddd4a201f281a5b8dd83feb09dd2
Reviewed-on: https://gerrit.instructure.com/12554
Reviewed-by: Bracken Mosbacker <bracken@instructure.com>
Tested-by: Jenkins <jenkins@instructure.com>
This commit is contained in:
Cody Cutrer 2012-07-27 13:23:01 -06:00
parent f8e02c5089
commit f7d8a0b992
3 changed files with 98 additions and 9 deletions

View File

@ -40,8 +40,9 @@
# }
class FoldersController < ApplicationController
include Api::V1::Folders
include Api::V1::Attachment
before_filter :require_context, :except => [:api_index, :show, :api_destroy, :update]
before_filter :require_context, :except => [:api_index, :show, :api_destroy, :update, :create, :create_file]
def index
if authorized_action(@context, @current_user, :read)
@ -268,20 +269,36 @@ class FoldersController < ApplicationController
#
# @example_request
#
# curl 'https://<canvas>/api/v1/files/<file_id>' \
# -F 'name=<new_name>' \
# -F 'locked=true' \
# curl 'https://<canvas>/api/v1/folders/<folder_id>/folders' \
# -F 'name=<new_name>' \
# -F 'locked=true' \
# -H 'Authorization: Bearer <token>'
#
#
# @example_request
#
# curl 'https://<canvas>/api/v1/courses/<course_id>/folders' \
# -F 'name=<new_name>' \
# -F 'locked=true' \
# -H 'Authorization: Bearer <token>'
#
# @returns Folder
def create
folder_params = process_folder_params(params, api_request?)
source_folder_id = folder_params.delete(:source_folder_id)
if folder_params[:folder_id]
parent_folder = Folder.find(folder_params[:folder_id])
@context = parent_folder.context
else
require_context
end
if folder_params[:parent_folder_path] && folder_params[:parent_folder_id]
if (folder_params[:folder_id] && (folder_params[:parent_folder_path] || folder_params[:parent_folder_id])) ||
(folder_params[:parent_folder_path] && folder_params[:parent_folder_id])
render :json => {:message => t('only_one_folder', "Can't set folder path and folder id")}, :status => 400
return
elsif folder_params[:folder_id]
folder_params.delete(:folder_id)
elsif folder_params[:parent_folder_id]
parent_folder = @context.folders.find(folder_params.delete(:parent_folder_id))
elsif @context.respond_to?(:folders) && folder_params[:parent_folder_path].is_a?(String)
@ -321,7 +338,7 @@ class FoldersController < ApplicationController
def process_folder_params(parameters, api_request)
folder_params = (api_request ? parameters : parameters[:folder]) || {}
folder_params.slice(:name, :parent_folder_id, :parent_folder_path,
folder_params.slice(:name, :parent_folder_id, :parent_folder_path, :folder_id,
:source_folder_id, :lock_at, :unlock_at, :locked,
:hidden, :context, :position, :just_hide)
end
@ -363,5 +380,24 @@ class FoldersController < ApplicationController
end
end
end
# @API Upload a file
#
# Upload a file to a folder.
#
# This API endpoint is the first step in uploading a file.
# See the {file:file_uploads.html File Upload Documentation} for details on
# the file upload workflow.
#
# Only those with the "Manage Files" permission on a course or group can
# upload files to a folder in that course or group.
def create_file
@folder = Folder.find(params[:folder_id])
params[:parent_folder_id] = @folder.id
@context = @folder.context
@attachment = Attachment.new(:context => @context)
if authorized_action(@attachment, @current_user, :create)
api_attachment_preflight(@context, request, :check_quota => true)
end
end
end

View File

@ -931,6 +931,8 @@ ActionController::Routing::Routes.draw do |map|
folders.get 'folders/:id/files', :controller => :files, :action => :api_index, :path_name => 'list_files'
folders.delete 'folders/:id', :action => :api_destroy
folders.put 'folders/:id', :action => :update
folders.post 'folders/:folder_id/folders', :action => :create, :path_name => 'create_folder'
folders.post 'folders/:folder_id/files', :action => :create_file
end
api.with_options(:controller => :favorites) do |favorites|

View File

@ -246,6 +246,46 @@ describe "Folders API", :type => :integration do
sub1.locked.should == true
end
it "should create by folder id in the path" do
group_model(:context => @course)
@root = Folder.root_folders(@group).first
@f1 = @root.sub_folders.create!(:name => "folder1", :context => @group)
json = api_call(:post, "/api/v1/folders/#{@f1.id}/folders",
@folders_path_options.merge(:folder_id => @f1.id.to_param),
{ :name => "sub1", :locked => 'true' }, {})
@f1.reload
sub1 = @f1.sub_folders.first
sub1.name.should == 'sub1'
sub1.locked.should == true
end
it "should error with invalid folder id" do
api_call(:post, "/api/v1/folders/0/folders",
@folders_path_options.merge(:folder_id => "0"),
{:name => "sub1", :locked => 'true'},
{},
:expected_status => 404)
end
it "should give error folder is used and path sent" do
json = api_call(:post, "/api/v1/folders/#{@root.id}/folders",
@folders_path_options.merge(:folder_id => @root.id.to_param),
{ :name => "sub1", :locked => 'true', :parent_folder_path => 'haha/fool'},
{},
:expected_status => 400)
json['message'].should == "Can't set folder path and folder id"
end
it "should give error folder is used and id sent" do
json = api_call(:post, "/api/v1/folders/#{@root.id}/folders",
@folders_path_options.merge(:folder_id => @root.id.to_param),
{ :name => "sub1", :locked => 'true', :parent_folder_id => @root.id.to_param},
{},
:expected_status => 400)
json['message'].should == "Can't set folder path and folder id"
end
it "should create by folder path" do
json = api_call(:post, "/api/v1/courses/#{@course.id}/folders",
@folders_path_options.merge(:course_id => @course.id.to_param),
@ -318,5 +358,16 @@ describe "Folders API", :type => :integration do
end
end
describe "#create_file" do
it "should create a file in the correct folder" do
@context = course_with_teacher
@user = @teacher
@root_folder = Folder.root_folders(@course).first
api_call(:post, "/api/v1/folders/#{@root_folder.id}/files",
{ :controller => "folders", :action => "create_file", :format => "json", :folder_id => @root_folder.id.to_param, },
:name => "with_path.txt")
attachment = Attachment.last(:order => :id)
attachment.folder_id.should == @root_folder.id
end
end
end