temper 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +50 -0
- data/Rakefile +2 -0
- data/lib/temper/version.rb +3 -0
- data/lib/temper.rb +82 -0
- data/temper.gemspec +20 -0
- metadata +101 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Andrew Eberbach
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# Temper
|
2
|
+
|
3
|
+
A gem for a slightly smarter temporary file and temporary directory maintenance. It combines the best parts of Upfile from paperclip and Rack::Multipart::UploadedFile from rack.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'temper'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install temper
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
If you want a tempfile that can hold an `original_filename` and `content_type` just do this:
|
22
|
+
|
23
|
+
Temper::File.new(:original_filename => "my_file.txt")
|
24
|
+
|
25
|
+
or
|
26
|
+
|
27
|
+
Temper::File.new(:original_filename => "foo.xls", :content_type => "application/vnd.ms-excel")
|
28
|
+
|
29
|
+
or if you want to use an existing file as a base:
|
30
|
+
|
31
|
+
Temper::File.new(:content => "/path/to/a/file", :original_filename => "foo.xls", :content_type => "application/vnd.ms-excel")
|
32
|
+
|
33
|
+
There's also a StringIO wrapper:
|
34
|
+
|
35
|
+
Temper:StringIO.new("some content", :original_filename => "foobar.csv")
|
36
|
+
|
37
|
+
There's a handy shortcut for both of the above:
|
38
|
+
|
39
|
+
Temper::Fi.new(:original_filename => "my_file.txt")
|
40
|
+
Temper::Si.new("some content", :original_filename => "foobar.csv")
|
41
|
+
|
42
|
+
There's also a `fingerprint` method that will calculate the MD5 hash of the content.
|
43
|
+
|
44
|
+
## Contributing
|
45
|
+
|
46
|
+
1. Fork it
|
47
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
48
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
49
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
50
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/temper.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'tempfile'
|
2
|
+
require 'stringio'
|
3
|
+
require 'mime/types'
|
4
|
+
|
5
|
+
module Temper
|
6
|
+
class Base
|
7
|
+
attr_accessor :original_filename
|
8
|
+
attr_accessor :content_type
|
9
|
+
|
10
|
+
def initialize(options = {})
|
11
|
+
raise RuntimeException, "No @storage set" unless @storage
|
12
|
+
self.original_filename = options[:original_filename] || ::File.basename(path)
|
13
|
+
self.content_type = infer_content_type || "text/plain"
|
14
|
+
end
|
15
|
+
|
16
|
+
def fingerprint
|
17
|
+
@fingerprint ||= Digest::MD5.hexdigest(self.read)
|
18
|
+
end
|
19
|
+
|
20
|
+
def respond_to?(*args)
|
21
|
+
super or @storage.respond_to?(*args)
|
22
|
+
end
|
23
|
+
|
24
|
+
def method_missing(method_name, *args, &block) #:nodoc:
|
25
|
+
@storage.__send__(method_name, *args, &block)
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
# Infer the MIME-type of the file from the extension.
|
31
|
+
def infer_content_type
|
32
|
+
types = MIME::Types.type_for(self.original_filename)
|
33
|
+
if types.length == 0
|
34
|
+
type_from_file_command
|
35
|
+
elsif types.length == 1
|
36
|
+
types.first.content_type
|
37
|
+
else
|
38
|
+
iterate_over_array_to_find_best_option(types)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def iterate_over_array_to_find_best_option(types)
|
43
|
+
types.reject {|type| type.content_type.match(/\/x-/) }.first
|
44
|
+
end
|
45
|
+
|
46
|
+
def type_from_file_command
|
47
|
+
# On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist.
|
48
|
+
type = (self.original_filename.match(/\.(\w+)$/)[1] rescue "octet-stream").downcase
|
49
|
+
mime_type = (run("file", "-b --mime-type :file", :file => self.path).split(':').last.strip rescue "application/x-#{type}")
|
50
|
+
mime_type = "application/x-#{type}" if mime_type.match(/\(.*?\)/)
|
51
|
+
mime_type
|
52
|
+
end
|
53
|
+
|
54
|
+
def run(command, *arguments)
|
55
|
+
Cocaine::CommandLine.new(command, *arguments).run
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
class File < Base
|
61
|
+
def initialize(options = {})
|
62
|
+
path = options[:content]
|
63
|
+
raise "#{path} file does not exist" if path && !::File.exist?(path)
|
64
|
+
@storage = Tempfile.new("temperfi")
|
65
|
+
@storage.set_encoding(Encoding::BINARY) if @storage.respond_to?(:set_encoding)
|
66
|
+
@storage.binmode unless options.key?(:binary) && !options[:binary]
|
67
|
+
FileUtils.copy_file(path, @storage.path) if path && !::File.exist?(path)
|
68
|
+
super options
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
Fi = File unless defined?(Fi)
|
73
|
+
|
74
|
+
class StringIO < Base
|
75
|
+
def initialize(options = {})
|
76
|
+
@storage = ::StringIO.new(options[:content].to_s)
|
77
|
+
super options.merge(:original_filename => "stringio.txt")
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
Si = StringIO unless defined?(Si)
|
82
|
+
end
|
data/temper.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/temper/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Andrew Eberbach"]
|
6
|
+
gem.email = ["andrew@ebertech.ca"]
|
7
|
+
gem.description = %q{A gem for a slightly smarter temporary file and temporary directory maintenance}
|
8
|
+
gem.summary = %q{Combines the best parts of Upfile from paperclip and Rack::Multipart::UploadedFile from rack.}
|
9
|
+
gem.homepage = "https://github.com/ebertech/temper"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "temper"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Temper::VERSION
|
17
|
+
|
18
|
+
gem.add_runtime_dependency "mime-types"
|
19
|
+
gem.add_runtime_dependency "cocaine"
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: temper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 59
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 9
|
9
|
+
- 0
|
10
|
+
version: 0.9.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Andrew Eberbach
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-05-28 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: mime-types
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: cocaine
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 3
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
version: "0"
|
46
|
+
type: :runtime
|
47
|
+
version_requirements: *id002
|
48
|
+
description: A gem for a slightly smarter temporary file and temporary directory maintenance
|
49
|
+
email:
|
50
|
+
- andrew@ebertech.ca
|
51
|
+
executables: []
|
52
|
+
|
53
|
+
extensions: []
|
54
|
+
|
55
|
+
extra_rdoc_files: []
|
56
|
+
|
57
|
+
files:
|
58
|
+
- .gitignore
|
59
|
+
- Gemfile
|
60
|
+
- LICENSE
|
61
|
+
- README.md
|
62
|
+
- Rakefile
|
63
|
+
- lib/temper.rb
|
64
|
+
- lib/temper/version.rb
|
65
|
+
- temper.gemspec
|
66
|
+
homepage: https://github.com/ebertech/temper
|
67
|
+
licenses: []
|
68
|
+
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options: []
|
71
|
+
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
hash: 3
|
80
|
+
segments:
|
81
|
+
- 0
|
82
|
+
version: "0"
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
hash: 3
|
89
|
+
segments:
|
90
|
+
- 0
|
91
|
+
version: "0"
|
92
|
+
requirements: []
|
93
|
+
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 1.8.21
|
96
|
+
signing_key:
|
97
|
+
specification_version: 3
|
98
|
+
summary: Combines the best parts of Upfile from paperclip and Rack::Multipart::UploadedFile from rack.
|
99
|
+
test_files: []
|
100
|
+
|
101
|
+
has_rdoc:
|