@@ -35,14 +35,18 @@ def initialize(url, cache_dir, http_client, file_path)
35
35
36
36
def get_file_buffer ( relative_path )
37
37
file_name = relative_path . split ( '/' ) . last
38
- File . join ( @cache_dir , file_name [ 0 ] . downcase , file_name )
38
+ target_file = File . join ( @cache_dir , file_name [ 0 ] . downcase , file_name )
39
39
path = Dir [ "#{ @cache_dir } /**/#{ file_name } " ] . first
40
40
unless File . exist? ( "#{ path } " )
41
41
buffer = download ( "#{ @file_path . chomp ( '/' ) } /#{ relative_path } " )
42
- File . open ( File . join ( @cache_dir , file_name [ 0 ] . downcase , file_name ) , 'wb' ) do |file |
43
- file . write ( buffer . read )
42
+ File . open ( target_file , 'wb' ) do |file |
43
+ bytes = buffer . read
44
+ file . write ( bytes )
45
+ @log . debug ( "Saved #{ bytes . size } bytes in filesystem cache for path: #{ relative_path } , target file: #{ target_file } " )
44
46
end
45
- path = File . join ( @cache_dir , file_name [ 0 ] . downcase , file_name )
47
+ path = target_file
48
+ else
49
+ @log . info ( "Filesystem cache HIT for path: #{ relative_path } " )
46
50
end
47
51
File . open ( path , 'rb' )
48
52
rescue => e
@@ -59,6 +63,27 @@ def upload(file_data)
59
63
protected
60
64
attr_reader :log
61
65
66
+ def get_non_mutable ( relative_url )
67
+ file_name = relative_url . split ( '/' ) . last
68
+ target_file = File . join ( @cache_dir , file_name [ 0 ] . downcase , file_name )
69
+ path = Dir [ "#{ @cache_dir } /**/#{ file_name } " ] . first
70
+ unless File . exist? ( "#{ path } " )
71
+ buffer = get ( relative_url )
72
+ File . open ( target_file , 'wb' ) do |file |
73
+ file . write ( buffer )
74
+ @log . debug ( "Saved #{ buffer } bytes in filesystem cache for url: #{ relative_url } , target file: #{ target_file } " )
75
+ end
76
+ path = target_file
77
+ else
78
+ @log . info ( "Filesystem cache HIT for url: #{ relative_url } " )
79
+ end
80
+ File . binread ( path )
81
+ rescue => e
82
+ @log . error ( "#{ self . class . name } failed getting non-mutable url '#{ relative_url } '" )
83
+ @log . error ( "Error: #{ e } " )
84
+ return nil
85
+ end
86
+
62
87
def get ( relative_url )
63
88
@http_client . get ( url ( relative_url ) )
64
89
end
0 commit comments