/
sc_collections_controller.rb
241 lines (205 loc) · 7.35 KB
/
sc_collections_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
require 'contentdm_translator'
class ScCollectionsController < ApplicationController
before_action :set_sc_collection, only: [:show, :edit, :update, :destroy, :explore_manifest, :import_manifest]
respond_to :html
def index
@universe_collections = ScCollection.universe
@sc_collections = ScCollection.all
respond_with(@sc_collections)
end
def import_cdm
cdm_url = params[:cdm_url]
if cdm_url.blank?
flash[:error] = "Please enter a URL for a CONTENTdm object."
redirect_to :back
return
end
begin
at_id = ContentdmTranslator.cdm_url_to_iiif(cdm_url)
flash[:notice] = "Using CONTENTdm IIIF manifest for #{cdm_url}"
redirect_to :action => :import, :at_id => at_id, :source => 'contentdm', :source_url => cdm_url
rescue => e
logger.error "Bad CONTENTdm URL: #{cdm_url} ERROR: #{e.message}"
flash[:error] = e.message
redirect_to :back
end
end
def import
at_id = params[:at_id]
begin
service = find_service(at_id)
if service["@type"] == "sc:Collection"
@sc_collection = ScCollection.collection_for_at_id(at_id)
@collection = set_collection
render 'explore_collection', at_id: at_id
elsif service["@type"] == "sc:Manifest"
@sc_manifest = ScManifest.manifest_for_at_id(at_id)
find_parent = @sc_manifest.service["within"]
if find_parent.nil? || !find_parent.is_a?(Hash)
@sc_collection = nil
else
parent_at_id = @sc_manifest.service["within"]["@id"]
unless parent_at_id.nil?
@sc_collection = ScCollection.collection_for_at_id(parent_at_id)
else
@sc_collection = nil
end
end
#this allows jquery to recover if there is no parent collection
if @sc_collection
@label = @sc_collection.label
@col = @sc_collection.collection
else
@label = nil
@col = nil
end
render 'explore_manifest', at_id: at_id
end
rescue => e
case params[:source]
when 'contentdm'
flash[:error] = "No IIIF manifest exists for CONTENTdm item #{params[:source_url]}"
else
flash[:error] = "Please enter a valid IIIF manifest URL."
end
redirect_to :back
end
end
def explore_manifest
at_id = params[:at_id]
begin
@sc_manifest = ScManifest.manifest_for_at_id(at_id)
rescue ArgumentError
redirect_to :action => 'explore_collection', :at_id => at_id
return
end
@collection = set_collection
if @sc_collection
@label = @sc_collection.label
@col = @sc_collection.collection
else
@label = nil
@col = nil
end
end
def explore_collection
at_id = params[:at_id]
@sc_collection = ScCollection.collection_for_at_id(at_id)
@collection = set_collection
end
def import_collection
sc_collection = ScCollection.find_by(id: params[:sc_collection_id])
collection_id = params[:collection_id]
cdm_ocr = !params[:contentdm_ocr].blank?
#if collection id is set to sc_collection or no collection is set,
# create a new collection with sc_collection label
unless collection_id == 'sc_collection'
collection = Collection.find_by(id: params[:collection_id])
end
if collection.nil?
collection = create_collection(sc_collection, current_user)
end
#make sure import folder exists
unless Dir.exist?("#{Rails.root}/public/imports")
Dir.mkdir("#{Rails.root}/public/imports")
end
#create logfile for collection
log_file = "#{Rails.root}/public/imports/#{collection.id}_iiif.log"
#map an array of at_ids for the selected manifests
manifest_array = params[:manifest_id].keys.map {|id| id}
#get a list of the manifests to pass to the rake task
manifest_ids = manifest_array.join(" ")
#kick off the rake task here, then redirect to the collection
rake_call = "#{RAKE} fromthepage:import_iiif_collection[#{sc_collection.id},'#{manifest_ids}',#{collection.id},#{current_user.id},#{cdm_ocr}] --trace >> #{log_file} &"
# Nice-up the rake call if we have the appropriate settings
rake_call = "nice -n #{NICE_RAKE_LEVEL} " << rake_call if NICE_RAKE_ENABLED
logger.info rake_call
system(rake_call)
#flash notice about the rake task
flash[:notice] = "IIIF collection import is processing. Reload this page in a few minutes to see imported works."
ajax_redirect_to collection_path(collection.owner, collection)
end
def create_collection(sc_collection, current_user)
collection = Collection.new
collection.owner = current_user
collection.title = sc_collection.label.truncate(255, separator: ' ', omission: '')
collection.save!
sc_collection.update_column(:collection_id, collection.id)
return collection
end
def convert_manifest
at_id = params[:at_id]
@sc_manifest = ScManifest.manifest_for_at_id(at_id)
work = nil
if params[:sc_manifest][:collection_id] == 'sc_collection'
set_sc_collection
work = @sc_manifest.convert_with_sc_collection(current_user, @sc_collection)
else
unless params[:sc_manifest][:collection_id].blank?
@collection = Collection.find params[:sc_manifest][:collection_id]
work = @sc_manifest.convert_with_collection(current_user, @collection)
else
work = @sc_manifest.convert_with_no_collection(current_user)
end
end
if ContentdmTranslator.iiif_manifest_is_cdm? at_id
ocr = !params[:contentdm_ocr].blank?
#make sure import folder exists
unless Dir.exist?("#{Rails.root}/public/imports")
Dir.mkdir("#{Rails.root}/public/imports")
end
log_file = "#{Rails.root}/public/imports/work_#{work.id}_cdm.log"
rake_call = "#{RAKE} fromthepage:cdm_work_update[#{work.id},#{ocr}] --trace >> #{log_file} 2>&1 &"
logger.info rake_call
system(rake_call)
#flash notice about the rake task
flash[:notice] = "Metadata #{ocr ? 'and OCR text ' : ''} is being imported from CONTENTdm and should appear shortly."
end
redirect_to :controller => 'display', :action => 'read_work', :work_id => work.id
end
def show
respond_with(@sc_collection)
end
def new
@sc_collection = ScCollection.new
respond_with(@sc_collection)
end
def edit
end
def create
@sc_collection = ScCollection.new(sc_collection_params)
@sc_collection.save
respond_with(@sc_collection)
end
def update
@sc_collection.update(sc_collection_params)
respond_with(@sc_collection)
end
def destroy
@sc_collection.destroy
respond_with(@sc_collection)
end
private
def set_sc_collection
id = params[:sc_collection_id] || params[:id]
# @sc_collection = ScCollection.find(id)
@sc_collection = ScCollection.find_by id: id
end
def sc_collection_params
params.require(:sc_collection).permit(:collection_id, :context)
end
def set_collection
#used to add new collections to select box on import
if session[:iiif_collection]
@collection = Collection.find_by(id: session[:iiif_collection])
session[:iiif_collection]=nil
return @collection
end
end
def find_service(at_id)
connection = open(at_id)
manifest_json = connection.read
service = IIIF::Service.parse(manifest_json)
return service
end
end