-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
213 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
class ApplicationController < ActionController::Base | ||
protect_from_forgery with: :exception | ||
skip_before_action :verify_authenticity_token | ||
class ApplicationController < ActionController::API | ||
include ActionController::MimeResponds | ||
|
||
protected | ||
|
||
def strip_druid(id) | ||
id&.split(':', 2)&.last | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'csv' | ||
|
||
## | ||
# ObjectsController allows consumers to interact with preserved objects | ||
# (Note: methods will eventually be ported from sdr-services-app) | ||
class ObjectsController < ApplicationController | ||
# return the PreservedObject model for the druid (supplied with druid: prefix) | ||
# GET /objects/:druid | ||
def show | ||
object = PreservedObject.find_by(druid: strip_druid(params[:id])) | ||
if object | ||
render json: object.to_json | ||
else | ||
render status: 404, json: 'Object not found' | ||
end | ||
end | ||
|
||
# return the checksums and filesize for a single druid (supplied with druid: prefix) | ||
# GET /objects/:druid/checksum | ||
def checksum | ||
render json: checksum_for_object(params[:id]).to_json | ||
rescue Moab::ObjectNotFoundException => e | ||
render status: 404, json: e.message | ||
rescue StandardError => e | ||
render status: 500, json: e.message | ||
end | ||
|
||
# return the checksums and filesize for a list of druid (supplied with druid: prefix) | ||
# GET /objects/checksums?druids=druida,druidb,druidc | ||
def checksums | ||
druids = params[:druids] | ||
respond_to do |format| | ||
format.json do | ||
results = druids.map { |druid| { "#{druid}": checksum_for_object(druid) } } | ||
render json: results.to_json | ||
end | ||
format.csv do | ||
results = CSV.generate do |csv| | ||
druids.each do |druid| | ||
checksum_for_object(druid).each do |checksum| | ||
csv << [druid, checksum[:filename], checksum[:md5], checksum[:sha1], checksum[:sha256], checksum[:filesize]] | ||
end | ||
end | ||
end | ||
render plain: results | ||
end | ||
format.any { render status: 406, plain: 'Format not acceptable' } | ||
end | ||
rescue Moab::ObjectNotFoundException => e | ||
render status: 404, json: e.message | ||
rescue StandardError => e | ||
render status: 500, json: e.message | ||
end | ||
|
||
private | ||
|
||
def checksum_for_object(druid) | ||
content_group = Moab::StorageServices.retrieve_file_group('content', druid) | ||
content_group.path_hash.map do |file, signature| | ||
{ filename: file, md5: signature.md5, sha1: signature.sha1, sha256: signature.sha256, filesize: signature.size } | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
require 'csv' | ||
RSpec.describe ObjectsController, type: :request do | ||
let(:prefixed_druid) { 'druid:bj102hs9687' } | ||
let(:prefixed_druid2) { 'druid:bz514sm9647' } | ||
let(:bare_druid) { 'bj102hs9687' } | ||
let(:pres_obj) { create(:preserved_object) } | ||
|
||
describe 'GET #show' do | ||
context 'when object found' do | ||
it 'response contains the object' do | ||
get object_url "druid:#{pres_obj.druid}", format: :json | ||
expect(response.body).to include(pres_obj.to_json) | ||
expect(response).to have_http_status(:ok) | ||
end | ||
end | ||
|
||
context 'when object not found' do | ||
it 'returns a 404 response code' do | ||
get object_url 'druid:garbage', format: :json | ||
expect(response).to have_http_status(:not_found) | ||
end | ||
end | ||
end | ||
|
||
describe 'GET #checksum' do | ||
context 'when object found' do | ||
it 'response contains the object checksum' do | ||
get checksum_object_url prefixed_druid, format: :json | ||
expected_response = [ | ||
{ filename: 'eric-smith-dissertation.pdf', | ||
md5: 'aead2f6f734355c59af2d5b2689e4fb3', | ||
sha1: '22dc6464e25dc9a7d600b1de6e3848bf63970595', | ||
sha256: 'e49957d53fb2a46e3652f4d399bd14d019600cf496b98d11ebcdf2d10a8ffd2f', | ||
filesize: 1_000_217 }, | ||
{ filename: 'eric-smith-dissertation-augmented.pdf', | ||
md5: '93802f1a639bc9215c6336ff5575ee22', | ||
sha1: '32f7129a81830004f0360424525f066972865221', | ||
sha256: 'a67276820853ddd839ba614133f1acd7330ece13f1082315d40219bed10009de', | ||
filesize: 905_566 } | ||
] | ||
expect(response).to have_http_status(:ok) | ||
expect(response.body).to eq(expected_response.to_json) | ||
end | ||
end | ||
|
||
context 'when object not found' do | ||
it 'returns a 404 response code' do | ||
get checksum_object_url 'druid:xx123yy9999', format: :json | ||
expect(response).to have_http_status(:not_found) | ||
end | ||
end | ||
|
||
context 'when bad parameter passed in' do | ||
it 'returns a 500 response code' do | ||
get checksum_object_url 'not a druid', format: :json | ||
expect(response).to have_http_status(:internal_server_error) | ||
end | ||
end | ||
end | ||
|
||
describe 'GET #checksums' do | ||
context 'when objects found' do | ||
it 'json response contains multiple object checksums' do | ||
get checksums_objects_url, params: { druids: [prefixed_druid, prefixed_druid2], format: :json } | ||
expected_response = [ | ||
{ "#{prefixed_druid}": | ||
[{ filename: 'eric-smith-dissertation.pdf', | ||
md5: 'aead2f6f734355c59af2d5b2689e4fb3', | ||
sha1: '22dc6464e25dc9a7d600b1de6e3848bf63970595', | ||
sha256: 'e49957d53fb2a46e3652f4d399bd14d019600cf496b98d11ebcdf2d10a8ffd2f', | ||
filesize: 1_000_217 }, | ||
{ filename: 'eric-smith-dissertation-augmented.pdf', | ||
md5: '93802f1a639bc9215c6336ff5575ee22', | ||
sha1: '32f7129a81830004f0360424525f066972865221', | ||
sha256: 'a67276820853ddd839ba614133f1acd7330ece13f1082315d40219bed10009de', | ||
filesize: 905_566 }] }, | ||
{ "#{prefixed_druid2}": | ||
[{ filename: 'SC1258_FUR_032a.jpg', | ||
md5: '42e9d4c0a766f837e5a2f5610d9f258e', | ||
sha1: '5bfc6052b0e458e0aa703a0a6853bb9c112e0695', | ||
sha256: '1530df24086afefd71bf7e5b7e85bb350b6972c838bf6c87ddd5c556b800c802', | ||
filesize: 167_784 }] } | ||
] | ||
expect(response).to have_http_status(:ok) | ||
expect(response.body).to eq(expected_response.to_json) | ||
end | ||
|
||
it 'csv response contains multiple object checksums' do | ||
get checksums_objects_url, params: { druids: [prefixed_druid, prefixed_druid2], format: :csv } | ||
expected_response = CSV.generate do |csv| | ||
csv << [prefixed_druid, 'eric-smith-dissertation.pdf', 'aead2f6f734355c59af2d5b2689e4fb3', | ||
'22dc6464e25dc9a7d600b1de6e3848bf63970595', 'e49957d53fb2a46e3652f4d399bd14d019600cf496b98d11ebcdf2d10a8ffd2f', '1000217'] | ||
csv << [prefixed_druid, 'eric-smith-dissertation-augmented.pdf', '93802f1a639bc9215c6336ff5575ee22', | ||
'32f7129a81830004f0360424525f066972865221', 'a67276820853ddd839ba614133f1acd7330ece13f1082315d40219bed10009de', '905566'] | ||
csv << [prefixed_druid2, 'SC1258_FUR_032a.jpg', '42e9d4c0a766f837e5a2f5610d9f258e', | ||
'5bfc6052b0e458e0aa703a0a6853bb9c112e0695', '1530df24086afefd71bf7e5b7e85bb350b6972c838bf6c87ddd5c556b800c802', '167784'] | ||
end | ||
expect(response).to have_http_status(:ok) | ||
expect(response.body).to eq(expected_response) | ||
end | ||
end | ||
|
||
context 'when object not found' do | ||
it 'returns a 404 response code' do | ||
get checksums_objects_url, params: { druids: [prefixed_druid, 'druid:xx123yy9999'], format: :json } | ||
expect(response).to have_http_status(:not_found) | ||
end | ||
end | ||
|
||
context 'when bad parameter passed in' do | ||
it 'returns a 500 response code with a bad druid passed in' do | ||
get checksums_objects_url, params: { druids: [prefixed_druid, 'not a druid'], format: :json } | ||
expect(response).to have_http_status(:internal_server_error) | ||
end | ||
|
||
it 'returns a 406 response code when an unsupported response format is provided' do | ||
get checksums_objects_url, params: { druids: [prefixed_druid], format: :xml } | ||
expect(response).to have_http_status(:not_acceptable) | ||
end | ||
|
||
it 'returns a 500 response code with no druid parameters passed in' do | ||
get checksums_objects_url, params: { format: :json } | ||
expect(response).to have_http_status(:internal_server_error) | ||
end | ||
end | ||
end | ||
end |