mongoid_unpack_paperclip 0.0.1
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/.gitignore +18 -0
- data/LICENSE +20 -0
- data/README.markdown +20 -0
- data/lib/mongoid_unpack_paperclip.rb +60 -0
- data/mongoid_unpack_paperclip.gemspec +16 -0
- metadata +82 -0
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2013 SunshineLibrary
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
mongoid_unpack_paperclip
|
2
|
+
========================
|
3
|
+
给含有paperclip的Mongoid 支持解压缩包和清理的封装。
|
4
|
+
|
5
|
+
|
6
|
+
Usage
|
7
|
+
------------------------
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
class ExampleModel
|
11
|
+
include Mongoid::Paperclip
|
12
|
+
include Mongoid::UnpackPaperclip
|
13
|
+
|
14
|
+
def process
|
15
|
+
self.unpack_paperclip do
|
16
|
+
... # your implementation
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
```
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
module ::Mongoid
|
4
|
+
module UnpackPaperclip
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
field :unpack_paperclip_errors, type: Array, default: []
|
9
|
+
end
|
10
|
+
|
11
|
+
def unpack_paperclip &blk
|
12
|
+
return false if not blk
|
13
|
+
item = self
|
14
|
+
|
15
|
+
# 1. unpack
|
16
|
+
# 1.1 fetch paperclip object
|
17
|
+
paperclip_regexp = /_([a-z_]+)_post_process_callbacks/
|
18
|
+
paperclip_method = item.methods.detect {|m| m.match(paperclip_regexp) }.to_s.match(paperclip_regexp)[1]
|
19
|
+
return false if not File.exists? item.send(paperclip_method).path
|
20
|
+
paperclip_path = item.send(paperclip_method).path
|
21
|
+
FileUtils.chdir File.dirname(paperclip_path)
|
22
|
+
extract_path = File.basename(paperclip_path).split('.')[0]
|
23
|
+
# 1.2 unpack zip
|
24
|
+
pre_uncompress_command = case extname = File.extname(paperclip_path)
|
25
|
+
when ".zip"
|
26
|
+
"unzip -oqqd "
|
27
|
+
when ".7z"
|
28
|
+
"7z x -yo"
|
29
|
+
else
|
30
|
+
# return false directly
|
31
|
+
return "#{extname} is invalid"
|
32
|
+
end
|
33
|
+
root_dir = File.join(Dir.pwd, extract_path)
|
34
|
+
|
35
|
+
item.unpack_paperclip_errors = []
|
36
|
+
item.save validate: false
|
37
|
+
result = nil
|
38
|
+
|
39
|
+
begin
|
40
|
+
system "#{pre_uncompress_command}#{extract_path} #{paperclip_path}"
|
41
|
+
# 1.3 look dir
|
42
|
+
FileUtils.chdir extract_path
|
43
|
+
|
44
|
+
# 2. yield process
|
45
|
+
result = yield Dir.pwd
|
46
|
+
|
47
|
+
rescue => e
|
48
|
+
item.unpack_paperclip_errors = e.backtrace.push(e).map(&:inspect)
|
49
|
+
item.save validate: false
|
50
|
+
end
|
51
|
+
|
52
|
+
# 3. clean files
|
53
|
+
FileUtils.rm_rf root_dir
|
54
|
+
FileUtils.chdir Rails.root
|
55
|
+
|
56
|
+
return result
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'mongoid_unpack_paperclip'
|
3
|
+
s.version = '0.0.1'
|
4
|
+
s.date = '2013-12-05'
|
5
|
+
s.summary = File.read("README.markdown").split(/===+/)[1].strip.split("\n")[0]
|
6
|
+
s.description = s.summary
|
7
|
+
s.authors = ["David Chen"]
|
8
|
+
s.email = 'mvjome@gmail.com'
|
9
|
+
s.homepage = 'https://github.com/SunshineLibrary/mongoid_unpack_paperclip/'
|
10
|
+
s.license = 'MIT'
|
11
|
+
|
12
|
+
s.add_dependency "mongoid"
|
13
|
+
s.add_dependency "activesupport", "> 3.2"
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid_unpack_paperclip
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- David Chen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-12-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: mongoid
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: activesupport
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>'
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '3.2'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>'
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '3.2'
|
46
|
+
description: 给含有paperclip的Mongoid 支持解压缩包和清理的封装。
|
47
|
+
email: mvjome@gmail.com
|
48
|
+
executables: []
|
49
|
+
extensions: []
|
50
|
+
extra_rdoc_files: []
|
51
|
+
files:
|
52
|
+
- .gitignore
|
53
|
+
- LICENSE
|
54
|
+
- README.markdown
|
55
|
+
- lib/mongoid_unpack_paperclip.rb
|
56
|
+
- mongoid_unpack_paperclip.gemspec
|
57
|
+
homepage: https://github.com/SunshineLibrary/mongoid_unpack_paperclip/
|
58
|
+
licenses:
|
59
|
+
- MIT
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 1.8.23
|
79
|
+
signing_key:
|
80
|
+
specification_version: 3
|
81
|
+
summary: 给含有paperclip的Mongoid 支持解压缩包和清理的封装。
|
82
|
+
test_files: []
|