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.
Files changed (122) hide show
  1. data/VERSION.yml +2 -2
  2. data/config/talia_core.yml.example +37 -35
  3. data/generators/talia_admin/templates/app/models/fake_source.rb +93 -0
  4. data/generators/talia_admin/templates/app/models/talia_collection.rb +13 -37
  5. data/generators/talia_base/talia_base_generator.rb +0 -1
  6. data/generators/talia_base/templates/app/controllers/custom_templates_controller.rb +2 -1
  7. data/generators/talia_base/templates/app/controllers/sources_controller.rb +1 -1
  8. data/generators/talia_base/templates/script/configure_talia +56 -73
  9. data/generators/talia_swicky/talia_swicky_generator.rb +18 -0
  10. data/generators/talia_swicky/templates/app/controllers/swicky_notebooks_controller.rb +111 -0
  11. data/generators/talia_swicky/templates/app/helpers/swicky_notebooks_helper.rb +29 -0
  12. data/generators/talia_swicky/templates/app/views/swicky_notebooks/index.builder +6 -0
  13. data/generators/talia_swicky/templates/app/views/swicky_notebooks/index.html.erb +10 -0
  14. data/generators/talia_swicky/templates/app/views/swicky_notebooks/show.html.erb +11 -0
  15. data/generators/talia_swicky/templates/test/fixtures/notebook.rdf +862 -0
  16. data/generators/talia_swicky/templates/test/functional/swicky_notebooks_controller_test.rb +44 -0
  17. data/lib/core_ext/boolean.rb +23 -0
  18. data/lib/core_ext/jdbc_rake_monkeypatch.rb +22 -0
  19. data/lib/core_ext/nil_class.rb +11 -0
  20. data/lib/core_ext/object.rb +34 -0
  21. data/lib/core_ext/string.rb +15 -0
  22. data/lib/custom_template.rb +3 -1
  23. data/lib/loader_helper.rb +16 -3
  24. data/lib/mysql.rb +7 -7
  25. data/lib/progressbar.rb +2 -2
  26. data/lib/swicky/exhibit_json/item.rb +129 -0
  27. data/lib/swicky/exhibit_json/item_collection.rb +129 -0
  28. data/lib/swicky/fragment.rb +0 -0
  29. data/lib/swicky/note.rb +7 -0
  30. data/lib/swicky/notebook.rb +78 -12
  31. data/lib/talia_core/active_source.rb +45 -13
  32. data/lib/talia_core/active_source_parts/class_methods.rb +154 -26
  33. data/lib/talia_core/active_source_parts/finders.rb +49 -26
  34. data/lib/talia_core/active_source_parts/predicate_handler.rb +71 -23
  35. data/lib/talia_core/active_source_parts/rdf/ntriples_reader.rb +13 -0
  36. data/lib/talia_core/active_source_parts/rdf/rdf_reader.rb +99 -0
  37. data/lib/talia_core/active_source_parts/rdf/rdfxml_reader.rb +12 -0
  38. data/lib/talia_core/active_source_parts/{rdf.rb → rdf_handler.rb} +52 -19
  39. data/lib/talia_core/active_source_parts/xml/generic_reader.rb +151 -260
  40. data/lib/talia_core/active_source_parts/xml/generic_reader_add_statements.rb +97 -0
  41. data/lib/talia_core/active_source_parts/xml/generic_reader_helpers.rb +88 -0
  42. data/lib/talia_core/active_source_parts/xml/generic_reader_import_statements.rb +239 -0
  43. data/lib/talia_core/active_source_parts/xml/rdf_builder.rb +14 -7
  44. data/lib/talia_core/active_source_parts/xml/source_builder.rb +7 -3
  45. data/lib/talia_core/active_source_parts/xml/source_reader.rb +17 -2
  46. data/lib/talia_core/collection.rb +192 -1
  47. data/lib/talia_core/data_types/data_loader.rb +88 -18
  48. data/lib/talia_core/data_types/data_record.rb +24 -2
  49. data/lib/talia_core/data_types/delayed_copier.rb +13 -3
  50. data/lib/talia_core/data_types/file_record.rb +24 -13
  51. data/lib/talia_core/data_types/file_store.rb +111 -94
  52. data/lib/talia_core/data_types/iip_data.rb +104 -23
  53. data/lib/talia_core/data_types/iip_loader.rb +102 -56
  54. data/lib/talia_core/data_types/image_data.rb +3 -1
  55. data/lib/talia_core/data_types/media_link.rb +4 -1
  56. data/lib/talia_core/data_types/mime_mapping.rb +65 -38
  57. data/lib/talia_core/data_types/path_helpers.rb +23 -17
  58. data/lib/talia_core/data_types/pdf_data.rb +9 -6
  59. data/lib/talia_core/data_types/simple_text.rb +5 -4
  60. data/lib/talia_core/data_types/xml_data.rb +53 -25
  61. data/lib/talia_core/dummy_handler.rb +3 -2
  62. data/lib/talia_core/errors.rb +13 -27
  63. data/lib/talia_core/initializer.rb +44 -4
  64. data/lib/talia_core/oai/active_source_model.rb +13 -6
  65. data/lib/talia_core/oai/active_source_oai_adapter.rb +13 -12
  66. data/lib/talia_core/rdf_import.rb +1 -1
  67. data/lib/talia_core/rdf_resource.rb +2 -1
  68. data/lib/talia_core/semantic_collection_wrapper.rb +143 -151
  69. data/lib/talia_core/semantic_property.rb +4 -0
  70. data/lib/talia_core/semantic_relation.rb +84 -33
  71. data/lib/talia_core/source.rb +45 -25
  72. data/lib/talia_core/source_fragment.rb +7 -0
  73. data/lib/talia_core/source_transfer_object.rb +3 -1
  74. data/lib/talia_core/source_types/agent.rb +16 -0
  75. data/lib/talia_core/source_types/dc_resource.rb +3 -3
  76. data/lib/talia_core/source_types/marcont_resource.rb +15 -0
  77. data/lib/talia_core/source_types/skos_concept.rb +17 -0
  78. data/lib/talia_dependencies.rb +1 -1
  79. data/lib/talia_util.rb +1 -1
  80. data/lib/talia_util/bar_progressor.rb +1 -1
  81. data/lib/talia_util/image_conversions.rb +8 -2
  82. data/lib/talia_util/import_job_helper.rb +40 -3
  83. data/lib/talia_util/io_helper.rb +15 -4
  84. data/lib/talia_util/progressable.rb +50 -1
  85. data/lib/talia_util/rake_tasks.rb +3 -21
  86. data/lib/talia_util/test_helpers.rb +6 -1
  87. data/lib/talia_util/util.rb +108 -27
  88. data/lib/talia_util/xml/base_builder.rb +28 -1
  89. data/lib/talia_util/xml/rdf_builder.rb +81 -5
  90. data/lib/tasks/talia_core_tasks.rake +2 -0
  91. data/test/core_ext/boolean_test.rb +26 -0
  92. data/test/core_ext/nil_class_test.rb +14 -0
  93. data/test/core_ext/object_test.rb +26 -0
  94. data/test/core_ext/string_test.rb +11 -0
  95. data/test/swicky/json_encoder_test.rb +51 -42
  96. data/test/swicky/notebook_test.rb +13 -6
  97. data/test/talia_core/active_source_finder_interface_test.rb +30 -0
  98. data/test/talia_core/active_source_test.rb +445 -34
  99. data/test/talia_core/collection_test.rb +332 -0
  100. data/test/talia_core/data_types/file_record_test.rb +2 -23
  101. data/test/talia_core/ntriples_reader_test.rb +49 -0
  102. data/test/talia_core/rdfxml_reader_test.rb +51 -0
  103. data/test/talia_core/source_test.rb +12 -0
  104. data/test/talia_util/import_job_helper_test.rb +19 -12
  105. metadata +190 -90
  106. data/config/database.yml +0 -19
  107. data/config/rdfstore.yml +0 -13
  108. data/config/talia_core.yml +0 -24
  109. data/generators/talia_base/templates/migrations/bj_migration.rb +0 -10
  110. data/lib/JXslt/jxslt.rb +0 -60
  111. data/lib/swicky/json_encoder.rb +0 -179
  112. data/lib/talia_core/agent.rb +0 -14
  113. data/lib/talia_core/background_jobs/job.rb +0 -82
  114. data/lib/talia_core/background_jobs/progress_job.rb +0 -68
  115. data/lib/talia_core/data_types/temp_file_handling.rb +0 -85
  116. data/lib/talia_core/ordered_source.rb +0 -228
  117. data/lib/talia_core/semantic_collection_item.rb +0 -94
  118. data/lib/talia_core/source_types/collection.rb +0 -15
  119. data/lib/talia_util/progressbar.rb +0 -236
  120. data/tasks/talia_core_tasks.rake +0 -2
  121. data/test/talia_core/ordered_source_test.rb +0 -394
  122. 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
