Class: TaliaCore::DataTypes::IipData
- Inherits:
-
FileRecord
- Object
- ActiveRecord::Base
- DataRecord
- FileRecord
- TaliaCore::DataTypes::IipData
- Defined in:
- lib/talia_core/data_types/iip_data.rb
Overview
Class to manage IIP Image data type. FIXME: Check if this correctly destroys existing files.
Instance Attribute Summary
Attributes inherited from DataRecord
Class Method Summary collapse
-
.iip_server_uri ⇒ Object
Returns the IIP server configured for the application.
Instance Method Summary collapse
-
#create_from_existing(thumb, pyramid, delete_originals = false) ⇒ Object
Create from existing thumb and pyramid images.
-
#create_pyramid(source) ⇒ Object
Creates the pyramid image for IIP by running the configured system command.
-
#direct_write! ⇒ Object
Checks if we have file paths given to directly copy thum and image file.
-
#get_iip_root_file_path(relative = false) ⇒ Object
Return the full file path related to the data directory.
-
#iip_root_directory(relative = false) ⇒ Object
Return the iip root directory for a specific iip image file.
-
#iip_server_path ⇒ Object
return IIP Server Path.
-
#prepare_for_pyramid ⇒ Object
Prepare for copying or creating the pyramid image.
-
#prepare_original_file ⇒ Object
This prepares the original file that needs to be converted.
-
#set_mime_type ⇒ Object
This is the mime type for the thumbnail - always tiff.
- #write_file_after_save ⇒ Object
Methods inherited from FileRecord
#all_bytes, #get_byte, #position, #reset, #seek, #size
Methods included from FileStore::ClassMethods
#find_or_create_and_assign_file
Methods included from PathHelpers::ClassMethods
#data_path, #extract_filename, #tempfile_path
Methods included from TempFileHandling::ClassMethods
#copy_to_temp_file, #create_tempfile_path, #write_to_temp_file
Methods included from DataLoader::ClassMethods
Methods included from IipLoader
#convert_original?, #create_from_files, #create_from_stream, #create_iip, #open_original_image, #open_original_image_file, #open_original_image_stream, #orig_location, #prepare_image_from_existing!
Methods included from TaliaUtil::IoHelper
#base_for, #file_url, #open_from_url, #open_generic
Methods included from TempFileHandling
#copy_to_temp_file, #temp_data, #temp_data=, #temp_path, #temp_path=, #temp_paths, #write_to_temp_file
Methods included from PathHelpers
#data_directory, #data_path, #extract_filename, #file_path, #full_filename, #static_path, #tempfile_path
Methods included from FileStore
#all_text, #assign_type, #create_from_data, #create_from_file, #file, #file=, #is_file_open?
Methods inherited from DataRecord
#all_bytes, #content_string, #extract_mime_type, find_by_type_and_location!, find_data_records, #get_byte, #mime_type, #position, #reset, #seek, #size
Class Method Details
.iip_server_uri ⇒ Object
Returns the IIP server configured for the application
8 9 10 |
# File 'lib/talia_core/data_types/iip_data.rb', line 8 def self.iip_server_uri TaliaCore::CONFIG['iip_server_uri'] ||= 'http://localhost/fcgi-bin/iipsrv.fcgi' end |
Instance Method Details
#create_from_existing(thumb, pyramid, delete_originals = false) ⇒ Object
Create from existing thumb and pyramid images
20 21 22 23 24 |
# File 'lib/talia_core/data_types/iip_data.rb', line 20 def create_from_existing(thumb, pyramid, delete_originals = false) @file_data_to_write = [thumb, pyramid] @delete_original_file = delete_originals self.location = '' end |
#create_pyramid(source) ⇒ Object
Creates the pyramid image for IIP by running the configured system command. This automatically creates the file in the correct location (IIP root)
120 121 122 123 124 125 126 127 |
# File 'lib/talia_core/data_types/iip_data.rb', line 120 def create_pyramid(source) # check if file already exists raise(IOError, "File already exists: #{get_iip_root_file_path}") if(File.exists?(get_iip_root_file_path)) prepare_for_pyramid TaliaUtil::ImageConversions::create_pyramid(source, get_iip_root_file_path) end |
#direct_write! ⇒ Object
Checks if we have file paths given to directly copy thum and image file. Will always return true if such paths were given.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/talia_core/data_types/iip_data.rb', line 67 def direct_write! return false unless(@file_data_to_write.kind_of?(Array)) thumb, pyramid = @file_data_to_write self.class.benchmark("\033[36mIipData\033[0m Direct write for #{self.id}", Logger::INFO) do prepare_for_pyramid copy_or_move(pyramid, get_iip_root_file_path) end # end benchmark @file_data_to_write = DataPath.new(thumb) true end |
#get_iip_root_file_path(relative = false) ⇒ Object
Return the full file path related to the data directory
139 140 141 |
# File 'lib/talia_core/data_types/iip_data.rb', line 139 def get_iip_root_file_path(relative = false) File.join(iip_root_directory(relative), self.id.to_s + '.tif') end |
#iip_root_directory(relative = false) ⇒ Object
Return the iip root directory for a specific iip image file
130 131 132 133 134 135 136 |
# File 'lib/talia_core/data_types/iip_data.rb', line 130 def iip_root_directory(relative = false) if relative == false File.join(TaliaCore::CONFIG["iip_root_directory_location"], ("00" + self.id.to_s)[-3..-1]) else File.join(("00" + self.id.to_s)[-3..-1]) end end |
#iip_server_path ⇒ Object
return IIP Server Path
27 28 29 |
# File 'lib/talia_core/data_types/iip_data.rb', line 27 def iip_server_path self.location end |
#prepare_for_pyramid ⇒ Object
Prepare for copying or creating the pyramid image
109 110 111 112 113 114 115 |
# File 'lib/talia_core/data_types/iip_data.rb', line 109 def prepare_for_pyramid # set location self.location = get_iip_root_file_path(true) # create data directory path FileUtils.mkdir_p(iip_root_directory) end |
#prepare_original_file ⇒ Object
This prepares the original file that needs to be converted. This will see if the data to be written is binary data or a file path. If this is binary data, it will create a temporary file on the disk.
This returns an array with two elements: The name of the file to be used (a file system path) and a flag indicating if the file is a temporary file or not.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/talia_core/data_types/iip_data.rb', line 90 def prepare_original_file if(@file_data_to_write.is_a?(DataPath)) [@file_data_to_write, false] else temp_file = File.join(Dir.tmpdir, "original_#{random_tempfile_filename}") # write the original file File.open(temp_file, 'w') do |original_file| if(@file_data_to_write.respond_to?(:read)) original_file << @file_data_to_write.read else original_file << @file_data_to_write end end [temp_file, true] end end |
#set_mime_type ⇒ Object
This is the mime type for the thumbnail - always tiff
13 14 15 |
# File 'lib/talia_core/data_types/iip_data.rb', line 13 def set_mime_type self.mime = 'image/gif' end |
#write_file_after_save ⇒ Object
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 |
# File 'lib/talia_core/data_types/iip_data.rb', line 31 def write_file_after_save return unless(@file_data_to_write) # Check if we have the images already given, in this case we prepare # them and call the super method return super if(direct_write!) # create name for orginal temp file and destination temp file original_file_path, orig_is_temp = prepare_original_file will_delete_source = orig_is_temp || @delete_original_file destination_thumbnail_file_path = File.join(Dir.tmpdir, "thumbnail_#{random_tempfile_filename}.gif") begin # Begin the file creation operation self.class.benchmark("\033[36mIipData\033[0m Making thumb and pyramid for #{self.id}", Logger::INFO) do TaliaUtil::ImageConversions::create_thumb(original_file_path, destination_thumbnail_file_path) create_pyramid(original_file_path) # Run the super implementation for the thumbnail # We will simply tell the system that we have to move the newly create # thumb file @file_data_to_write = DataPath.new(destination_thumbnail_file_path) @delete_original_file = true end # end benchmarking super ensure # delete teƒmp files File.delete original_file_path if(File.exists?(original_file_path) && will_delete_source) end end |