wfl_file_base 0.0.2

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b1b56b3c13fa48b7e8c3859d64e8ee75c0d524ab
4
+ data.tar.gz: 24faf6d8777d3404ff3c625b063c1d1d25f16c1f
5
+ SHA512:
6
+ metadata.gz: 25c11169c55bfebb6b129cf98ee10ba02dcd25cfe1f53d9d7a01193889921e729a72c69da66eda26da1f62d34274e08d7abbb33bec316980b60a8239287e4c20
7
+ data.tar.gz: f86afd4c191ecc0975c3fe11a985c57b2fc264b4ab1ee974007fcd26a2a217696bbfeae028267db2fa3b43191e09ebb7a894331a7cdc8a746ee201764f15399b
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in wfl_file_base.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 wpzero
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,155 @@
1
+ # WflFileBase
2
+
3
+ this gem is used to map a file to a record based on the activerecord.
4
+ this gem is very simple and small. but I think it can fit most the requests of u.
5
+ this gem is similar to carrierwave. I must admit 'yes'
6
+ before I write this gem, I use the carrierwave in my project, my project has the function network disk. but the carrierwave can not bind the folder to a record(so not fit my request and the carrier is too fat).so I write a gem.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'wfl_file_base'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install wfl_file_base
21
+
22
+ ## Usage
23
+
24
+ in application.rb
25
+
26
+ ```
27
+ WflFileBase.configure do |config|
28
+ config.root = APP_ROOT + '/public/'
29
+ config.tmp_dir = 'tmp'
30
+ end
31
+ ```
32
+ is used to set the tmp_dir and the application root folder.
33
+
34
+
35
+
36
+
37
+ create WflFileBase::Base subclass and override the filename , dir_name the method, to set the file location.
38
+
39
+ such as:
40
+
41
+ ```
42
+ require 'digest/md5'
43
+ require 'carrierwave/processing/mime_types'
44
+ require_relative '../services/file_system/file_path_setting'
45
+ require_relative '../services/file_system/base/md5_process'
46
+
47
+ class NormalFileBase < WflFileBase::Base
48
+
49
+ pre_process :save_content_type_and_size_in_model
50
+
51
+ def store_dir
52
+ FileSys::FilePathSetting.resource_path + '/' + md5init
53
+ end
54
+
55
+ def filename
56
+ @name ||= md5
57
+ end
58
+
59
+ def filename_was
60
+ model.send(:"#{column}_was")
61
+ end
62
+
63
+ def md5
64
+ # creat process
65
+ if self.store
66
+ puts '---------new'
67
+ file.rewind
68
+ @md5 ||= FileSys::Md5Process.send(:get_md5_name, :content => file.read.to_s, :size => file.size.to_s)
69
+ # read process
70
+ else
71
+ puts '---------old'
72
+ model.read_attribute(column)
73
+ end
74
+ end
75
+
76
+ def md5init
77
+ md5[0].upcase
78
+ end
79
+
80
+ def save_content_type_and_size_in_model
81
+ model.content_type = file.mime_type if file.mime_type
82
+ model.file_size = file.size
83
+ end
84
+ end
85
+ ```
86
+
87
+
88
+
89
+ use wfl_mount_file_base activereord macro to bind column and WflFileBase::Base subclass.
90
+
91
+ such as:
92
+
93
+ ```
94
+ require_relative 'md5_process'
95
+
96
+ module FileSys
97
+ class FileResource < ActiveRecord::Base
98
+
99
+ wfl_mount_file_base :file, NormalFileBase
100
+
101
+ before_save :update_file_attributes
102
+
103
+ class << self
104
+ def find_or_create_file_resource file
105
+ file=file[:tempfile] if file.is_a?(Hash) && file[:tempfile].is_a?(Tempfile)
106
+ file.rewind
107
+ file_resource = self.find_by_file(Md5Process.get_md5_name(:content => file.read.to_s, :size => file.size.to_s))
108
+ if !file_resource
109
+ file_resource = FileSys::FileResource.create(:file => file)
110
+ end
111
+ file_resource
112
+ end
113
+
114
+ def find_by_path url
115
+ find_by_file File.basename(url)
116
+ end
117
+ end
118
+
119
+ def read
120
+ file.read
121
+ end
122
+
123
+ private
124
+ def update_file_attributes
125
+ if file.present?
126
+ self.md5_val = file.md5
127
+ end
128
+ end
129
+ end
130
+ end
131
+ ```
132
+
133
+ now we can use FileSys::FileResource simplely.
134
+
135
+ ```
136
+ f = FileSys::FileResource.new
137
+ f.file = File.open('tmp.txt')
138
+ f.save
139
+ f.file # => <SymbleFileBase>
140
+ f.file.file # => <File>
141
+ f.file.read # => 'String'
142
+ ```
143
+
144
+
145
+
146
+
147
+
148
+
149
+ ## Contributing
150
+
151
+ 1. Fork it ( https://github.com/[my-github-username]/wfl_file_base/fork )
152
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
153
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
154
+ 4. Push to the branch (`git push origin my-new-feature`)
155
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,22 @@
1
+ require "wfl_file_base/version"
2
+
3
+ module WflFileBase
4
+
5
+ class << self
6
+ attr_reader :config
7
+ def configure
8
+ @config = WflFileBaseConfig.instance
9
+ yield(@config) if block_given?
10
+ end
11
+ end
12
+
13
+
14
+ class WflFileBaseConfig
15
+ attr_accessor :root, :tmp_dir
16
+ include Singleton
17
+ end
18
+ end
19
+
20
+ require "wfl_file_base/file"
21
+ require "wfl_file_base/base"
22
+ require "wfl_file_base/record"
@@ -0,0 +1,162 @@
1
+
2
+ module WflFileBase
3
+ class Base
4
+ attr_accessor :column, :model, :file, :store
5
+
6
+ def initialize column, model
7
+ self.column = column
8
+ self.model = model
9
+ self.store = false
10
+ if model.read_attribute(column).is_a? File
11
+ self.file = model.read_attribute(column)
12
+ elsif model.read_attribute(column).is_a? Tempfile
13
+ self.file = model.read_attribute(column)
14
+ elsif model.read_attribute(column).is_a?(Hash) && model.read_attribute(column)[:tempfile].is_a?(Tempfile)
15
+ self.file = model.read_attribute(column)[:tempfile]
16
+ elsif self.filename.is_a?(String) && !self.filename.empty? && File.exist?(self.path)
17
+ self.file = File.open(self.path)
18
+ end
19
+ end
20
+
21
+ def store_dir
22
+ model.id
23
+ end
24
+
25
+ def filename
26
+ 'original'
27
+ end
28
+
29
+ def path
30
+ "#{WflFileBase.config.root}#{self.url}"
31
+ end
32
+
33
+ def url
34
+ "#{self.store_dir}/#{self.filename}"
35
+ end
36
+
37
+ def abs_store_dir
38
+ "#{WflFileBase.config.root}#{self.store_dir}"
39
+ end
40
+
41
+
42
+ def read
43
+ if file && !File.directory?(file)
44
+ file.send(:read)
45
+ else
46
+ nil
47
+ end
48
+ end
49
+
50
+ def method_missing(method, *args)
51
+ if file
52
+ file.methods.include?(method) ? file.send(method, *args) : super
53
+ end
54
+ end
55
+
56
+ def respond_to? method
57
+ file.methods.include?(method) || super
58
+ end
59
+
60
+ def is_new?
61
+ model.read_attribute(column).is_a?(File) || model.read_attribute(column).is_a?(Tempfile) || (model.read_attribute(column).is_a?(Hash) && model.read_attribute(column)[:tempfile].is_a?(Tempfile))
62
+ end
63
+
64
+ class << self
65
+ attr_reader :wfl_process_methods, :wfl_pre_process_methods
66
+
67
+ def process method_name
68
+ @wfl_process_methods ||= []
69
+ @wfl_process_methods.push method_name
70
+ end
71
+
72
+ def pre_process method_name
73
+ @wfl_pre_process_methods ||= []
74
+ @wfl_pre_process_methods.push method_name
75
+ end
76
+ end
77
+
78
+ private
79
+
80
+ def write_column
81
+ self.store = true
82
+ model.send(:write_attribute, :"#{column}", self.filename)
83
+ delete_old_file
84
+ if self.class.wfl_pre_process_methods
85
+ self.class.wfl_pre_process_methods.each do |process_method|
86
+ self.send(process_method)
87
+ end
88
+ end
89
+ end
90
+
91
+ def build_file
92
+ FileUtils.mkdir_p(abs_store_dir) unless File.exists?(abs_store_dir)
93
+ temple_file = self.file
94
+ if temple_file.is_a?(Tempfile)
95
+ FileUtils.copy(tmpfile_to_file(temple_file), path)
96
+ File.delete(tmp_file_path) if File.exists?(tmp_file_path)
97
+ elsif temple_file.is_a?(Hash) && temple_file[:tempfile].is_a?(Tempfile)
98
+ FileUtils.copy(tmpfile_to_file(temple_file[:tempfile]), path)
99
+ File.delete(tmp_file_path) if File.exists?(tmp_file_path)
100
+ elsif temple_file.is_a?(File) && File.directory?(temple_file)
101
+ FileUtils.mkdir_p(path)
102
+ elsif temple_file.is_a?(File)
103
+ FileUtils.copy temple_file, path
104
+ else
105
+ return nil
106
+ end
107
+
108
+ self.file = File.open(path)
109
+
110
+ if self.class.wfl_process_methods
111
+ self.class.wfl_process_methods.each do |process_method|
112
+ self.send(process_method)
113
+ end
114
+ end
115
+ self.store = false
116
+ end
117
+
118
+ def delete_old_file
119
+ if !model.new_record? && !model.send(:"#{column}_was").nil? && !model.send(:"#{column}_was").empty?
120
+ File.delete(path_was) if File.exist?(path_was) && !File.directory?(path_was)
121
+ end
122
+ end
123
+
124
+ def path_was
125
+ "#{WflFileBase.config.root}#{self.store_dir}/#{self.filename_was}"
126
+ end
127
+
128
+ def remove_file
129
+ if File.exist?(file) && File.directory?(file)
130
+ FileUtils.remove_dir file
131
+ elsif File.exist?(file)
132
+ File.delete file
133
+ end
134
+ end
135
+
136
+ def tmpfile_to_file file
137
+ if file.is_a? Tempfile
138
+ FileUtils.mkdir_p(tmp_file_dir) unless File.exists?(tmp_file_dir)
139
+ File.delete(tmp_file_path) if File.exists?(tmp_file_path)
140
+ file.rewind
141
+ File.open(tmp_file_path, 'w') do |f|
142
+ f.write(file.read)
143
+ end
144
+ File.open(tmp_file_path)
145
+ end
146
+ end
147
+
148
+ def tmp_file_path
149
+ "#{tmp_file_dir}/#{tmp_file_name}"
150
+ end
151
+
152
+ def tmp_file_name
153
+ @uu_name ||= SecureRandom.uuid
154
+ end
155
+
156
+ def tmp_file_dir
157
+ "#{WflFileBase.config.root}#{WflFileBase.config.tmp_dir}"
158
+ end
159
+
160
+ end
161
+
162
+ end
@@ -0,0 +1,44 @@
1
+
2
+
3
+ module WflMimeType
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ base.class_eval do
7
+ def mime_type
8
+ `file --brief --mime-type #{self.path}`.strip
9
+ end
10
+
11
+ def charset
12
+ `file --brief --mime #{self.path}`.split(';').second.split('=').second.strip
13
+ end
14
+ end
15
+ end
16
+
17
+ module ClassMethods
18
+ def mime_type file
19
+ if file.is_a?(String) && File.exist?(file)
20
+ file = File.open(file)
21
+ `file --brief --mime-type #{file.path}`.strip
22
+ elsif (file.is_a?(File) && !File.directory?(file))
23
+ `file --brief --mime-type #{file.path}`.strip
24
+ else
25
+ raise 'the path is useless or the file is useless'
26
+ end
27
+ end
28
+
29
+ def charset
30
+ if file.is_a?(String) && File.exist?(file)
31
+ file = File.open(file)
32
+ `file --brief --mime #{file.path}`.split(';').second.split('=').second.strip
33
+ elsif (file.is_a?(File) && !File.directory?(file))
34
+ `file --brief --mime #{file.path}`.split(';').second.split('=').second.strip
35
+ else
36
+ raise 'the path is useless or the file is useless'
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ class File
43
+ include WflMimeType
44
+ end
@@ -0,0 +1,32 @@
1
+ class << ActiveRecord::Base
2
+ def wfl_mount_file_base column, uploader_class
3
+
4
+ self.class_eval do
5
+
6
+ define_method column.to_sym do
7
+ @uploader ||= uploader_class.new(column, self)
8
+ end
9
+
10
+ define_method "write_#{column}" do
11
+ if self.send("#{column}_changed?") && (self.read_attribute(column).is_a?(File) || (self.read_attribute(column).is_a?(Hash) && self.read_attribute(column)[:tempfile].is_a?(Tempfile)) || self.read_attribute(column).is_a?(Tempfile))
12
+ self.send(column).send(:write_column)
13
+ end
14
+ end
15
+
16
+ define_method "store_#{column}" do
17
+ if self.send(column).store
18
+ self.send(column).send(:build_file)
19
+ end
20
+ end
21
+
22
+ define_method "remove_#{column}" do
23
+ self.send(column).send(:remove_file)
24
+ end
25
+
26
+ end
27
+ self.after_save :"store_#{column}"
28
+ self.before_save :"write_#{column}"
29
+ self.after_commit :"remove_#{column}", :on => :destroy
30
+ end
31
+
32
+ end
@@ -0,0 +1,3 @@
1
+ module WflFileBase
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'wfl_file_base/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "wfl_file_base"
8
+ spec.version = WflFileBase::VERSION
9
+ spec.authors = ["wpzero"]
10
+ spec.email = ["wpcreep@gmail.com"]
11
+ spec.summary = %q{map file to a range of ORMs, store them on different backends}
12
+ spec.description = %q{map file to a range of ORMs, store them on different backends. it can bind a record to a folder}
13
+ spec.homepage = "https://github.com/wpzero/wfl_file_base"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activerecord"
22
+ spec.add_development_dependency "bundler", "~> 1.6"
23
+ spec.add_development_dependency "rake"
24
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wfl_file_base
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - wpzero
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: map file to a range of ORMs, store them on different backends. it can
56
+ bind a record to a folder
57
+ email:
58
+ - wpcreep@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - .gitignore
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/wfl_file_base.rb
69
+ - lib/wfl_file_base/base.rb
70
+ - lib/wfl_file_base/file.rb
71
+ - lib/wfl_file_base/record.rb
72
+ - lib/wfl_file_base/version.rb
73
+ - wfl_file_base.gemspec
74
+ homepage: https://github.com/wpzero/wfl_file_base
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.2.2
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: map file to a range of ORMs, store them on different backends
98
+ test_files: []