-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17375 from opf/impl/59900-storages-new-folder-end…
…point Storages new folder API endpoint
- Loading branch information
Showing
10 changed files
with
496 additions
and
0 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
docs/api/apiv3/components/examples/storage-create-folder-request-body.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
description: |- | ||
A valid request body to create a new folder on a external storage | ||
value: | ||
name: Uploads | ||
parentId: "200" |
13 changes: 13 additions & 0 deletions
13
docs/api/apiv3/components/schemas/storage_folder_write_model.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Schema: StorageFolderWriteModel | ||
--- | ||
type: object | ||
required: | ||
- name | ||
- parentId | ||
properties: | ||
name: | ||
type: string | ||
description: Name of the folder to be created | ||
parentId: | ||
type: string | ||
description: Unique identifier of the parent folder in which the new folder should be created in |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# /api/v3/storages/{id}/folders | ||
--- | ||
post: | ||
summary: Creation of a new folder | ||
operationId: create_storage_folder | ||
tags: | ||
- File links | ||
description: Creates a new folder under the given parent | ||
parameters: | ||
- name: id | ||
description: Storage id | ||
in: path | ||
required: true | ||
schema: | ||
type: integer | ||
example: 1337 | ||
requestBody: | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '../components/schemas/storage_folder_write_model.yml' | ||
examples: | ||
'Valid example': | ||
$ref: '../components/examples/storage-create-folder-request-body.yml' | ||
responses: | ||
'201': | ||
description: Created | ||
content: | ||
application/hal+json: | ||
schema: | ||
$ref: '../components/schemas/storage_file_model.yml' | ||
'400': | ||
content: | ||
application/hal+json: | ||
schema: | ||
$ref: '../components/schemas/error_response.yml' | ||
example: | ||
_type: Error | ||
errorIdentifier: urn:openproject-org:api:v3:errors:InvalidQuery | ||
message: The given parent is not a directory. | ||
description: |- | ||
Returned if the request is missing a required parameter. | ||
'403': | ||
content: | ||
application/hal+json: | ||
schema: | ||
$ref: '../components/schemas/error_response.yml' | ||
example: | ||
_type: Error | ||
errorIdentifier: urn:openproject-org:api:v3:errors:MissingPermission | ||
message: You are not authorized to access this resource. | ||
description: |- | ||
Returned if the client does not have sufficient permissions. | ||
**Required permission:** manage file links | ||
'404': | ||
content: | ||
application/hal+json: | ||
schema: | ||
$ref: '../components/schemas/error_response.yml' | ||
example: | ||
_type: Error | ||
errorIdentifier: urn:openproject-org:api:v3:errors:NotFound | ||
message: The requested resource could not be found. | ||
description: |- | ||
Returned if the storage does not exist or the client does not have sufficient permissions to see it. | ||
**Required permission:** view file links |
77 changes: 77 additions & 0 deletions
77
modules/storages/app/services/storages/create_folder_service.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# frozen_string_literal: true | ||
|
||
#-- copyright | ||
# OpenProject is an open source project management software. | ||
# Copyright (C) the OpenProject GmbH | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU General Public License version 3. | ||
# | ||
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: | ||
# Copyright (C) 2006-2013 Jean-Philippe Lang | ||
# Copyright (C) 2010-2013 the ChiliProject Team | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU General Public License | ||
# as published by the Free Software Foundation; either version 2 | ||
# of the License, or (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program; if not, write to the Free Software | ||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
# | ||
# See COPYRIGHT and LICENSE files for more details. | ||
#++ | ||
|
||
module Storages | ||
class CreateFolderService < BaseService | ||
using Peripherals::ServiceResultRefinements | ||
|
||
def self.call(storage:, user:, name:, parent_id:) | ||
new.call(storage:, user:, name:, parent_id:) | ||
end | ||
|
||
def call(storage:, user:, name:, parent_id:) | ||
auth_strategy = Peripherals::Registry.resolve("#{storage}.authentication.user_bound").call(user: user) | ||
|
||
Peripherals::Registry | ||
.resolve("#{storage}.commands.create_folder") | ||
.call( | ||
storage:, | ||
auth_strategy:, | ||
folder_name: name, | ||
parent_location: parent_path(parent_id, storage, user) | ||
) | ||
end | ||
|
||
private | ||
|
||
def parent_path(parent_id, storage, user) | ||
case storage.short_provider_type | ||
when "nextcloud" | ||
location_from_file_info(parent_id, storage, user) | ||
when "one_drive" | ||
Peripherals::ParentFolder.new(parent_id) | ||
else | ||
raise "Unknown Storage Type" | ||
end | ||
end | ||
|
||
def location_from_file_info(parent_id, storage, user) | ||
StorageFileService | ||
.call(storage: storage, user: user, file_id: parent_id) | ||
.match( | ||
on_success: lambda { |folder_info| | ||
path = URI.decode_uri_component(folder_info.location) | ||
Peripherals::ParentFolder.new(path) | ||
}, | ||
on_failure: ->(error) { raise error } | ||
) | ||
end | ||
end | ||
end |
66 changes: 66 additions & 0 deletions
66
modules/storages/lib/api/v3/storage_files/storage_folders_api.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# frozen_string_literal: true | ||
|
||
#-- copyright | ||
# OpenProject is an open source project management software. | ||
# Copyright (C) the OpenProject GmbH | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU General Public License version 3. | ||
# | ||
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: | ||
# Copyright (C) 2006-2013 Jean-Philippe Lang | ||
# Copyright (C) 2010-2013 the ChiliProject Team | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU General Public License | ||
# as published by the Free Software Foundation; either version 2 | ||
# of the License, or (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program; if not, write to the Free Software | ||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
# | ||
# See COPYRIGHT and LICENSE files for more details. | ||
#++ | ||
|
||
module API | ||
module V3 | ||
module StorageFiles | ||
class StorageFoldersAPI < ::API::OpenProjectAPI | ||
using ::Storages::Peripherals::ServiceResultRefinements | ||
|
||
helpers ::Storages::Peripherals::StorageErrorHelper | ||
|
||
resources :folders do | ||
params do | ||
requires :name, type: String, desc: "Folder name" | ||
requires :parent_id, type: String, desc: "Id of the parent folder" | ||
end | ||
|
||
post do | ||
::Storages::CreateFolderService.call( | ||
storage: @storage, | ||
user: current_user, | ||
name: params["name"], | ||
parent_id: params["parent_id"] | ||
).match( | ||
on_success: lambda { |storage_folder| | ||
API::V3::StorageFiles::StorageFileRepresenter.new( | ||
storage_folder, | ||
@storage, | ||
current_user: | ||
) | ||
}, | ||
on_failure: ->(error) { raise_error(error) } | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.