Skip to content

Commit

Permalink
[#51274] return correct parent id
Browse files Browse the repository at this point in the history
- https://community.openproject.org/wp/51274
- one drive files query now returns real parent id when folder is empty
  - was a fake id before
- upload needs it for the prepare upload request, fake id led to error
  • Loading branch information
Kharonus committed Nov 28, 2023
1 parent fe20c94 commit 6e65061
Showing 1 changed file with 34 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,28 @@ def call(user:, folder:)
result = Util.using_user_token(@storage, user) do |token|
# Make the Get Request to the necessary endpoints
response = Net::HTTP.start(@uri.host, @uri.port, use_ssl: true) do |http|
http.get(uri_path_for(folder) + FIELDS, { 'Authorization' => "Bearer #{token.access_token}" })
http.get(children_uri_path_for(folder) + FIELDS, { 'Authorization' => "Bearer #{token.access_token}" })
end

handle_response(response)
handle_response(response, :value)
end

if result.result.empty?
empty_response(folder)
empty_response(user, folder)
else
result.map { |json_files| storage_files(json_files) }
end
end

private

def handle_response(response)
def handle_response(response, map_value)
json = MultiJson.load(response.body, symbolize_keys: true)
error_data = ::Storages::StorageErrorData.new(source: self, payload: json)

case response
when Net::HTTPSuccess
ServiceResult.success(result: MultiJson.load(response.body, symbolize_keys: true)[:value])
ServiceResult.success(result: MultiJson.load(response.body, symbolize_keys: true)[map_value])
when Net::HTTPNotFound
ServiceResult.failure(result: :not_found,
errors: ::Storages::StorageError.new(code: :not_found, data: error_data))
Expand Down Expand Up @@ -106,20 +106,28 @@ def storage_file(json_file)
)
end

def empty_response(folder)
path = folder.path

ServiceResult.success(
result: StorageFiles.new(
[],
StorageFile.new(
id: Digest::SHA256.hexdigest(path),
name: path.split('/').last,
location: path,
permissions: %i[readable writeable]
),
forge_ancestors(path:)
)
def empty_response(user, folder)
result = Util.using_user_token(@storage, user) do |token|
response = Net::HTTP.start(@uri.host, @uri.port, use_ssl: true) do |http|
http.get(location_uri_path_for(folder) + FIELDS, { 'Authorization' => "Bearer #{token.access_token}" })
end

handle_response(response, :id)
end

result.map { |parent_location_id| empty_storage_files(folder.path, parent_location_id) }
end

def empty_storage_files(path, parent_id)
StorageFiles.new(
[],
StorageFile.new(
id: parent_id,
name: path.split('/').last,
location: path,
permissions: %i[readable writeable]
),
forge_ancestors(path:)
)
end

Expand Down Expand Up @@ -166,12 +174,18 @@ def root(id)
permissions: %i[readable writeable])
end

def uri_path_for(folder)
def children_uri_path_for(folder)
return "/v1.0/drives/#{@storage.drive_id}/root/children" if folder.root?

"/v1.0/drives/#{@storage.drive_id}/root:#{encode_path(folder.path)}:/children"
end

def location_uri_path_for(folder)
return "/v1.0/drives/#{@storage.drive_id}/root" if folder.root?

"/v1.0/drives/#{@storage.drive_id}/root:#{encode_path(folder.path)}"
end

def encode_path(path)
path.split('/').map { |fragment| URI.encode_uri_component(fragment) }.join('/')
end
Expand Down

0 comments on commit 6e65061

Please sign in to comment.