-
Notifications
You must be signed in to change notification settings - Fork 1
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 #10 from performant-software/feature/iiif5_resources
IIIF #5 - Resources
- Loading branch information
Showing
57 changed files
with
2,924 additions
and
89 deletions.
There are no files selected for viewing
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
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,42 @@ | ||
class Api::ResourcesController < Api::BaseController | ||
# Includes | ||
include Api::Uploadable | ||
|
||
# Search attributes | ||
search_attributes :name | ||
|
||
# Preloads | ||
preloads Resource.attachment_preloads | ||
|
||
# Actions | ||
before_action :validate_new_resource, unless: -> { current_user.admin? }, only: :create | ||
before_action :validate_resource, unless: -> { current_user.admin? }, only: [:update, :destroy] | ||
|
||
protected | ||
|
||
def base_query | ||
subquery = Project.where(Project.arel_table[:id].eq(Resource.arel_table[:project_id])) | ||
|
||
if !current_user.admin? | ||
subquery = subquery | ||
.joins(organization: :user_organizations) | ||
.where(user_organizations: { user_id: current_user.id }) | ||
end | ||
|
||
subquery = subquery.where(id: params[:project_id]) if params[:project_id].present? | ||
|
||
Resource.where(subquery.arel.exists) | ||
end | ||
|
||
private | ||
|
||
def validate_new_resource | ||
project = Project.find(params[:resource][:project_id]) | ||
check_authorization project.organization_id | ||
end | ||
|
||
def validate_resource | ||
resource = Resource.find(params[:id]) | ||
check_authorization resource.project.organization_id | ||
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
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,115 @@ | ||
class Resource < ApplicationRecord | ||
# Includes | ||
include Attachable | ||
|
||
# Relationships | ||
belongs_to :project | ||
|
||
# Resourceable parameters | ||
allow_params :project_id, :name, :metadata, :content | ||
|
||
# Callbacks | ||
before_create :set_uuid | ||
after_create_commit :after_create | ||
|
||
# ActiveStorage | ||
has_one_attached :content | ||
has_one_attached :content_converted | ||
|
||
# Delegates | ||
delegate :audio?, to: :content | ||
delegate :image?, to: :content | ||
delegate :video?, to: :content | ||
|
||
# Validations | ||
validate :validate_metadata | ||
|
||
# Overload attachable methods | ||
alias_method :attachable_content_base_url, :content_base_url | ||
|
||
def content_base_url | ||
return attachable_content_base_url unless content_converted.attached? | ||
|
||
"#{ENV['IIIF_HOST']}/iiif/3/#{content_converted.key}" | ||
end | ||
|
||
def content_type | ||
return content.content_type unless content_converted.attached? | ||
|
||
content_converted.content_type | ||
end | ||
|
||
def pdf? | ||
content.content_type == 'application/pdf' | ||
end | ||
|
||
protected | ||
|
||
def after_create | ||
# Convert the image to a TIFF | ||
convert | ||
|
||
# Create the manifest | ||
create_manifest | ||
|
||
# Extract EXIF data | ||
extract_exif | ||
end | ||
|
||
private | ||
|
||
def convert | ||
return unless image? && content.attached? | ||
|
||
content.open do |file| | ||
filepath = Images::Convert.to_tiff(file) | ||
filename = Images::Convert.filename(content.filename.to_s, 'tif') | ||
|
||
content_converted.attach( | ||
io: File.open(filepath), | ||
filename: filename, | ||
content_type: 'image/tiff' | ||
) | ||
end | ||
end | ||
|
||
def create_manifest | ||
self.manifest = Iiif::Manifest.create(self) | ||
save | ||
end | ||
|
||
|
||
def extract_exif | ||
return unless content.attached? && content.image? | ||
|
||
content.open do |file| | ||
data = Images::Exif.extract(file) | ||
|
||
unless data.nil? | ||
self.exif = JSON.dump(data) | ||
save | ||
end | ||
end | ||
end | ||
|
||
def set_uuid | ||
self.uuid = SecureRandom.uuid | ||
end | ||
|
||
def validate_metadata | ||
items = JSON.parse(project.metadata || '[]') | ||
return if items.nil? || items.empty? | ||
|
||
values = JSON.parse(metadata || '{}') | ||
|
||
items.each do |item| | ||
required = item['required'].to_s.to_bool | ||
name = item['name'] | ||
value = values[name] | ||
|
||
if required && (value.nil? || value.empty?) | ||
errors.add("metadata[#{name}]", I18n.t('errors.resource.metadata.required', name: name)) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
class ProjectsSerializer < BaseSerializer | ||
index_attributes :id, :uid, :name, :description, :avatar_url, :organization_id, organization: OrganizationsSerializer | ||
show_attributes :id, :uid, :name, :description, :api_key, :avatar_url, :organization_id, organization: OrganizationsSerializer | ||
index_attributes :id, :uid, :name, :description, :avatar_thumbnail_url, :organization_id, organization: OrganizationsSerializer | ||
show_attributes :id, :uid, :name, :description, :api_key, :avatar_url, :avatar_preview_url, :metadata, :organization_id, organization: OrganizationsSerializer | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class ResourcesSerializer < BaseSerializer | ||
index_attributes :id, :name, :metadata, :exif, :project_id, :content_thumbnail_url, :content_type | ||
show_attributes :id, :name, :metadata, :exif, :project_id, :content_url, :content_preview_url, :content_download_url, :manifest, :content_type | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
class UsersSerializer < BaseSerializer | ||
index_attributes :id, :name, :email, :admin, :avatar_url | ||
show_attributes :id, :name, :email, :admin, :avatar_url, user_organizations: [:id, :organization_id, organization: OrganizationsSerializer] | ||
index_attributes :id, :name, :email, :admin, :avatar_thumbnail_url | ||
show_attributes :id, :name, :email, :admin, :avatar_url, :avatar_preview_url, user_organizations: [:id, :organization_id, organization: OrganizationsSerializer] | ||
end |
Oops, something went wrong.