eac_fs 0.1.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f40117e7ce057f0d1557f1247000ce9310932c0b2120f26e4419f041d47ce6e7
4
- data.tar.gz: 8a54c982cc267e99ee6bb4b5904314de2c36067fd220d9d8958e0a5f5d392393
3
+ metadata.gz: f84f3e6bee2753e30079a3e79cf4d8ad3568b59dfeac360f1de73d2ca34162d5
4
+ data.tar.gz: 184ab1296d60754f52dc2d598dec9b18871b1024dde617b3b2cac3d8b50c1490
5
5
  SHA512:
6
- metadata.gz: ecfc31d514fdfe3f70d6bc10417c2de8ec38758d432bfe2534ded497a560e76bfdbec2882fb149822950b4773c81092ba1bb4d379489b8f37e9a56a8b63144eb
7
- data.tar.gz: 8db0333bfb90f128d44881454b0d24dc507a93c6b3197cf2f153d2f669404e06376ec0a49b297b301feb4b56f6f0311ca0477aa074b9834a14750844ce030592
6
+ metadata.gz: 6c1b1a320116205e2c690564bdd3d6aa78451a0b26ef3feeb8a5cddf76b6a96e427e89bc5db02fd535b10453b61d01edd6211e075a2e9819b9f41bef809d1203
7
+ data.tar.gz: f4bc08f798e701a06a6dadf703eeb6d0c4c5a21c25cb95c5946b6aaf40ae13f9a015a8cb8ac6ffaac96abffefe71dc25e2bf9f6a8970bb2a149ce552306ab4f6
@@ -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,10 @@
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
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,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.0'
4
+ VERSION = '0.4.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.0
4
+ version: 0.4.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-06-15 00:00:00.000000000 Z
11
+ date: 2021-09-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: content-type
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0.67'
33
+ version: '0.70'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0.67'
40
+ version: '0.70'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: ruby-filemagic
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0.2'
61
+ version: '0.3'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0.2'
68
+ version: '0.3'
69
69
  description:
70
70
  email:
71
71
  executables: []
@@ -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: []
@@ -93,7 +102,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
102
  - !ruby/object:Gem::Version
94
103
  version: '0'
95
104
  requirements: []
96
- rubygems_version: 3.0.9
105
+ rubygems_version: 3.1.6
97
106
  signing_key:
98
107
  specification_version: 4
99
108
  summary: Put here de description.