Sha256: 129a88cfe3428e2d5e58be551995fa1afc496f99aeca020c2f6f88c1581b3ab0

Contents?: true

Size: 1.86 KB

Versions: 11

Compression:

Stored size: 1.86 KB

Contents

module RailsConnector
  # A binary containing the blob data of a Content or CMS management
  # data of Attribute (fields), ObjClass (object classes), for example.
  class Blob < CmsBaseModel
    self.primary_key = "blob_name"

    class << self
      # Does not retrieve the column blob_data.
      def find_with_excluded_blob_data(primary_key)
        select(:blob_name, :blob_length).find(primary_key)
      end

      alias_method :find_without_excluded_blob_data, :find
      alias_method :find, :find_with_excluded_blob_data
    end

    def length
      blob_length
    end

    def data
      @data ||= self.class.find_without_excluded_blob_data(id).blob_data
    end

    def path_of_stored_data(use_cached_file_if_older_than = Time.now)
      store_data if !File.exist?(path) || file_size_differs? || stale?(use_cached_file_if_older_than)
      path
    end

    def self.cache_dir
      Configuration.blob_cache_dir || File.join(Rails.root, %w(tmp cache))
    end

    def self.initialize_blob_streaming_for(adapter_name)
      # Redefines store_data:
      case adapter_name
      when /mysql$/i
        raise "Adapter 'mysql' no longer supported. " +
              "Please change adapter in your database.yml to 'mysql2'."
      when /mysql2/i
        require "rails_connector/blob_mysql"
        include BlobMysql
      when /oracle/i
        require "rails_connector/blob_oracle"
        include BlobOracle
      end
    end

    private

    def path
      "#{Blob.cache_dir}/#{id}"
    end

    def store_data
      store_data_atomically do |f|
        f << data
      end
    end

    def store_data_atomically(&block)
      tmp_file = "#{path}.#{Process.pid}"
      File.open(tmp_file, "wb", &block)
      File.rename(tmp_file, path)
    end

    def stale?(time)
      time > File.mtime(path)
    end

    def file_size_differs?
      blob_length != File.stat(path).size
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
infopark_fiona_connector-7.0.1.6.1.6.rc4 lib/rails_connector/blob.rb
infopark_fiona_connector-7.0.1.6.1.6.rc3 lib/rails_connector/blob.rb
infopark_fiona_connector-7.0.1.6.1.6.rc2 lib/rails_connector/blob.rb
infopark_fiona_connector-7.0.1.6.1.6.rc1 lib/rails_connector/blob.rb
infopark_fiona_connector-7.0.1.5.2.7.rc1 lib/rails_connector/blob.rb
infopark_fiona_connector-7.0.1.5.2.4.rc2 lib/rails_connector/blob.rb
infopark_fiona_connector-7.0.1.5.2.4.rc1 lib/rails_connector/blob.rb
infopark_fiona_connector-7.0.1.5.2.3.rc8 lib/rails_connector/blob.rb
infopark_fiona_connector-7.0.1.5.2.3.rc7 lib/rails_connector/blob.rb
infopark_fiona_connector-7.0.1.5.2.3.rc6 lib/rails_connector/blob.rb
infopark_fiona_connector-7.0.1.5.2.3.rc5 lib/rails_connector/blob.rb