filestorage 0.1.0 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: ee9a243f8dfa4db40b47f67ded7aff2e772f09bf
4
- data.tar.gz: 6ee870b92979fe9db1e77dd9aab240fd89370c4f
2
+ SHA256:
3
+ metadata.gz: 93b5b4b0df62361b6356ee67434394c9c25cc2c6e52452cea707c4e16b42c8e7
4
+ data.tar.gz: 780077070a925c6a6e41b1ccd3d6a95bd5e3c326132529df3c3263125cb94067
5
5
  SHA512:
6
- metadata.gz: 68570d4ed0937bf06393216431629eb43013338b0157af05594a9e376d32bba8ea323871094f6e9716c7b0ebbda6c0a29d1afc3625b80e296bc29755a60496db
7
- data.tar.gz: 6a56e1b551b3cff129d7c4057ad9630cb2eae4d64ce8891b2bf0eefcd81b6a84095e5e3e4d54717b3c0be73a93bc008a0b98ef71813fbb074098b46f454f24de
6
+ metadata.gz: 9a5695af704f9d68e4ae01e94fd443441c0184c39aca7c87b4c085fac77146788eccd9818de0ed985b73f3d004d489ddff5b0a8e2e3c38968a5435d0b6208f31
7
+ data.tar.gz: eeee2f2ed9cc5165444be0d272b766584fede24238268becffa05b1f1b519f4657404639b81d0e7b86aca933dab18caa04f28d9468b3b2ba0705f2ce045803d0
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in filestorage.gemspec
4
- gemspec
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in filestorage.gemspec
4
+ gemspec
data/LICENSE.txt CHANGED
@@ -1,22 +1,22 @@
1
- Copyright (c) 2014 takatoh
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.
1
+ Copyright (c) 2014-2023 takatoh
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 CHANGED
@@ -1,41 +1,71 @@
1
- # Filestorage
2
-
3
- A simple file storage.
4
-
5
- ## Installation
6
-
7
- Add this line to your application's Gemfile:
8
-
9
- gem 'filestorage'
10
-
11
- And then execute:
12
-
13
- $ bundle
14
-
15
- Or install it yourself as:
16
-
17
- $ gem install filestorage
18
-
19
- ## Usage
20
-
21
- Create a instance.
22
-
23
- require 'filestorage'
24
- storage = Filesotrage::Local.new(base_dir)
25
-
26
- Store a file to path.
27
-
28
- storage.store("foo/bar/baz.txt", file)
29
-
30
- And get the file. `get' method returns instance of File class.
31
-
32
- file = storage.get("foo/bar/baz.txt")
33
- content = file.read
34
-
35
- ## Contributing
36
-
37
- 1. Fork it ( https://github.com/takatoh/filestorage/fork )
38
- 2. Create your feature branch (`git checkout -b my-new-feature`)
39
- 3. Commit your changes (`git commit -am 'Add some feature'`)
40
- 4. Push to the branch (`git push origin my-new-feature`)
41
- 5. Create a new Pull Request
1
+ # Filestorage
2
+
3
+ A simple file storage.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem "filestorage"
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install filestorage
18
+
19
+ ## Store to local file system
20
+
21
+ Create a instance.
22
+
23
+ ```ruby
24
+ require "filestorage"
25
+ storage = Filesotrage::Local.new(base_dir)
26
+ ```
27
+
28
+ Store a file to path.
29
+
30
+ ```ruby
31
+ storage.store(file, "foo/bar/baz.txt")
32
+ ```
33
+
34
+ And get the file. `get` method returns instance of File class.
35
+
36
+ ```ruby
37
+ file = storage.get("foo/bar/baz.txt")
38
+ content = file.read
39
+ ```
40
+
41
+ ## Store to local with random file name
42
+
43
+ Use `Fielstorage::LocalRandom` class.
44
+
45
+ Create a instance.
46
+
47
+ ```ruby
48
+ require "filestorage"
49
+ storage = Filesotrage::LocalRandom.new(base_dir, length)
50
+ ```
51
+
52
+ `length` is for filename, default to `8`.
53
+
54
+ Store a file.
55
+
56
+ ```ruby
57
+ storage.store(file)
58
+ ```
59
+
60
+ Returns path to stored file, e.g. "5Q/CY/5QCyP0gT.txt"
61
+
62
+ And get the file. `get` method returns instance of File class.
63
+
64
+ ```ruby
65
+ file = storage.get("5Q/CY/5QCyP0gT.txt")
66
+ content = file.read
67
+ ```
68
+
69
+ ## License
70
+
71
+ MIT license
data/Rakefile CHANGED
@@ -1,2 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
2
7
 
8
+ task default: :spec
data/filestorage.gemspec CHANGED
@@ -1,23 +1,37 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'filestorage/version'
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "filestorage"
8
- spec.version = Filestorage::VERSION
9
- spec.authors = ["takatoh"]
10
- spec.email = ["takatoh.m@gmail.com"]
11
- spec.summary = %q{A simple file storeage.}
12
- spec.description = %q{}
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_development_dependency "bundler", "~> 1.10"
22
- spec.add_development_dependency "rake"
23
- end
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/filestorage/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "filestorage"
7
+ spec.version = Filestorage::VERSION
8
+ spec.authors = ["takatoh"]
9
+ spec.email = ["takatoh.m@gmail.com"]
10
+
11
+ spec.summary = %q{A simple file storeage.}
12
+ spec.description = %q{}
13
+ spec.homepage = "https://github.com/takatoh/filestorage"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ # spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
18
+
19
+ # spec.metadata["homepage_uri"] = spec.homepage
20
+ # spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
21
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(__dir__) do
26
+ `git ls-files -z`.split("\x0").reject do |f|
27
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
28
+ end
29
+ end
30
+ # spec.bindir = "exe"
31
+ # spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ spec.add_development_dependency "bundler", ">= 2.2.33"
35
+ spec.add_development_dependency "rake", ">= 13.0"
36
+ spec.add_development_dependency "rspec", ">= 3.0"
37
+ end
@@ -1,81 +1,81 @@
1
- # encoding: utf-8
2
-
3
- require 'pathname'
4
- require 'fileutils'
5
- require 'find'
6
-
7
-
8
- module Filestorage
9
-
10
- class Local
11
-
12
- def initialize(base_dir)
13
- @base_dir = Pathname.new(base_dir)
14
- end
15
-
16
- def store(path, file)
17
- fullpath = @base_dir + path
18
- raise AlreadyExist.new("Already exist #{path}") if File.exist?(fullpath)
19
- FileUtils.mkdir_p(fullpath.parent)
20
- if file.instance_of?(Pathname)
21
- FileUtils.cp(file, fullpath)
22
- elsif file.instance_of?(File)
23
- File.open(fullpath, "wb") do |f|
24
- f.write(file.read)
25
- end
26
- else
27
- File.open(fullpath, "wb") do |f|
28
- f.write(file)
29
- end
30
- end
31
- path
32
- end
33
-
34
- def get(path)
35
- fullpath = @base_dir + path
36
- raise NotExist.new("Not exist #{path}") unless File.exist?(fullpath)
37
- File.open(fullpath, "rb")
38
- end
39
-
40
- def delete(path, delete_dir: false)
41
- fullpath = @base_dir + path
42
- raise NotExist.new("Not exist #{path}") unless File.exist?(fullpath)
43
- FileUtils.rm(fullpath)
44
- sweep(fullpath.parent) if delete_dir
45
- path
46
- end
47
-
48
- def exist?(path)
49
- fullpath = @base_dir + path
50
- File.exist?(fullpath)
51
- end
52
-
53
- def files
54
- files = []
55
- Find.find(@base_dir.to_s) do |f|
56
- f = Pathname.new(f)
57
- files << f.relative_path_from(@base_dir).to_s if f.file?
58
- end
59
- files
60
- end
61
-
62
- private
63
-
64
- def sweep(path)
65
- paths(path).reverse.each do |p|
66
- FileUtils.rmdir(p) if p.children.empty?
67
- end
68
- end
69
-
70
- def paths(path)
71
- p = []
72
- until path.to_s == @base_dir.to_s
73
- p << path
74
- path = path.parent
75
- end
76
- p.reverse
77
- end
78
-
79
- end # of class Local
80
-
81
- end # of module Filestorage
1
+ # encoding: utf-8
2
+
3
+ require "pathname"
4
+ require "fileutils"
5
+ require "find"
6
+
7
+
8
+ module Filestorage
9
+
10
+ class Local
11
+
12
+ def initialize(base_dir)
13
+ @base_dir = Pathname.new(base_dir)
14
+ end
15
+
16
+ def store(file, path)
17
+ fullpath = @base_dir + path
18
+ raise AlreadyExist.new("Already exist #{path}") if File.exist?(fullpath)
19
+ FileUtils.mkdir_p(fullpath.parent)
20
+ if file.instance_of?(Pathname)
21
+ FileUtils.cp(file, fullpath)
22
+ elsif file.instance_of?(File)
23
+ File.open(fullpath, "wb") do |f|
24
+ f.write(file.read)
25
+ end
26
+ else
27
+ File.open(fullpath, "wb") do |f|
28
+ f.write(file)
29
+ end
30
+ end
31
+ path
32
+ end
33
+
34
+ def get(path)
35
+ fullpath = @base_dir + path
36
+ raise NotExist.new("Not exist #{path}") unless File.exist?(fullpath)
37
+ File.open(fullpath, "rb")
38
+ end
39
+
40
+ def delete(path, delete_dir: false)
41
+ fullpath = @base_dir + path
42
+ raise NotExist.new("Not exist #{path}") unless File.exist?(fullpath)
43
+ FileUtils.rm(fullpath)
44
+ sweep(fullpath.parent) if delete_dir
45
+ path
46
+ end
47
+
48
+ def exist?(path)
49
+ fullpath = @base_dir + path
50
+ File.exist?(fullpath)
51
+ end
52
+
53
+ def files
54
+ files = []
55
+ Find.find(@base_dir.to_s) do |f|
56
+ f = Pathname.new(f)
57
+ files << f.relative_path_from(@base_dir).to_s if f.file?
58
+ end
59
+ files
60
+ end
61
+
62
+ private
63
+
64
+ def sweep(path)
65
+ paths(path).reverse.each do |p|
66
+ FileUtils.rmdir(p) if p.children.empty?
67
+ end
68
+ end
69
+
70
+ def paths(path)
71
+ p = []
72
+ until path.to_s == @base_dir.to_s
73
+ p << path
74
+ path = path.parent
75
+ end
76
+ p.reverse
77
+ end
78
+
79
+ end # of class Local
80
+
81
+ end # of module Filestorage
@@ -0,0 +1,51 @@
1
+ # encoding: utf-8
2
+
3
+ require "pathname"
4
+ require "fileutils"
5
+ require "find"
6
+ require "securerandom"
7
+
8
+
9
+ module Filestorage
10
+
11
+ class LocalRandom < Local
12
+
13
+ DEFAULT_LETTERS = "abcdefghijklmnopqrstuvwxyz0123456789"
14
+ DEFAULT_LENGTH = 8
15
+
16
+ def initialize(base_dir, length = DEFAULT_LENGTH)
17
+ @base_dir = Pathname.new(base_dir)
18
+ @filename_length = length
19
+ @letter_pool = DEFAULT_LETTERS
20
+ @pool_size = @letter_pool.size
21
+ end
22
+
23
+ def store(file)
24
+ begin
25
+ filename = gen_random + File.extname(file)
26
+ path = [filename[0, 2].upcase, filename[2, 2].upcase, filename].join("/")
27
+ super(file, path)
28
+ rescue AlreadyExist
29
+ retry
30
+ end
31
+ path
32
+ end
33
+
34
+ private
35
+
36
+ def gen_random
37
+ s = []
38
+ @filename_length.times do
39
+ c = up(@letter_pool[SecureRandom.random_number(@pool_size)])
40
+ s << c
41
+ end
42
+ s.join
43
+ end
44
+
45
+ def up(c)
46
+ [true, false].sample ? c.upcase : c
47
+ end
48
+
49
+ end # of class LocalRandom
50
+
51
+ end # of module Filestorage
@@ -1,3 +1,3 @@
1
- module Filestorage
2
- VERSION = "0.1.0"
3
- end
1
+ module Filestorage
2
+ VERSION = "0.2.0"
3
+ end
data/lib/filestorage.rb CHANGED
@@ -1,7 +1,8 @@
1
- require "filestorage/version"
2
- require "filestorage/local"
3
-
4
- module Filestorage
5
- class NotExist < StandardError; end
6
- class AlreadyExist < StandardError; end
7
- end
1
+ require "filestorage/version"
2
+ require "filestorage/local"
3
+ require "filestorage/local_random"
4
+
5
+ module Filestorage
6
+ class NotExist < StandardError; end
7
+ class AlreadyExist < StandardError; end
8
+ end
metadata CHANGED
@@ -1,43 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: filestorage
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - takatoh
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-17 00:00:00.000000000 Z
11
+ date: 2023-02-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.10'
19
+ version: 2.2.33
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.10'
26
+ version: 2.2.33
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '13.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '13.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
34
48
  type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
52
  - - ">="
39
53
  - !ruby/object:Gem::Version
40
- version: '0'
54
+ version: '3.0'
41
55
  description: ''
42
56
  email:
43
57
  - takatoh.m@gmail.com
@@ -45,7 +59,6 @@ executables: []
45
59
  extensions: []
46
60
  extra_rdoc_files: []
47
61
  files:
48
- - ".gitignore"
49
62
  - Gemfile
50
63
  - LICENSE.txt
51
64
  - README.md
@@ -53,12 +66,13 @@ files:
53
66
  - filestorage.gemspec
54
67
  - lib/filestorage.rb
55
68
  - lib/filestorage/local.rb
69
+ - lib/filestorage/local_random.rb
56
70
  - lib/filestorage/version.rb
57
- homepage: ''
71
+ homepage: https://github.com/takatoh/filestorage
58
72
  licenses:
59
73
  - MIT
60
74
  metadata: {}
61
- post_install_message:
75
+ post_install_message:
62
76
  rdoc_options: []
63
77
  require_paths:
64
78
  - lib
@@ -66,16 +80,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
66
80
  requirements:
67
81
  - - ">="
68
82
  - !ruby/object:Gem::Version
69
- version: '0'
83
+ version: 2.6.0
70
84
  required_rubygems_version: !ruby/object:Gem::Requirement
71
85
  requirements:
72
86
  - - ">="
73
87
  - !ruby/object:Gem::Version
74
88
  version: '0'
75
89
  requirements: []
76
- rubyforge_project:
77
- rubygems_version: 2.4.8
78
- signing_key:
90
+ rubygems_version: 3.2.33
91
+ signing_key:
79
92
  specification_version: 4
80
93
  summary: A simple file storeage.
81
94
  test_files: []
data/.gitignore DELETED
@@ -1,22 +0,0 @@
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