Sha256: 92667293ff218eb0f19d140d7133b65b5f377085825300825ed1cd759bc4c412

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

require 'fileutils'
module Orange
  class AssetResource < Orange::ModelResource
    use Orange::Asset
    call_me :assets
    def afterLoad
      orange[:admin, true].add_link("Content", :resource => @my_orange_name, :text => 'Assets')
      orange.register(:stack_loaded) do
        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
    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

1 entries across 1 versions & 1 rubygems

Version Path
orange-0.1.8 lib/orange-more/assets/resources/asset_resource.rb