eac_fs 0.2.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/eac_fs/cache.rb +64 -0
- data/lib/eac_fs/cached_download.rb +39 -0
- data/lib/eac_fs/file_info.rb +5 -0
- data/lib/eac_fs/patches/module/fs_cache.rb +10 -0
- data/lib/eac_fs/patches/module.rb +4 -0
- data/lib/eac_fs/patches/object/fs_cache.rb +17 -0
- data/lib/eac_fs/patches/object.rb +4 -0
- data/lib/eac_fs/version.rb +1 -1
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: edf014df44c45cd3a4e083d632141bcdbeb28f28d98f7759edd031d66acfb5ab
|
4
|
+
data.tar.gz: d014be25611b6d58dc561c790c19f82ed19bad2c94002e19b922db52e1d18632
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 764614f228e5fa7eec4239459d839157352198eae43c10cb6580199e87879a595c469f29be6d677e514bf4e69cc42ce94ed8ac643e2acec546aa5d69750ac67e
|
7
|
+
data.tar.gz: 7ab8466b22bda77686d8f372eb722bd26631fac807e3439a76ba93bfa6f992d699e0a80021dbe84475414d31e6c60424b0c8694fdc1c3902d10224d73929322d
|
data/lib/eac_fs/cache.rb
ADDED
@@ -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
|
data/lib/eac_fs/file_info.rb
CHANGED
@@ -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,17 @@
|
|
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
|
+
oid = fs_cache_object_id
|
9
|
+
oid = [oid.to_s] unless oid.is_a?(::Enumerable)
|
10
|
+
oid.inject(self.class.fs_cache) { |a, e| a.child(e.to_s) }
|
11
|
+
end
|
12
|
+
|
13
|
+
# @return [String, Array<String>]
|
14
|
+
def fs_cache_object_id
|
15
|
+
raise 'Abstract method hit'
|
16
|
+
end
|
17
|
+
end
|
data/lib/eac_fs/version.rb
CHANGED
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.
|
4
|
+
version: 0.6.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-
|
11
|
+
date: 2021-10-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: content-type
|
@@ -73,8 +73,14 @@ 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
|
77
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
|
78
84
|
- lib/eac_fs/patches/pathname.rb
|
79
85
|
- lib/eac_fs/patches/pathname/info.rb
|
80
86
|
- lib/eac_fs/version.rb
|