Skip to content

Commit

Permalink
Merge branch 'main' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
sinaeftekhar committed Jan 3, 2025
2 parents b5aa237 + 110199b commit 17a0f5f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
6 changes: 5 additions & 1 deletion app/controllers/decidim/apifiles/blobs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ module Apifiles
# GraphQL API (e.g. by base64 encoding the whole file which may cause issues
# when the file sizes become larger).
class BlobsController < Decidim::Api::ApplicationController
include Rails.application.routes.url_helpers

register_permissions(
::Decidim::Apifiles::BlobsController,
::Decidim::Apifiles::Permissions,
Expand All @@ -33,7 +35,9 @@ def create
content_type: uploaded_file.content_type
)

render json: blob.as_json(methods: :signed_id).transform_keys { |key| key.camelize(:lower) }
render json: blob.as_json(methods: :signed_id)
.merge("src" => Rails.application.routes.url_helpers.rails_blob_path(blob, only_path: true))
.transform_keys { |key| key.camelize(:lower) }
end

private
Expand Down
5 changes: 2 additions & 3 deletions lib/decidim/api/blob_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ def self.authorized?(object, context)
field :checksum, GraphQL::Types::String, "The checksum of this blob", null: false
field :created_at, Decidim::Core::DateTimeType, "When this blob was created", null: true
field :service_name, GraphQL::Types::String, "The service name of this blob (where the blob is stored at)", null: false
field :src, GraphQL::Types::String, "The url of this blob", null: false

field :url, GraphQL::Types::String, "The url of this blob", null: false

def url
def src
asset_routes.rails_blob_url(object, **default_url_options)
end

Expand Down
14 changes: 11 additions & 3 deletions lib/decidim/apifiles/query_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,20 @@ module QueryExtensions
# Returns nothing.
def self.included(type)
type.field :blob, BlobType, null: true, description: "Finds a blob" do
argument :id, GraphQL::Types::ID, description: "The ID of the blob", required: true
argument :id, GraphQL::Types::ID, required: false, description: "The ID of the blob"
argument :signed_id, GraphQL::Types::String, required: false, description: "The signed ID of the blob"
end
end

def blob(id:)
ActiveStorage::Blob.find_by(id:)
# Retrieves a blob by ID or signed ID
def blob(id: nil, signed_id: nil)
if id
ActiveStorage::Blob.find_by(id: id)
elsif signed_id
ActiveStorage::Blob.find_signed(signed_id)
else
raise GraphQL::ExecutionError, "Either 'id' or 'signedId' must be provided."
end
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/types/decidim/apifiles/blob_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@
end
end

describe "url" do
let(:query) { "{ url }" }
describe "src" do
let(:query) { "{ src }" }

let(:host) { "http://#{current_organization.host}:#{Capybara.server_port}" }

it "returns the url" do
expect(response).to include("url" => "#{host}/rails/active_storage/blobs/redirect/#{model.signed_id}/#{model.filename}")
it "returns the src" do
expect(response).to include("src" => "#{host}/rails/active_storage/blobs/redirect/#{model.signed_id}/#{model.filename}")
end
end
end
Expand Down

0 comments on commit 17a0f5f

Please sign in to comment.