Module: TaliaCore::DataTypes::FileStore

Included in:
FileRecord
Defined in:
lib/talia_core/data_types/file_store.rb

Defined Under Namespace

Modules: ClassMethods Classes: DataPath

Instance Method Summary collapse

Instance Method Details

#all_textObject

returns the complete text



59
60
61
62
63
64
# File 'lib/talia_core/data_types/file_store.rb', line 59

def all_text
  if(!is_file_open?)
    open_file
  end
  @file_handle.read(self.size)
end

#assign_type(content_type) ⇒ Object

Assign the STI subclass, perfoming a mime-type lookup.



116
117
118
# File 'lib/talia_core/data_types/file_store.rb', line 116

def assign_type(content_type)
  self.type = MimeMapping.class_type_from(content_type).name
end

#create_from_data(file_location, data, options = {}) ⇒ Object

Add data as string into file



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/talia_core/data_types/file_store.rb', line 43

def create_from_data(file_location, data, options = {})
  # close file if opened
  close_file
    
  # Set the location for the record
  self.location = file_location
    
  if(data.respond_to?(:read))
    @file_data_to_write = data.read
  else
    @file_data_to_write = data
  end
    
end

#create_from_file(location, file_path, delete_original = false) ⇒ Object

This will create the data object from a given file. This will simply move the given file to the correct location upon save. This will avoid multiple read/write operations during import.

The original file must not be touched by external processes until the record is saved.

If the delete_original flag is set, the original file will be removed on save



35
36
37
38
39
40
# File 'lib/talia_core/data_types/file_store.rb', line 35

def create_from_file(location, file_path, delete_original = false)
  close_file
  self.location = location
  @file_data_to_write = DataPath.new(file_path)
  @delete_original_file = delete_original
end

#fileObject

This is a placeholder in case file is used in a form.



67
# File 'lib/talia_core/data_types/file_store.rb', line 67

def file() nil; end

#file=(file_data) ⇒ Object

Assign the file data (StringIO or File).



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/talia_core/data_types/file_store.rb', line 70

def file=(file_data)
  return nil if file_data.nil? || file_data.size == 0 
  self.assign_type file_data.content_type
  self.location = file_data.original_filename
  if file_data.is_a?(StringIO)
    file_data.rewind
    self.temp_data = file_data.read
  else
    self.temp_path = file_data.path
  end
  @save_attachment = true
end

#is_file_open?Boolean

Return true if the specified data file is open, false otherwise

Returns:

  • (Boolean)


110
111
112
# File 'lib/talia_core/data_types/file_store.rb', line 110

def is_file_open?
  (@file_handle != nil)
end

#write_file_after_saveObject



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
# File 'lib/talia_core/data_types/file_store.rb', line 83

def write_file_after_save 
  # check if there are data to write
  return unless(@file_data_to_write)
    
  # check if file already exists
#        raise(RuntimeError, "File already exists: #{file_path}") if(File.exists?(file_path))

  begin
    self.class.benchmark("\033[36m\033[1m\033[4mFileStore\033[0m Saving file for #{self.id}") do
      # create data directory path
      FileUtils.mkdir_p(data_directory)
    
      if(@file_data_to_write.is_a?(DataPath))
        copy_data_file
      else
        save_cached_data
      end
    
      @file_data_to_write = nil
    end
  rescue Exception => e
    assit_fail("Exception on writing file #{self.location}: #{e}")
  end

end