meribah 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +3 -0
- data/app/controllers/meribah/download_controller.rb +44 -0
- data/config/routes.rb +4 -0
- data/lib/meribah.rb +91 -0
- data/lib/tasks/meribah_tasks.rake +4 -0
- metadata +61 -0
data/README.rdoc
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
module Meribah
|
4
|
+
class DownloadController < ::ApplicationController
|
5
|
+
def index
|
6
|
+
yaml_path = nil
|
7
|
+
Meribah::MUTEX.synchronize do
|
8
|
+
# Return if there are no files
|
9
|
+
if session[:meribah_files].nil? || session[:meribah_files].empty?
|
10
|
+
redirect_to '/404.html'
|
11
|
+
return
|
12
|
+
end
|
13
|
+
|
14
|
+
# Get the path to the YAML file
|
15
|
+
files = session[:meribah_files]
|
16
|
+
yaml_path = "#{Rails.root}/tmp/#{files.shift}"
|
17
|
+
session[:meribah_files] = files
|
18
|
+
unless File.exist? yaml_path
|
19
|
+
redirect_to '/404.html'
|
20
|
+
return
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# Extract path and options from YAML file
|
25
|
+
::Meribah::TEMPFILES.delete(yaml_path)
|
26
|
+
yaml_file = File.open(yaml_path)
|
27
|
+
path,options = YAML::load(yaml_file)
|
28
|
+
yaml_file.close
|
29
|
+
File::unlink(yaml_file)
|
30
|
+
|
31
|
+
unless File.exist? path
|
32
|
+
redirect_to '/404.html'
|
33
|
+
return
|
34
|
+
end
|
35
|
+
|
36
|
+
# Send the file
|
37
|
+
file = File.open(path,'rb')
|
38
|
+
data = file.read
|
39
|
+
file.close
|
40
|
+
File::unlink(path) if ::Meribah::TEMPFILES.delete(path) # Delete it if it was a temp file
|
41
|
+
send_data data, options
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/config/routes.rb
ADDED
data/lib/meribah.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'active_support/dependencies'
|
2
|
+
require 'set'
|
3
|
+
require 'thread'
|
4
|
+
require 'yaml'
|
5
|
+
require 'action_controller'
|
6
|
+
|
7
|
+
module Meribah
|
8
|
+
META_TAG = "<meta http-equiv='refresh' content='1;url=/meribah/download' />"
|
9
|
+
JAVASCRIPT_TAG = "<script type='text/javascript'>window.open('/meribah/download')</script>"
|
10
|
+
IFRAME_TAG = "<iframe style='display:none;' src='/meribah/download'></iframe>"
|
11
|
+
|
12
|
+
# Our host application root path
|
13
|
+
# We set this when the engine is initialized
|
14
|
+
mattr_accessor :app_root
|
15
|
+
|
16
|
+
# Yield self on setup for nice config blocks
|
17
|
+
def self.setup
|
18
|
+
yield self
|
19
|
+
end
|
20
|
+
|
21
|
+
# Generate a temporary file name
|
22
|
+
def self.temp_file()
|
23
|
+
while true
|
24
|
+
path = "#{Rails.root}/tmp/meribah_#{rand(1000000..9999999)}"
|
25
|
+
return path unless File.exist? path
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
TEMPFILES = Set.new
|
30
|
+
MUTEX = Mutex.new
|
31
|
+
|
32
|
+
class Engine < Rails::Engine
|
33
|
+
config.mount_at = '/'
|
34
|
+
|
35
|
+
# Check the gem config
|
36
|
+
initializer "check config" do |app|
|
37
|
+
|
38
|
+
# make sure mount_at ends with trailing slash
|
39
|
+
config.mount_at += '/' unless config.mount_at.last == '/'
|
40
|
+
end
|
41
|
+
|
42
|
+
initializer "static assets" do |app|
|
43
|
+
app.middleware.use ::ActionDispatch::Static, "#{root}/public"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
module Controller
|
48
|
+
extend ActiveSupport::Concern
|
49
|
+
|
50
|
+
# To send a file, store options in the YML and then send them
|
51
|
+
def meribah_send_file(path, options={})
|
52
|
+
yaml_file = File.open(Meribah::temp_file(),'w')
|
53
|
+
yaml_file.write([path,options].to_yaml)
|
54
|
+
yaml_file.close
|
55
|
+
session[:meribah_files] ||= []
|
56
|
+
session[:meribah_files] << File::basename(yaml_file.path)
|
57
|
+
::Meribah::TEMPFILES << yaml_file.path
|
58
|
+
end
|
59
|
+
|
60
|
+
# To send data, save data to a temporary file, then send
|
61
|
+
def meribah_send_data(data, options={})
|
62
|
+
file = File.open(Meribah::temp_file(), 'wb')
|
63
|
+
file.write(data)
|
64
|
+
file.close
|
65
|
+
::Meribah::TEMPFILES << file.path
|
66
|
+
meribah_send_file(file.path,options)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
module ApplicationHelper
|
72
|
+
def meribah_meta_tag
|
73
|
+
meribah_tag(Meribah::META_TAG.html_safe)
|
74
|
+
end
|
75
|
+
|
76
|
+
def meribah_javascript_tag
|
77
|
+
meribah_tag(Meribah::JAVASCRIPT_TAG.html_safe)
|
78
|
+
end
|
79
|
+
|
80
|
+
def meribah_iframe_tag
|
81
|
+
meribah_tag(Meribah::IFRAME_TAG.html_safe)
|
82
|
+
end
|
83
|
+
|
84
|
+
private
|
85
|
+
def meribah_tag(tag)
|
86
|
+
return nil if session[:meribah_files].nil? || session[:meribah_files].empty?
|
87
|
+
return tag
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
::ActionController::Base.send :include, Meribah::Controller
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: meribah
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.4
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Chris Dollard
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-02 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: &12364380 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *12364380
|
25
|
+
description:
|
26
|
+
email: chrisdollard@dolphinmicro.com
|
27
|
+
executables: []
|
28
|
+
extensions: []
|
29
|
+
extra_rdoc_files:
|
30
|
+
- README.rdoc
|
31
|
+
files:
|
32
|
+
- lib/meribah.rb
|
33
|
+
- app/controllers/meribah/download_controller.rb
|
34
|
+
- config/routes.rb
|
35
|
+
- lib/tasks/meribah_tasks.rake
|
36
|
+
- README.rdoc
|
37
|
+
homepage:
|
38
|
+
licenses: []
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
requirements: []
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 1.8.11
|
58
|
+
signing_key:
|
59
|
+
specification_version: 3
|
60
|
+
summary: Send a file AND render a page
|
61
|
+
test_files: []
|