talia_core 0.5.4 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION.yml +2 -2
- data/config/talia_core.yml.example +37 -35
- data/generators/talia_admin/templates/app/models/fake_source.rb +93 -0
- data/generators/talia_admin/templates/app/models/talia_collection.rb +13 -37
- data/generators/talia_base/talia_base_generator.rb +0 -1
- data/generators/talia_base/templates/app/controllers/custom_templates_controller.rb +2 -1
- data/generators/talia_base/templates/app/controllers/sources_controller.rb +1 -1
- data/generators/talia_base/templates/script/configure_talia +56 -73
- data/generators/talia_swicky/talia_swicky_generator.rb +18 -0
- data/generators/talia_swicky/templates/app/controllers/swicky_notebooks_controller.rb +111 -0
- data/generators/talia_swicky/templates/app/helpers/swicky_notebooks_helper.rb +29 -0
- data/generators/talia_swicky/templates/app/views/swicky_notebooks/index.builder +6 -0
- data/generators/talia_swicky/templates/app/views/swicky_notebooks/index.html.erb +10 -0
- data/generators/talia_swicky/templates/app/views/swicky_notebooks/show.html.erb +11 -0
- data/generators/talia_swicky/templates/test/fixtures/notebook.rdf +862 -0
- data/generators/talia_swicky/templates/test/functional/swicky_notebooks_controller_test.rb +44 -0
- data/lib/core_ext/boolean.rb +23 -0
- data/lib/core_ext/jdbc_rake_monkeypatch.rb +22 -0
- data/lib/core_ext/nil_class.rb +11 -0
- data/lib/core_ext/object.rb +34 -0
- data/lib/core_ext/string.rb +15 -0
- data/lib/custom_template.rb +3 -1
- data/lib/loader_helper.rb +16 -3
- data/lib/mysql.rb +7 -7
- data/lib/progressbar.rb +2 -2
- data/lib/swicky/exhibit_json/item.rb +129 -0
- data/lib/swicky/exhibit_json/item_collection.rb +129 -0
- data/lib/swicky/fragment.rb +0 -0
- data/lib/swicky/note.rb +7 -0
- data/lib/swicky/notebook.rb +78 -12
- data/lib/talia_core/active_source.rb +45 -13
- data/lib/talia_core/active_source_parts/class_methods.rb +154 -26
- data/lib/talia_core/active_source_parts/finders.rb +49 -26
- data/lib/talia_core/active_source_parts/predicate_handler.rb +71 -23
- data/lib/talia_core/active_source_parts/rdf/ntriples_reader.rb +13 -0
- data/lib/talia_core/active_source_parts/rdf/rdf_reader.rb +99 -0
- data/lib/talia_core/active_source_parts/rdf/rdfxml_reader.rb +12 -0
- data/lib/talia_core/active_source_parts/{rdf.rb → rdf_handler.rb} +52 -19
- data/lib/talia_core/active_source_parts/xml/generic_reader.rb +151 -260
- data/lib/talia_core/active_source_parts/xml/generic_reader_add_statements.rb +97 -0
- data/lib/talia_core/active_source_parts/xml/generic_reader_helpers.rb +88 -0
- data/lib/talia_core/active_source_parts/xml/generic_reader_import_statements.rb +239 -0
- data/lib/talia_core/active_source_parts/xml/rdf_builder.rb +14 -7
- data/lib/talia_core/active_source_parts/xml/source_builder.rb +7 -3
- data/lib/talia_core/active_source_parts/xml/source_reader.rb +17 -2
- data/lib/talia_core/collection.rb +192 -1
- data/lib/talia_core/data_types/data_loader.rb +88 -18
- data/lib/talia_core/data_types/data_record.rb +24 -2
- data/lib/talia_core/data_types/delayed_copier.rb +13 -3
- data/lib/talia_core/data_types/file_record.rb +24 -13
- data/lib/talia_core/data_types/file_store.rb +111 -94
- data/lib/talia_core/data_types/iip_data.rb +104 -23
- data/lib/talia_core/data_types/iip_loader.rb +102 -56
- data/lib/talia_core/data_types/image_data.rb +3 -1
- data/lib/talia_core/data_types/media_link.rb +4 -1
- data/lib/talia_core/data_types/mime_mapping.rb +65 -38
- data/lib/talia_core/data_types/path_helpers.rb +23 -17
- data/lib/talia_core/data_types/pdf_data.rb +9 -6
- data/lib/talia_core/data_types/simple_text.rb +5 -4
- data/lib/talia_core/data_types/xml_data.rb +53 -25
- data/lib/talia_core/dummy_handler.rb +3 -2
- data/lib/talia_core/errors.rb +13 -27
- data/lib/talia_core/initializer.rb +44 -4
- data/lib/talia_core/oai/active_source_model.rb +13 -6
- data/lib/talia_core/oai/active_source_oai_adapter.rb +13 -12
- data/lib/talia_core/rdf_import.rb +1 -1
- data/lib/talia_core/rdf_resource.rb +2 -1
- data/lib/talia_core/semantic_collection_wrapper.rb +143 -151
- data/lib/talia_core/semantic_property.rb +4 -0
- data/lib/talia_core/semantic_relation.rb +84 -33
- data/lib/talia_core/source.rb +45 -25
- data/lib/talia_core/source_fragment.rb +7 -0
- data/lib/talia_core/source_transfer_object.rb +3 -1
- data/lib/talia_core/source_types/agent.rb +16 -0
- data/lib/talia_core/source_types/dc_resource.rb +3 -3
- data/lib/talia_core/source_types/marcont_resource.rb +15 -0
- data/lib/talia_core/source_types/skos_concept.rb +17 -0
- data/lib/talia_dependencies.rb +1 -1
- data/lib/talia_util.rb +1 -1
- data/lib/talia_util/bar_progressor.rb +1 -1
- data/lib/talia_util/image_conversions.rb +8 -2
- data/lib/talia_util/import_job_helper.rb +40 -3
- data/lib/talia_util/io_helper.rb +15 -4
- data/lib/talia_util/progressable.rb +50 -1
- data/lib/talia_util/rake_tasks.rb +3 -21
- data/lib/talia_util/test_helpers.rb +6 -1
- data/lib/talia_util/util.rb +108 -27
- data/lib/talia_util/xml/base_builder.rb +28 -1
- data/lib/talia_util/xml/rdf_builder.rb +81 -5
- data/lib/tasks/talia_core_tasks.rake +2 -0
- data/test/core_ext/boolean_test.rb +26 -0
- data/test/core_ext/nil_class_test.rb +14 -0
- data/test/core_ext/object_test.rb +26 -0
- data/test/core_ext/string_test.rb +11 -0
- data/test/swicky/json_encoder_test.rb +51 -42
- data/test/swicky/notebook_test.rb +13 -6
- data/test/talia_core/active_source_finder_interface_test.rb +30 -0
- data/test/talia_core/active_source_test.rb +445 -34
- data/test/talia_core/collection_test.rb +332 -0
- data/test/talia_core/data_types/file_record_test.rb +2 -23
- data/test/talia_core/ntriples_reader_test.rb +49 -0
- data/test/talia_core/rdfxml_reader_test.rb +51 -0
- data/test/talia_core/source_test.rb +12 -0
- data/test/talia_util/import_job_helper_test.rb +19 -12
- metadata +190 -90
- data/config/database.yml +0 -19
- data/config/rdfstore.yml +0 -13
- data/config/talia_core.yml +0 -24
- data/generators/talia_base/templates/migrations/bj_migration.rb +0 -10
- data/lib/JXslt/jxslt.rb +0 -60
- data/lib/swicky/json_encoder.rb +0 -179
- data/lib/talia_core/agent.rb +0 -14
- data/lib/talia_core/background_jobs/job.rb +0 -82
- data/lib/talia_core/background_jobs/progress_job.rb +0 -68
- data/lib/talia_core/data_types/temp_file_handling.rb +0 -85
- data/lib/talia_core/ordered_source.rb +0 -228
- data/lib/talia_core/semantic_collection_item.rb +0 -94
- data/lib/talia_core/source_types/collection.rb +0 -15
- data/lib/talia_util/progressbar.rb +0 -236
- data/tasks/talia_core_tasks.rake +0 -2
- data/test/talia_core/ordered_source_test.rb +0 -394
- data/test/talia_core/semantic_collection_item_test.rb +0 -125
@@ -3,14 +3,47 @@ require 'tmpdir'
|
|
3
3
|
module TaliaCore
|
4
4
|
module DataTypes
|
5
5
|
|
6
|
-
#
|
6
|
+
# Loader module for IipData records. IIP records should always be created
|
7
|
+
# by using the #create_iip method in this module as a loader. Check the
|
8
|
+
# DataLoader documentation to find out how to configure loaders for
|
9
|
+
# different MIME types.
|
10
|
+
#
|
11
|
+
# = How does this work?
|
12
|
+
#
|
13
|
+
# The loader will take the original image, and create two new DataRecords
|
14
|
+
# which contain three different versions of the image:
|
15
|
+
#
|
16
|
+
# * An ImageData record will contain the original version of the image.
|
17
|
+
# This will contain the original file if the original is a jpeg or png
|
18
|
+
# image. Otherwise it will be converted to png.
|
19
|
+
# * An IipData record which contains a thumbnail of the original image and
|
20
|
+
# create the pyramidal version of it in the IIP server's directory. The
|
21
|
+
# location of the IipData will contain the path to the pyramidal image
|
22
|
+
# on the server
|
23
|
+
#
|
24
|
+
# = Using existing images
|
25
|
+
#
|
26
|
+
# Creating the pyramid images can be a time-consuming process and it may
|
27
|
+
# be useful to separate this from the actual import process (if, for example
|
28
|
+
# you want to re-import the same data set several times). The Talia
|
29
|
+
# distribution contains a <tt>prepare_images</tt> script that will take the
|
30
|
+
# original files from a given directory and create the converted files in
|
31
|
+
# another location, using the subdirectories <tt>thumbs</tt>, <tt>pyramids</tt>
|
32
|
+
# and <tt>originals</tt> respectively.
|
33
|
+
#
|
34
|
+
# If a directory with "prepared files" exists, the <tt>prepared_images</tt>
|
35
|
+
# option in the _environment_ to the path to the directory. When the loader
|
36
|
+
# see that environment variable, it will automatically load the prepared
|
37
|
+
# images from that directory.
|
7
38
|
module IipLoader
|
8
39
|
|
9
40
|
|
10
41
|
# Loads an image for the given file. This is a tad more complex than loading
|
11
42
|
# the data into a data record: It will create both an IIP data object and
|
12
|
-
# an Image data object.
|
43
|
+
# an Image data object. If the original is an IO stream, a temp file will
|
44
|
+
# be created for the conversions. See above for more.
|
13
45
|
def create_iip(mime_type, location, source, is_file)
|
46
|
+
# Create the new records
|
14
47
|
iip_record = TaliaCore::DataTypes::IipData.new
|
15
48
|
image_record = TaliaCore::DataTypes::ImageData.new
|
16
49
|
records = [iip_record, image_record]
|
@@ -35,32 +68,40 @@ module TaliaCore
|
|
35
68
|
records
|
36
69
|
end
|
37
70
|
|
38
|
-
# Rewrite the
|
71
|
+
# Rewrite the <tt>location</tt> field (filename) for "original image" data
|
72
|
+
# record, changing the extension to .png
|
39
73
|
def orig_location(location)
|
40
74
|
File.basename(location, File.extname(location)) + '.png'
|
41
75
|
end
|
42
76
|
|
43
|
-
# Indicates if the given mime type
|
44
|
-
# original image
|
77
|
+
# Indicates if an original image with the given mime type
|
78
|
+
# should be converted to png for the "original image" record
|
45
79
|
def convert_original?(mime_type)
|
46
80
|
!([:jpeg, :png].include?(mime_type.to_sym))
|
47
81
|
end
|
48
82
|
|
49
|
-
#
|
83
|
+
# Pass in the empty data records (for original and iip), and
|
84
|
+
# fill each one by calling #create_from_file with the
|
85
|
+
# given file
|
50
86
|
def create_from_files(location, file, records)
|
51
87
|
records.each { |rec| rec.create_from_file(location, file) }
|
52
88
|
end
|
53
89
|
|
54
|
-
#
|
90
|
+
# Pass in the empty data records (for original and iip), and
|
91
|
+
# fill them by reading the data from the io stream and
|
92
|
+
# calling #create_from_data on each record
|
55
93
|
def create_from_stream(location, io, records)
|
56
94
|
data = io.read
|
57
95
|
records.each { |rec| rec.create_from_data(location, data)}
|
58
96
|
end
|
59
97
|
|
60
|
-
# Attempts to create
|
98
|
+
# Attempts to create the records from pre-prepared images, if possible.
|
61
99
|
# Returns true if (and only if) the object has been created with existing
|
62
|
-
# data. Always
|
63
|
-
#
|
100
|
+
# data. Always false if the data_record is not an IipData object or the
|
101
|
+
# <tt>prepared_images</tt> option is not set.
|
102
|
+
#
|
103
|
+
# If this method returns true, the images have been successfully prepared
|
104
|
+
# from pre-prepared images.
|
64
105
|
def prepare_image_from_existing!(iip_record, image_record, url, location)
|
65
106
|
return false unless(iip_record.is_a?(TaliaCore::DataTypes::IipData) && image_record.is_a?(TaliaCore::DataTypes::ImageData))
|
66
107
|
return false unless((prep = ENV['prepared_images']) && prep.to_s.downcase != 'no' && prep.to_s.downcase != 'false')
|
@@ -68,62 +109,67 @@ module TaliaCore
|
|
68
109
|
file_ext = File.extname(url)
|
69
110
|
file_base = File.basename(url, file_ext)
|
70
111
|
|
112
|
+
# Get the file paths for each prepared image: thumb, pyramid and original
|
71
113
|
thumb_file = File.join( ENV['prepared_images'], 'thumbs', "#{file_base}.gif")
|
72
114
|
pyramid_file = File.join( ENV['prepared_images'], 'pyramids', "#{file_base}.tif")
|
115
|
+
# Use a pattern for the original file, since we don't know the extensions
|
73
116
|
orig_file_pattern = File.join(ENV['prepared_images'], 'originals', "#{file_base}.*")
|
74
|
-
# We need to fix the pattern
|
75
|
-
orig_file_pattern.gsub!(/\[/, '\\[')
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
117
|
+
# We need to fix the pattern
|
118
|
+
orig_file_pattern.gsub!(/\[/, '\\[') # Escape brackets, Dir[] doesn't like them
|
119
|
+
orig_file_pattern.gsub!(/\]/, '\\]')
|
120
|
+
orig_file_l = Dir[orig_file_pattern]
|
121
|
+
raise(ArgumentError, 'Original find not found for ' + url) unless(orig_file_l.size > 0)
|
122
|
+
orig_file = orig_file_l.first # Original is the first file matching the pattern
|
123
|
+
assit_block { %w(.jpg .jpeg .png).include?(File.extname(orig_file).downcase) }
|
124
|
+
|
125
|
+
# Now create the existing records from the files
|
126
|
+
iip_record.create_from_existing(thumb_file, pyramid_file)
|
127
|
+
image_record.create_from_file(location, orig_file)
|
128
|
+
|
129
|
+
true
|
130
|
+
end
|
87
131
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
end
|
132
|
+
# Little helper to decide how to open the original image
|
133
|
+
def open_original_image(thing, is_file, current_type, &block)
|
134
|
+
if(is_file)
|
135
|
+
open_original_image_file(thing, &block)
|
136
|
+
else
|
137
|
+
open_original_image_stream(thing, current_type, &block)
|
95
138
|
end
|
139
|
+
end
|
96
140
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
open_original_image_file(tempfile, &block)
|
107
|
-
ensure
|
108
|
-
FileUtils.rm(tempfile) if(File.exist?(tempfile))
|
141
|
+
# Same as open_original_image_file, but getting the data from a stream.
|
142
|
+
# This writes the data to a temp file and calls open_original_file on it
|
143
|
+
# The temporary file is deleted after the operation
|
144
|
+
def open_original_image_stream(io, type, &block)
|
145
|
+
# First load this from the web to a temp file
|
146
|
+
tempfile = File.join(Dir.tmpdir, "talia_down_#{rand 10E16}.#{type.to_sym}")
|
147
|
+
begin
|
148
|
+
File.open(tempfile, 'w') do |fio|
|
149
|
+
fio << io.read # Download the file
|
109
150
|
end
|
151
|
+
assit(File.exist?(tempfile))
|
152
|
+
open_original_image_file(tempfile, &block)
|
153
|
+
ensure
|
154
|
+
FileUtils.rm(tempfile) if(File.exist?(tempfile))
|
110
155
|
end
|
156
|
+
end
|
111
157
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
end
|
121
|
-
ensure
|
122
|
-
FileUtils.rm(converted_file) if(File.exist?(converted_file))
|
158
|
+
# Opens the "original" image for the given file. This will convert the
|
159
|
+
# image to PNG image and the yield the io object for the PNG.
|
160
|
+
def open_original_image_file(filename)
|
161
|
+
converted_file = File.join(Dir.tmpdir, "talia_convert_#{rand 10E16}.png")
|
162
|
+
begin
|
163
|
+
TaliaUtil::ImageConversions.to_png(filename, converted_file)
|
164
|
+
File.open(converted_file) do |io|
|
165
|
+
yield(io)
|
123
166
|
end
|
167
|
+
ensure
|
168
|
+
FileUtils.rm(converted_file) if(File.exist?(converted_file))
|
124
169
|
end
|
170
|
+
end
|
125
171
|
|
126
172
|
|
127
|
-
|
128
|
-
|
129
|
-
|
173
|
+
end # Ending modules and such
|
174
|
+
end
|
175
|
+
end
|
@@ -1,11 +1,13 @@
|
|
1
1
|
module TaliaCore
|
2
2
|
module DataTypes
|
3
3
|
|
4
|
-
#
|
4
|
+
# A FileRecord that contains an image.
|
5
5
|
class ImageData < FileRecord
|
6
6
|
|
7
7
|
# return the mime_type for a file
|
8
8
|
def extract_mime_type(location)
|
9
|
+
# TODO: This may work automatically if all the MIME types
|
10
|
+
# are configured correctly (?)
|
9
11
|
case File.extname(location).downcase
|
10
12
|
when '.bmp'
|
11
13
|
'image/bmp'
|
@@ -3,7 +3,10 @@ module TaliaCore
|
|
3
3
|
|
4
4
|
# Data class that contains just a link to a remote data element (and no
|
5
5
|
# locally stored data at all). The uri for the link is stored in the location
|
6
|
-
# field
|
6
|
+
# field.
|
7
|
+
#
|
8
|
+
# *Note*: It is not possible to store data in media links, so all data-related
|
9
|
+
# methods will raise an exception
|
7
10
|
class MediaLink < DataRecord
|
8
11
|
|
9
12
|
def all_bytes
|
@@ -1,21 +1,48 @@
|
|
1
1
|
module TaliaCore
|
2
2
|
module DataTypes
|
3
3
|
|
4
|
-
# Mapping from Mime types to data classes and importing methods
|
5
|
-
# default mapping. If the mime type is not known, it will use a fallback default handler.
|
4
|
+
# Mapping from Mime types to data classes and importing methods for DataRecord.
|
6
5
|
#
|
7
|
-
#
|
6
|
+
# See the DataTypes::DataLoader module to see how the import works. In a nutshell,
|
7
|
+
# each MIME type can be connected either to a DataTypes::FileRecord type that will
|
8
|
+
# be used for new data records, or the MIME type can be connected to a handler
|
9
|
+
# method that will do the creation.
|
8
10
|
#
|
9
|
-
#
|
11
|
+
# = Default Mappings
|
12
|
+
#
|
13
|
+
# See the source code of the mapping_hash method for the detailed default mapping.
|
14
|
+
# It goes something like this:
|
15
|
+
#
|
16
|
+
# * All image types (:jpeg, :tiff, :png, :gif, ...) use DataTypes::ImageData
|
17
|
+
# * HTML, XML and all "transcription" types (:html, :xml, :tei, ...) use DataTypes::XmlData
|
18
|
+
# * :text uses DataTypes::SimpleText
|
19
|
+
# * :pdf uses DataTypes::PdfData
|
20
|
+
# * The default (for unknow types) is to use DataTypes::FileRecord
|
21
|
+
#
|
22
|
+
# = Configure the MIME mappings for Talia
|
23
|
+
#
|
24
|
+
# The mapping can be configured in Rails' initializer files (e.g.
|
25
|
+
# config/initializers/talia.rb):
|
26
|
+
#
|
27
|
+
# TaliaCore::DataTypes::MimeMapping.add_mapping(:tiff, :image_data, :create_iip)
|
28
|
+
#
|
29
|
+
# Add a mapping for each MIME type that you need, or where you want to change
|
30
|
+
# the default mapping
|
10
31
|
class MimeMapping
|
11
32
|
|
12
33
|
class << self
|
13
34
|
|
14
|
-
# Gets the data class for the given mime type
|
35
|
+
# Gets the data class for the given mime type. For loaders configured
|
36
|
+
# through add_mapping, this will always return the class corresponding
|
37
|
+
# to data_class. (Otherwise it will return the data_class configured
|
38
|
+
# in the default mapping)
|
15
39
|
def class_type_from(mime_type)
|
16
40
|
mapping_for(mime_type)[:type]
|
17
41
|
end
|
18
42
|
|
43
|
+
# Return the "loader type" for the given MIME type. This will return the
|
44
|
+
# handler (as a symbol, see add_mapping) if set.
|
45
|
+
# If no handler is set, it will return the data_class (as class_type_from).
|
19
46
|
def loader_type_from(mime_type)
|
20
47
|
map = mapping_for(mime_type)
|
21
48
|
map[:loader] || map[:type]
|
@@ -23,23 +50,17 @@ module TaliaCore
|
|
23
50
|
|
24
51
|
# Set a new mapping for the given MIME type. If only a class is given, this
|
25
52
|
# will use the class to create new data records from. If a symbol is given
|
26
|
-
# for
|
53
|
+
# for data_class, this will take the corresponding class from
|
27
54
|
# TaliaCore::DataTypes.
|
28
55
|
#
|
29
|
-
#
|
30
|
-
#
|
31
|
-
#
|
32
|
-
#
|
33
|
-
#
|
34
|
-
#
|
35
|
-
#
|
36
|
-
#
|
37
|
-
# * The is_file flag. This will be true if the source is the name of a
|
38
|
-
# regular file. Otherwise, the source field should be interpreted as
|
39
|
-
# a URL.
|
40
|
-
# == Example handler method
|
41
|
-
#
|
42
|
-
# def data_loader(mime_type, location, source, is_file)
|
56
|
+
# = Examples
|
57
|
+
#
|
58
|
+
# # Uses DataTypes::ImageData#create_iip to create new records
|
59
|
+
# TaliaCore::DataTypes::MimeMapping.add_mapping(:tiff, :image_data, :create_iip)
|
60
|
+
#
|
61
|
+
# # Use the DataTypes::ImageData class for new data records, and create records
|
62
|
+
# # in the default way (using create_with_file or similar)
|
63
|
+
# TaliaCore::DataTypes::MimeMapping.add_mapping(:png, DataTypes::ImageData)
|
43
64
|
def add_mapping(mime_type, data_class, handler = nil)
|
44
65
|
mapping = {}
|
45
66
|
if(!data_class.is_a?(Class))
|
@@ -54,23 +75,8 @@ module TaliaCore
|
|
54
75
|
true
|
55
76
|
end
|
56
77
|
|
57
|
-
|
58
|
-
|
59
|
-
def symbol_for(mime_type)
|
60
|
-
mime_type = Mime::Type.lookup(mime_type) unless(mime_type.is_a?(Mime::Type))
|
61
|
-
mime_type.to_sym
|
62
|
-
end
|
63
|
-
|
64
|
-
def mapping_for(mime_type)
|
65
|
-
mapping = mapping_hash[symbol_for(mime_type)]
|
66
|
-
TaliaCore.logger.warn { "No data class registered for mime type #{mime_type.inspect}, trying default handler." } unless(mapping)
|
67
|
-
mapping ||= mapping_hash[:default]
|
68
|
-
|
69
|
-
raise(ArgumentError, "No data class registered for type #{mime_type.inspect}") unless(mapping)
|
70
|
-
mapping
|
71
|
-
end
|
72
|
-
|
73
|
-
# Currently there is only the default mapping
|
78
|
+
# Returns the current mapping. This will be automatically initialized with
|
79
|
+
# the default mappings.
|
74
80
|
def mapping_hash
|
75
81
|
@mapping ||= {
|
76
82
|
:xml => { :type => DataTypes::XmlData },
|
@@ -92,10 +98,31 @@ module TaliaCore
|
|
92
98
|
:default => { :type => FileRecord }
|
93
99
|
}
|
94
100
|
end
|
101
|
+
|
102
|
+
private
|
103
|
+
|
104
|
+
# Returns the symbol that corresponds to a given MIME type. The MIME type
|
105
|
+
# can be a Mime::Type object, a MIME string like 'text/html' or a MIME
|
106
|
+
# symbol like :jpeg
|
107
|
+
def symbol_for(mime_type)
|
108
|
+
mime_type = Mime::Type.lookup(mime_type) unless(mime_type.is_a?(Mime::Type))
|
109
|
+
mime_type.to_sym
|
110
|
+
end
|
111
|
+
|
112
|
+
# Return the mapping for the given MIME type. If no mapping is defined for the
|
113
|
+
# MIME type, it will log a warning and use the "default" mapping.
|
114
|
+
def mapping_for(mime_type)
|
115
|
+
mapping = mapping_hash[symbol_for(mime_type)]
|
116
|
+
TaliaCore.logger.warn { "No data class registered for mime type #{mime_type.inspect}, trying default handler." } unless(mapping)
|
117
|
+
mapping ||= mapping_hash[:default]
|
118
|
+
|
119
|
+
raise(ArgumentError, "No data class registered for type #{mime_type.inspect}") unless(mapping)
|
120
|
+
mapping
|
121
|
+
end
|
95
122
|
|
96
123
|
end
|
97
124
|
|
98
125
|
end
|
99
126
|
|
100
127
|
end
|
101
|
-
end
|
128
|
+
end
|
@@ -5,6 +5,7 @@ module TaliaCore
|
|
5
5
|
module PathHelpers
|
6
6
|
|
7
7
|
module ClassMethods
|
8
|
+
|
8
9
|
# Path used to store temporary files.
|
9
10
|
def tempfile_path
|
10
11
|
@@tempfile_path ||= File.join(TALIA_ROOT, 'tmp', 'data_records')
|
@@ -15,22 +16,26 @@ module TaliaCore
|
|
15
16
|
@@data_path ||= File.join(TALIA_ROOT, 'data')
|
16
17
|
end
|
17
18
|
|
18
|
-
|
19
|
-
|
20
|
-
# Extract the filename.
|
21
|
-
def extract_filename(file_data)
|
22
|
-
file_data.original_filename if file_data.respond_to?(:original_filename)
|
23
|
-
end
|
24
|
-
|
25
19
|
end
|
26
20
|
|
27
|
-
# Return the full file path
|
21
|
+
# Return the full file path for this record. If the relative
|
22
|
+
# flag is set, this will only return the relative path inside
|
23
|
+
# the data directory
|
28
24
|
def file_path(relative = false)
|
29
25
|
File.join(data_directory(relative), self.id.to_s)
|
30
26
|
end
|
31
27
|
|
32
28
|
# Gets the path that will be used for serving the image as a static
|
33
|
-
# resource.
|
29
|
+
# resource.
|
30
|
+
#
|
31
|
+
# This will return nil unless the <tt>static_data_prefix</tt> option
|
32
|
+
# is set in the configuration. This option defines a URL prefix for
|
33
|
+
# static files.
|
34
|
+
#
|
35
|
+
# If the prefix is set, this method will return a URL that can be used
|
36
|
+
# access the current file as a static asset. To use this, the data
|
37
|
+
# directory has to be available on a web server at the
|
38
|
+
# <tt>static_data_prefix</tt>
|
34
39
|
def static_path
|
35
40
|
prefix = TaliaCore::CONFIG['static_data_prefix']
|
36
41
|
return unless(prefix)
|
@@ -38,13 +43,16 @@ module TaliaCore
|
|
38
43
|
"#{prefix}/#{class_name}/#{("00" + self.id.to_s)[-3..-1]}/#{self.id}"
|
39
44
|
end
|
40
45
|
|
41
|
-
#
|
42
|
-
# This is a wrapper for the tempfile_path class method.
|
46
|
+
# See ClassMethods.tempfile_path
|
43
47
|
def tempfile_path
|
44
48
|
self.class.tempfile_path
|
45
49
|
end
|
46
50
|
|
47
|
-
# Return the
|
51
|
+
# Return the _directory_ in which the file for the current record is stored.
|
52
|
+
# If the relative flag is set, it will only return the relative path
|
53
|
+
# inside the main data directory.
|
54
|
+
#
|
55
|
+
# The paths will look something like: <tt>.../XmlData/031/</tt>
|
48
56
|
def data_directory(relative = false)
|
49
57
|
class_name = self.class.name.gsub(/(.*::)/, '')
|
50
58
|
if relative == false
|
@@ -54,19 +62,17 @@ module TaliaCore
|
|
54
62
|
end
|
55
63
|
end
|
56
64
|
|
57
|
-
#
|
58
|
-
# This is a wrapper for the data_path class method.
|
65
|
+
# See ClassMethods.data_path
|
59
66
|
def data_path
|
60
67
|
self.class.data_path
|
61
68
|
end
|
62
69
|
|
63
|
-
#
|
70
|
+
# Cached version of #file_path
|
64
71
|
def full_filename
|
65
72
|
@full_filename ||= self.file_path #File.join(data_path, class_name, location)
|
66
73
|
end
|
67
74
|
|
68
|
-
#
|
69
|
-
# This is a wrapper for the extract_filename class method.
|
75
|
+
# See ClassMethods.extract_filename
|
70
76
|
def extract_filename(file_data)
|
71
77
|
self.class.extract_filename(file_data)
|
72
78
|
end
|