Skip to content

Commit

Permalink
oni api updates
Browse files Browse the repository at this point in the history
  • Loading branch information
johnf committed Dec 17, 2024
1 parent b9a7d6b commit e5ad3a6
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 57 deletions.
34 changes: 31 additions & 3 deletions app/controllers/api/v1/oni_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Api
module V1
class OniController < ApplicationController
def objects
def entities
query = Oni::ObjectsValidator.new(params)

render json: { errors: query.errors.full_messages }, status: :unprocessable_entity unless query.valid?
Expand Down Expand Up @@ -82,7 +82,7 @@ def objects
.group('items.id')
.includes(:collection, :access_condition, :content_languages)

@objects = ids.map do |id|
@entities = ids.map do |id|
if id['type'] == 'collection'
collections.find { |c| c.id == id['id'] }
else
Expand All @@ -91,7 +91,7 @@ def objects
end
end

def object_meta
def entity
unless params[:id]
render json: { error: 'id is required' }, status: :bad_request

Expand All @@ -118,6 +118,34 @@ def object_meta
raise ActiveRecord::RecordNotFound
end

def file
unless params[:id]
render json: { error: 'id is required' }, status: :bad_request

return
end

unless params[:path]
render json: { error: 'path is required' }, status: :bad_request

return
end

as_attachment = params[:disposition] == 'attachment'
filename = params[:filename]

raise ActiveRecord::RecordNotFound unless check_for_item

essence = @data.essences.find_by(filename: params[:path])

raise ActiveRecord::RecordNotFound unless essence

location = Nabu::Catalog.instance.essence_url(essence, as_attachment:, filename:)
raise ActionController::RoutingError, 'Essence file not found' unless location

redirect_to location, allow_other_host: true
end

private
def check_for_essence
md = params[:id].match(repository_essence_url(collection_identifier: '(.*)', item_identifier: '(.*)', essence_filename: '(.*)'))
Expand Down
2 changes: 1 addition & 1 deletion app/validators/oni/objects_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ObjectsValidator
validates :offset, numericality: { only_integer: true, greater_than_or_equal_to: 0 }, allow_nil: true

def initialize(params)
object_params = params.permit(*ATTRIBUTES.map { | attr| attr.to_s.camelize(:lower).to_sym }).except(:format)
object_params = params.permit(*ATTRIBUTES.map { | attr| attr.to_s.camelize(:lower).to_sym })
object_params.each do |key, value|
snake_key = key.to_s.underscore

Expand Down
45 changes: 45 additions & 0 deletions app/views/api/v1/oni/entities.json.jb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
total: @total,

entities: @entities.map do |entity|
class_name = entity.class.name
is_item = class_name == 'Item'

response = {
id: is_item ? repository_item_url(entity.collection, entity) : repository_collection_url(entity),
name: entity.title,
description: entity.description.truncate(256),
conformsTo: "https://w3id.org/ldac/profile##{is_item ? 'Object' : 'Collection'}",
recordType: ['Data', 'Object', is_item ? 'RepositoryObject' : 'RepositoryCollection'],
extra: {
language: (is_item ? entity.content_languages : entity.languages).map(&:name)
}
}

if is_item
response[:memberOf] = repository_collection_url(entity.collection)
response[:root] = repository_collection_url(entity.collection)
end

extra = response[:extra]

extra[:objectCount] = entity.items_count if entity.has_attribute?(:items_count)
extra[:fileCount] = entity.essences_count if entity.has_attribute?(:essences_count)

if entity.private?
extra[:accessControl] = 'AccessControlList'
elsif entity.access_condition.nil?
extra[:accessControl] = 'AuthorizationByInvitation'
elsif entity.access_condition.name == 'Open (subject to agreeing to PDSC access conditions)'
extra[:accessControl] = 'AgreeToTerms'
else
extra[:accessControl] = 'Public'
end

# TODO:: add communicationMode

extra[:mediaType] = @mime_types

response
end
}
2 changes: 1 addition & 1 deletion app/views/api/v1/oni/object_meta_item.json.jb
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ data[:license] = { '@id': access_condition_json(@data.access_condition) } if @da
data[:operator] = { '@id': person_json(@data.operator) } if @data.operator
data[:sourceOrganization] = organisation_json(@data.university) if @data.university

data[:memberOf] = { '@id': repository_collection_url(@data.collection) }
data[:memberOf] = { '@id': repository_collection_url(@data.collection), name: @data.collection.title }
data[:root] = { '@id': repository_collection_url(@data.collection) }
data[:identifier] << property_value_json('collectionIdentifier', @data.collection.identifier)
data[:languageAsGiven] = @data.language
Expand Down
45 changes: 0 additions & 45 deletions app/views/api/v1/oni/objects.json.jb

This file was deleted.

6 changes: 3 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@
namespace :v1 do
post '/graphql', to: 'graphql#execute'
scope '/oni', as: 'oni', defaults: { format: 'json' } do
get 'objects' => 'oni#objects'
get 'object' => 'oni#object'
get 'object/meta' => 'oni#object_meta'
get 'entities' => 'oni#entities'
get 'entity/:id' => 'oni#entity'
get 'enttity/:id/file/:path' => 'oni#file'
get 'stream' => 'oni#stream'
end
end
Expand Down
14 changes: 10 additions & 4 deletions lib/nabu/catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ def item_admin_url(item, filename)
download(parts.join('/'))
end

def essence_url(essence, as_attachment: false)
def essence_url(essence, as_attachment: false, filename:)
Rails.logger.debug { "Nabu::Catalog: Get essence URL #{essence.item.full_identifier}:#{essence.filename}" }
parts = [essence.item.collection.identifier, essence.item.identifier, essence.filename]

download(parts.join('/'), as_attachment:)
download(parts.join('/'), as_attachment:, filename:)
end

def deposit_form_url(collection, as_attachment: false)
Expand All @@ -103,12 +103,18 @@ def upload(key, data, content_type)
)
end

def download(key, as_attachment: false)
def download(key, as_attachment: false, filename:)
disposition = nil
if as_attachment
disposition = 'attachment'
disposition += "; filename=\"#{filename}\"" if filename
end

@presigner.presigned_url(
:get_object,
bucket: bucket_name,
key:,
response_content_disposition: as_attachment ? 'attachment' : nil
response_content_disposition: disposition
)
end

Expand Down

0 comments on commit e5ad3a6

Please sign in to comment.