- # Special module that contains the DataLoader methods to create IIP image data
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 file location for original image files (to .png)
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 requires a conversion for the
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
- # Create the elements from a file
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
- # Create the elements from a stream
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 an IipData object with pre-prepared images if possible
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 fals if the data_record is not an IipData object or the
63
- # :prepared_images option is not set.
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, also the Dir[] doesn't like unescaped brackets
75
- orig_file_pattern.gsub!(/\[/, '\\[')
76
- orig_file_pattern.gsub!(/\]/, '\\]')
77
- orig_file_l = Dir[orig_file_pattern]
78
- raise(ArgumentError, 'Original find not found for ' + url) unless(orig_file_l.size > 0)
79
- orig_file = orig_file_l.first
80
- assit_block { %w(.jpg .jpeg .png).include?(File.extname(orig_file).downcase) }
81
-
82
- iip_record.create_from_existing(thumb_file, pyramid_file)
83
- image_record.create_from_file(location, orig_file)
84
-
85
- true
86
- end
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
- # Little helper to decide how to open the original image
89
- def open_original_image(thing, is_file, current_type, &block)
90
- if(is_file)
91
- open_original_image_file(thing, &block)
92
- else
93
- open_original_image_stream(thing, current_type, &block)
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
- # Same as open_original_image_file, but getting the data from a stream.
98
- def open_original_image_stream(io, type, &block)
99
- # First load this from the web to a temp file
100
- tempfile = File.join(Dir.tmpdir, "talia_down_#{rand 10E16}.#{type.to_sym}")
101
- begin
102
- File.open(tempfile, 'w') do |fio|
103
- fio << io.read # Download the file
104
- end
105
- assit(File.exist?(tempfile))
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
- # Opens the "original" image for the given file. This will convert the
113
- # image to PNG image and the yield the io object for the PNG.
114
- def open_original_image_file(filename)
115
- converted_file = File.join(Dir.tmpdir, "talia_convert_#{rand 10E16}.png")
116
- begin
117
- TaliaUtil::ImageConversions.to_png(filename, converted_file)
118
- File.open(converted_file) do |io|
119
- yield(io)
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
- end # Ending modules and such
128
- end
129
- end
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
- # Class to manage image data type
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. Currently uses a fixed
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
- # The mapping can be configured in Rails' initializer files. Example:
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
- # TaliaCore::DataTypes::MimeMapping(:tiff, :image_data, :create_iip)
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 the class name, this will take the corresponding class from
53
+ # for data_class, this will take the corresponding class from
27
54
  # TaliaCore::DataTypes.
28
55
  #
29
- # The loader method can be a symbol, if given it must correspond to a *class*
30
- # method that can be called on type_class and which accepts exactly 4 parameters,
31
- # which will be passed in during record creation
32
- #
33
- # * Mime type of the record
34
- # * 'location' field of the record
35
- # * The record source - this is a descriptive string with either the url
36
- # or the file name from which the data should be fetched
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
- private
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 related to the data directory
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. Nil if the prefix isn't set
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
- # Path used to store temporary files.
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 data directory for a specific data file
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
- # Path used to store data files.
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
- # Return the full path of the current attachment.
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
- # Extract the filename.
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