eac_fs 0.1.1 → 0.5.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
2
  SHA256:
3
- metadata.gz: d26fc715752fe44df3923d947e85ec433450992587d840821507197a398d430c
4
- data.tar.gz: ca7db761b496c82bf493ff17834cd06d486e784c2d18370b54b417894f0209a5
3
+ metadata.gz: '08ea81a4a492c2774c2a0468785eabbe2b34ef1f483fb7fb538059c10acbc6c9'
4
+ data.tar.gz: cc5e36a5c94620e55185707dc2cd95814f12bfd25d8d5e6578403a36f7ffa145
5
5
  SHA512:
6
- metadata.gz: f526650a08ea0e3909692606cf957781e77a0dba6039e5bee18b084b025fda05271c2e997e66568952ccc511351607ce60947d29cfa91c4747850123d95dd307
7
- data.tar.gz: 619a1e73eb40e97d484c84f61a459f5f64af3b93468397e9a49a25da4d8c57b710857c6e7c4f0d9e7c4536684ebfd10c7d6f4535456661595b96aec588574b6c
6
+ metadata.gz: 45350032ec7d70a5c143a50a704c2422a3c0d92e1b122dc4b013804b46397748e025cd644de290020feca8b0efd7dee265beba298bd916b58565f3c06134f298
7
+ data.tar.gz: 6d3f782a17c3dbe927b3d534a7805f5f7fe11590d24a0a6cf215d8629a6d8b0c1b7ba87f6289646cdd07650d4216222d40c63a9403a1ee05e985302d3675f057
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+ require 'fileutils'
5
+
6
+ module EacFs
7
+ class Cache
8
+ enable_context
9
+
10
+ CONTENT_FILE_NAME = '__content__'
11
+
12
+ attr_reader :path
13
+
14
+ def initialize(*path_parts)
15
+ raise ArgumentError, "\"#{path_parts}\" is empty" if path_parts.empty?
16
+
17
+ @path = ::File.expand_path(::File.join(*path_parts.map(&:to_s)))
18
+ end
19
+
20
+ def clear
21
+ return unless cached?
22
+
23
+ ::File.unlink(content_path)
24
+ end
25
+
26
+ def read
27
+ return nil unless cached?
28
+
29
+ ::File.read(content_path)
30
+ end
31
+
32
+ def read_or_cache
33
+ write(yield) unless cached?
34
+
35
+ read
36
+ end
37
+
38
+ def write(value)
39
+ assert_directory_on_path
40
+ ::File.write(content_path, value)
41
+ value
42
+ end
43
+
44
+ def child(*child_path_parts)
45
+ self.class.new(path, *child_path_parts)
46
+ end
47
+
48
+ def cached?
49
+ ::File.exist?(content_path)
50
+ end
51
+
52
+ def content_path
53
+ ::File.join(path, CONTENT_FILE_NAME)
54
+ end
55
+
56
+ private
57
+
58
+ def assert_directory_on_path
59
+ raise "#{path} is a file" if ::File.file?(path)
60
+
61
+ ::FileUtils.mkdir_p(path)
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_fs/patches'
4
+ require 'eac_ruby_utils/fs/temp'
5
+
6
+ module Avm
7
+ class CachedDownload
8
+ attr_reader :url, :fs_cache
9
+
10
+ def initialize(url, parent_fs_cache = nil)
11
+ @url = url
12
+ @fs_cache = (parent_fs_cache || fs_cache).child(url.parameterize)
13
+ end
14
+
15
+ def assert
16
+ download unless fs_cache.cached?
17
+ end
18
+
19
+ def download
20
+ ::EacRubyUtils::Fs::Temp.on_file do |temp|
21
+ download_to(temp)
22
+ fs_cache.content_path.to_pathname.dirname.mkpath
23
+ ::FileUtils.mv(temp.to_path, fs_cache.content_path)
24
+ end
25
+ end
26
+
27
+ def path
28
+ fs_cache.content_path.to_pathname
29
+ end
30
+
31
+ private
32
+
33
+ def download_to(local_file)
34
+ ::URI.parse(url).open do |remote|
35
+ local_file.open('wb') { |handle| handle.write(remote.read) }
36
+ end
37
+ end
38
+ end
39
+ end
@@ -6,6 +6,9 @@ require 'filemagic'
6
6
 
7
7
  module EacFs
8
8
  class FileInfo
9
+ UNKNOWN_CONTENT_TYPE_STRING = 'application/octet-stream'
10
+ UNKNOWN_CONTENT_TYPE = ::ContentType.parse(UNKNOWN_CONTENT_TYPE_STRING)
11
+
9
12
  enable_simple_cache
10
13
  attr_reader :magic_string
11
14
 
@@ -19,6 +22,8 @@ module EacFs
19
22
 
20
23
  def content_type_uncached
21
24
  ::ContentType.parse(magic_string)
25
+ rescue ::Parslet::ParseFailed
26
+ UNKNOWN_CONTENT_TYPE
22
27
  end
23
28
  end
24
29
  end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_fs/cache'
4
+
5
+ class Module
6
+ # @return [EacFs::Cache]
7
+ def fs_cache
8
+ ::EacFs::Cache.context.current.child('fs_cache', *name.split('::'))
9
+ end
10
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/require_sub'
4
+ ::EacRubyUtils.require_sub(__FILE__)
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_fs/patches/module/fs_cache'
4
+
5
+ class Object
6
+ # @return [EacFs::Cache]
7
+ def fs_cache
8
+ self.class.fs_cache.child(fs_cache_object_id)
9
+ end
10
+
11
+ # @return [String]
12
+ def fs_cache_object_id
13
+ raise 'Abstract method hit'
14
+ end
15
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/require_sub'
4
+ ::EacRubyUtils.require_sub(__FILE__)
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_fs/file_info'
4
+ require 'pathname'
5
+
6
+ class Pathname
7
+ # Shortcut for `EacFs::FileInfo.new(self)`.
8
+ # @return [EacFs::FileInfo]
9
+ def info
10
+ ::EacFs::FileInfo.new(self)
11
+ end
12
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/require_sub'
4
+ ::EacRubyUtils.require_sub(__FILE__)
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/require_sub'
4
+ ::EacRubyUtils.require_sub(__FILE__)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacFs
4
- VERSION = '0.1.1'
4
+ VERSION = '0.5.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eac_fs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Put here the authors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-07-25 00:00:00.000000000 Z
11
+ date: 2021-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: content-type
@@ -73,7 +73,16 @@ extensions: []
73
73
  extra_rdoc_files: []
74
74
  files:
75
75
  - lib/eac_fs.rb
76
+ - lib/eac_fs/cache.rb
77
+ - lib/eac_fs/cached_download.rb
76
78
  - lib/eac_fs/file_info.rb
79
+ - lib/eac_fs/patches.rb
80
+ - lib/eac_fs/patches/module.rb
81
+ - lib/eac_fs/patches/module/fs_cache.rb
82
+ - lib/eac_fs/patches/object.rb
83
+ - lib/eac_fs/patches/object/fs_cache.rb
84
+ - lib/eac_fs/patches/pathname.rb
85
+ - lib/eac_fs/patches/pathname/info.rb
77
86
  - lib/eac_fs/version.rb
78
87
  homepage:
79
88
  licenses: []