Sha256: 40627084de057e3c4f2bda24967c7b9341df99bf94481cf424dd8c4c3f5e36a5

Contents?: true

Size: 1.83 KB

Versions: 5

Compression:

Stored size: 1.83 KB

Contents

require 'fileutils'
module Orange
  class AssetResource < Orange::ModelResource
    use Orange::Asset
    call_me :assets
    
    def stack_init
      orange[:admin, true].add_link("Content", :resource => @my_orange_name, :text => 'Assets')
      orange[:radius, true].context.define_tag "asset" do |tag|
        if tag.attr['id']
          (m = model_class.first(:id => tag.attr['id'])) ? m.to_asset_tag : 'Invalid Asset'
        else
          ''
        end
      end
    end
    
    def onNew(packet, params = {})
      m = false
      if(file = params['file'][:tempfile])
        file_path = orange.app_dir('assets','uploaded', params['file'][:filename]) if params['file'][:filename]
        # Check for secondary file (useful for videos/images with thumbnails)
        if(params['file2'] && secondary = params['file2'][:tempfile])
          secondary_path = orange.app_dir('assets','uploaded', params['file2'][:filename])
        else
          secondary_path = nil
        end
        # Move the files
        FileUtils.cp(file.path, file_path)
        FileUtils.cp(secondary.path, secondary_path) if secondary_path
        
        params['path'] = params['file'][:filename] if file_path
        params['secondary_path'] = params['file2'][:filename] if secondary_path
        params['mime_type'] = params['file'][:type] if file_path
        params['secondary_mime_type'] = params['file2'][:type] if secondary_path
        params.delete('file')
        params.delete('file2')
        
        m = model_class.new(params)
      end
      m
    end
    
    def onDelete(packet, m, opts = {})
      begin
        FileUtils.rm(orange.app_dir('assets','uploaded', m.path)) if m.path
        FileUtils.rm(orange.app_dir('assets','uploaded', m.secondary_path)) if m.secondary_path
      rescue
        # Problem deleting file
      end
      m.destroy if m
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
orange-0.2.6 lib/orange-more/assets/resources/asset_resource.rb
orange-0.2.5 lib/orange-more/assets/resources/asset_resource.rb
orange-0.2.4 lib/orange-more/assets/resources/asset_resource.rb
orange-0.2.3 lib/orange-more/assets/resources/asset_resource.rb
orange-0.1.10 lib/orange-more/assets/resources/asset_resource.rb