filing-cabinet 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
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
+ *.log
19
+ *.pid
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in filer.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Brian Zeligson
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,29 @@
1
+ # Filer
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'filer'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install filer
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/<my-github-username>/filer/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/filer ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'filer'
4
+
5
+ Filer::Command.start(ARGV)
data/filer.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'filer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "filing-cabinet"
8
+ spec.version = Filer::VERSION
9
+ spec.authors = ["Brian Zeligson"]
10
+ spec.email = ["brian.zeligson@gmail.com"]
11
+ spec.summary = %q{Indexes files and stores to s3}
12
+ spec.description = %q{Indexes files and stores to s3}
13
+ spec.homepage = ""
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 "bundler", "~> 1.5"
22
+ spec.add_dependency "rake"
23
+ spec.add_dependency "thor"
24
+ spec.add_dependency "configliere"
25
+ spec.add_dependency "notifier"
26
+ spec.add_dependency "aws-sdk"
27
+ spec.add_dependency "flex"
28
+ spec.add_dependency "flex-models"
29
+ spec.add_dependency "fallen"
30
+ end
@@ -0,0 +1,38 @@
1
+ require 'flex'
2
+ require 'flex-models'
3
+
4
+ module Filer
5
+ class Filed
6
+ include Flex::ActiveModel
7
+
8
+ INDEX = 'filer_filed'
9
+
10
+ def self.ensure_index
11
+ return if Flex.exist?(index: INDEX)
12
+ params = self.flex.default_mapping.clone[INDEX]
13
+ params[:settings] = { number_of_replicas: 1,
14
+ number_of_shards: 4 }
15
+ puts params
16
+ Flex.POST("/#{INDEX}", params)
17
+ end
18
+
19
+ flex.index = INDEX
20
+
21
+ attribute :key
22
+ attribute_timestamps
23
+ attribute_attachment
24
+
25
+ def flex_id
26
+ key.gsub("/", "_")
27
+ end
28
+
29
+ scope :searchable do |q|
30
+ attachment_scope
31
+ .highlight(:fields => { :attachment => {},
32
+ :'attachment.title' => {} },
33
+ :pre_tags => ["*"], :post_tags => ["*"])
34
+ .query_string(q)
35
+ end
36
+ end
37
+ end
38
+ Filer::Filed.ensure_index
data/lib/filer/s3.rb ADDED
@@ -0,0 +1,57 @@
1
+ require 'aws-sdk'
2
+ require 'tempfile'
3
+
4
+ module Filer
5
+ class S3
6
+
7
+ def initialize(key, secret, bucket_name)
8
+ @key, @secret, @bucket_name = key, secret, bucket_name
9
+ end
10
+
11
+ def s3_init_params
12
+ { access_key_id: @key, secret_access_key: @secret,
13
+ server_side_encryption: :aes256 }
14
+ end
15
+
16
+ def s3
17
+ @s3 ||= AWS::S3.new(s3_init_params)
18
+ end
19
+
20
+ def bucket
21
+ return @bucket if @bucket
22
+ bucket = s3.buckets[@bucket_name]
23
+ unless bucket.location_constraint == s3.config.region
24
+ @s3 = nil
25
+ @s3 = AWS::S3.new(s3_init_params.
26
+ merge(region: bucket.location_constraint))
27
+ bucket = s3.buckets[@bucket_name]
28
+ end
29
+ @bucket = bucket
30
+ end
31
+
32
+ def s3_key(path)
33
+ filename = File.basename(path)
34
+ y = Time.now.strftime("%Y")
35
+ m = Time.now.strftime("%m")
36
+ d = Time.now.strftime("%d")
37
+ "#{y}/#{m}/#{d}/#{filename}"
38
+ end
39
+
40
+ def put_file(path)
41
+ o = bucket.objects[s3_key(path)]
42
+ o.write(Pathname.new(path), server_side_encryption: :aes256)
43
+ end
44
+
45
+ def open_file(key)
46
+ o = bucket.objects[key]
47
+ filename = File.basename(key)
48
+ ext = File.extname(key)
49
+ t = Tempfile.new([filename, ext])
50
+ o.read do |ch| t.write(ch) end
51
+ t.close
52
+ # seems like a race condition
53
+ sleep 1
54
+ `open #{t.path}`
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,3 @@
1
+ module Filer
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,53 @@
1
+ require "fallen"
2
+ require "notifier"
3
+ require "filer/s3"
4
+ require "filer/filed"
5
+
6
+ module Watcher
7
+ extend Fallen
8
+ FILER_LOG = "filer_watcher.log"
9
+
10
+ def self.instance(settings)
11
+ @settings = settings
12
+ Watcher.pid_file 'filer_watcher.pid'
13
+ Watcher.stderr FILER_LOG
14
+ self.s3
15
+ self
16
+ end
17
+
18
+ def self.s3
19
+ params = [:s3_key, :s3_secret, :s3_bucket].map {|m| @settings[m]}
20
+ unless params.compact.size == 3
21
+ puts "Please run filer configure-s3 first"
22
+ exit
23
+ end
24
+ @s3 ||= Filer::S3.new(*params)
25
+ end
26
+
27
+ def self.notify(msg)
28
+ Notifier.notify(
29
+ title: "Filer",
30
+ message: msg)
31
+ end
32
+
33
+ def self.run
34
+ while running?
35
+ begin
36
+ files = @settings[:directories].flat_map {|d| Dir["#{d}/*"]}
37
+ files.each do |f|
38
+ @s3.put_file(f)
39
+ fd = Filer::Filed.new(
40
+ key: @s3.s3_key(f),
41
+ attachment: Base64.encode64(File.read(f)))
42
+ fd.save
43
+ File.delete(f)
44
+ end
45
+ self.notify("Processed #{files.size} files") unless files.empty?
46
+ sleep 20
47
+ rescue Exception => e
48
+ self.notify("Filer encountered an error. " <<
49
+ "See #{File.dirname(__FILE__)}/#{FILER_LOG}")
50
+ end
51
+ end
52
+ end
53
+ end
data/lib/filer.rb ADDED
@@ -0,0 +1,91 @@
1
+ require "filer/version"
2
+ require 'filer/watcher'
3
+ require "yaml"
4
+ require "thor"
5
+ require "configliere"
6
+
7
+ module Filer
8
+ CONFIG = '.filer.yml'
9
+
10
+ class Command < Thor
11
+
12
+ no_commands do
13
+ def watcher
14
+ Watcher.instance(Settings)
15
+ end
16
+
17
+ def save_settings
18
+ Settings.save!(CONFIG)
19
+ end
20
+
21
+ def s3
22
+ params = [:s3_key, :s3_secret, :s3_bucket].map {|m| Settings[m]}
23
+ unless params.compact.size == 3
24
+ puts "Please run filer configure-s3 first"
25
+ exit
26
+ end
27
+ @s3 ||= Filer::S3.new(*params)
28
+ end
29
+ end
30
+
31
+ desc "start-watching", "start watching configured directories"
32
+ def start_watching
33
+ watcher.daemonize!
34
+ watcher.start!
35
+ end
36
+
37
+ desc "stop-watching", "stop watching configured directories"
38
+ def stop_watching
39
+ watcher.stop!
40
+ end
41
+
42
+ desc "directories", "List directories currently handled by filer"
43
+ def directories
44
+ Settings[:directories].each_with_index do |dir, i|
45
+ puts "#{i+1}) #{dir}"
46
+ end
47
+ end
48
+
49
+ desc "add-directory", "Adds a directory to be handled by filer"
50
+ def add_directory(dir)
51
+ Settings[:directories].push(dir)
52
+ save_settings
53
+ end
54
+
55
+ desc "remove-directory", "Removes directory with given index. Index is taken from " <<
56
+ "output of filer directories"
57
+ def remove_directory(i)
58
+ Settings[:directories].delete_at(i.to_i-1)
59
+ save_settings
60
+ puts "Updated. New directories: "
61
+ directories
62
+ end
63
+
64
+ desc "configure-s3 KEY SECRET BUCKET",
65
+ "Define key, secret, and bucket to use for s3 " <<
66
+ "upload, eg: filer configure-s3 my_key my_secret my_bucket"
67
+ def configure_s3(key, secret, bucket)
68
+ Settings[:s3_key], Settings[:s3_secret], Settings[:s3_bucket] =
69
+ key, secret, bucket
70
+ save_settings
71
+ end
72
+
73
+ desc 'search "KEYWORDS"', "Search indexed files"
74
+ def search(keywords)
75
+ results = Filer::Filed.searchable(keywords).all
76
+ if results.empty?
77
+ puts "No results found"
78
+ return
79
+ end
80
+ results.each_with_index do |r, i|
81
+ puts "#{i+1}) #{r.key} - #{r.highlight["attachment"].join(" ... ")}"
82
+ end
83
+ ix = ask("Index of file to open (q to exit):")
84
+ return unless ix.to_i > 0 && results[ix.to_i - 1]
85
+ s3.open_file(results[ix.to_i - 1].key.first)
86
+ end
87
+
88
+ Settings.read(CONFIG)
89
+ Settings[:directories] ||= []
90
+ end
91
+ end
metadata ADDED
@@ -0,0 +1,204 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: filing-cabinet
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brian Zeligson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-01-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.5'
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: '1.5'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
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: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: thor
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: configliere
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: notifier
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: aws-sdk
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: flex
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: flex-models
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :runtime
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ - !ruby/object:Gem::Dependency
143
+ name: fallen
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ type: :runtime
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ description: Indexes files and stores to s3
159
+ email:
160
+ - brian.zeligson@gmail.com
161
+ executables:
162
+ - filer
163
+ extensions: []
164
+ extra_rdoc_files: []
165
+ files:
166
+ - .gitignore
167
+ - Gemfile
168
+ - LICENSE.txt
169
+ - README.md
170
+ - Rakefile
171
+ - bin/filer
172
+ - filer.gemspec
173
+ - lib/filer.rb
174
+ - lib/filer/filed.rb
175
+ - lib/filer/s3.rb
176
+ - lib/filer/version.rb
177
+ - lib/filer/watcher.rb
178
+ homepage: ''
179
+ licenses:
180
+ - MIT
181
+ post_install_message:
182
+ rdoc_options: []
183
+ require_paths:
184
+ - lib
185
+ required_ruby_version: !ruby/object:Gem::Requirement
186
+ none: false
187
+ requirements:
188
+ - - ! '>='
189
+ - !ruby/object:Gem::Version
190
+ version: '0'
191
+ required_rubygems_version: !ruby/object:Gem::Requirement
192
+ none: false
193
+ requirements:
194
+ - - ! '>='
195
+ - !ruby/object:Gem::Version
196
+ version: '0'
197
+ requirements: []
198
+ rubyforge_project:
199
+ rubygems_version: 1.8.24
200
+ signing_key:
201
+ specification_version: 3
202
+ summary: Indexes files and stores to s3
203
+ test_files: []
204
+ has_rdoc: