@@ -20,51 +20,97 @@ class << self
2020 attr_accessor :spatial_root , :geoserver_root , :processing_root
2121
2222 def extract_and_move ( zip_file )
23- extract_to_path = extract_zipfile ( zip_file )
23+ extract_to_path = perform_extraction ( zip_file )
24+ summary = prepare_publishing_files ( extract_to_path )
25+ geofile_name_hash = analyze_summary ( summary )
26+ { extract_to_path :, geofile_name_hash : }
27+ end
28+
29+ private
30+
31+ def analyze_summary ( summary )
32+ public_map_files = [ ]
33+ ucb_map_files = [ ]
34+ summary . each do |summ |
35+ filename = summ [ :map_filename ]
36+ summ [ :public_access ] ? public_map_files << filename : ucb_map_files << filename
37+ end
38+ { public_files : public_map_files . compact . reject ( &:empty? ) , ucb_files : ucb_map_files . compact . reject ( &:empty? ) }
39+ end
40+
41+ # Extacting ingestion zip file to processing directory
42+ def perform_extraction ( zip_file )
43+ extract_to_path = prepare_extract_to_path ( zip_file )
44+ extract_zipfile ( zip_file )
45+ extract_to_path
46+ end
2447
25- geofile_ingestion_dir_path = move_files ( extract_to_path )
26- { extract_to_path :, geofile_name_hash : get_geofile_name_hash ( geofile_ingestion_dir_path ) }
48+ def prepare_extract_to_path ( zip_file )
49+ dir_name = File . basename ( zip_file , '.*' )
50+ extract_to_path = File . join ( @processing_root , dir_name )
51+ clr_directory ( extract_to_path )
52+ extract_to_path
53+ end
54+
55+ # Moving files to Geoserver and spatial server
56+ def prepare_publishing_files ( extract_to_path )
57+ from_geofile_ingestion_path = File . join ( extract_to_path , Config . geofile_ingestion_dirname )
58+ subdirectory_list ( from_geofile_ingestion_path ) . map { |dir | move_a_record ( dir ) }
59+ rescue StandardError => e
60+ logger . error "An error occurred while extracting and moving files from #{ from_geofile_ingestion_path } : #{ e . message } "
2761 end
2862
2963 def extract_zipfile ( zip_file , to_dir = @processing_root )
30- extracted_to_path = clr_subdirectory ( zip_file )
3164 Zip ::File . open ( zip_file ) do |zip |
3265 zip . each do |entry |
33- entry_path = File . join ( to_dir , entry . name )
34- entry . extract ( entry_path ) { true }
66+ entry . extract ( destination_directory : to_dir ) { true }
3567 end
3668 end
37- extracted_to_path
69+ rescue StandardError => e
70+ logger . error "An unexpected error occurred during unzip #{ zip_file } : #{ e . message } "
71+ raise
3872 end
73+
74+ # some records may no have a map.zip files
75+ def move_a_record ( dir_path )
76+ attributes = record_attributes ( dir_path )
77+ arkid = File . basename ( dir_path ) . strip
78+ map_filename = nil
79+ souredata_moved = false
3980
40- def move_files ( from_dir_path )
41- geofile_ingestion_dir_path = File . join ( from_dir_path , Config . geofile_ingestion_dirname )
42- subdirectory_list ( geofile_ingestion_dir_path ) . each do | subdirectory_path |
43- move_a_record ( subdirectory_path )
81+ subfile_list ( dir_path ) . each do | file |
82+ filename = File . basename ( file )
83+ map_filename = move_map_file ( file , arkid , attributes ) if filename == 'map.zip'
84+ souredata_moved = move_source_file ( file , arkid , attributes [ :public_access ] ) if filename == 'data.zip'
4485 end
45- geofile_ingestion_dir_path
86+ logger . warning " '#{ arkid } has no map.zip file, please check" if map_filename . nil?
87+ logger . warning " '#{ arkid } has no data.zip file, please check" unless souredata_moved
88+ { public_access : attributes [ :public_access ] , map_filename : }
4689 end
4790
48- def move_a_record ( dir_path )
49- subfile_list ( dir_path ) . each do |file |
50- if File . basename ( file ) == 'map.zip'
51- dest_dir_path = file_path ( dir_path , @geoserver_root )
52- unzip_map_files ( dest_dir_path , file )
53- else
54- dest_dir_path = file_path ( dir_path , @spatial_root )
55- mv_spatial_file ( dest_dir_path , file )
56- end
57- end
91+ def move_map_file ( file , arkid , attributes )
92+ dest_dir_path = file_path ( @geoserver_root , arkid , attributes [ :public_access ] )
93+ unzip_map_files ( dest_dir_path , file )
94+ format = attributes [ :format ] . downcase
95+ ext = format == 'shapefile' ? '.shp' : '.tif'
96+ "#{ arkid } #{ ext } "
97+ rescue StandardError => e
98+ logger . error "Failed to move map file '#{ file } ' for arkid '#{ arkid } ': #{ e . message } "
99+ ''
58100 end
59101
60- # remove the subdirectory if it exists
61- def clr_subdirectory ( zip_file )
62- subdir_name = File . basename ( zip_file , '.*' )
63- subdir_path = File . join ( @processing_root , subdir_name )
64- FileUtils . rm_r ( subdir_path ) if File . directory? subdir_path
65- subdir_path
102+ def move_source_file ( file , arkid , public_access )
103+ dest_dir_path = file_path ( @spatial_root , arkid , public_access )
104+ mv_spatial_file ( dest_dir_path , file )
105+ true
106+ rescue StandardError => e
107+ logger . error "Failed to move soucedata '#{ file } ' for '#{ arkid } ': #{ e . message } "
108+ end
109+
110+ def clr_directory ( directory_name )
111+ FileUtils . rm_r ( directory_name ) if File . directory? directory_name
66112 rescue Errno ::EACCES
67- logger . error ( "Permission denied: #{ subdir_path } " )
113+ logger . error ( "Permission denied to clear #{ directory_name } " )
68114 raise
69115 end
70116
@@ -76,37 +122,13 @@ def subfile_list(directory_path)
76122 Pathname ( directory_path ) . children . select ( &:file? )
77123 end
78124
79- def get_geofile_name_hash ( directory_path )
80- public_names = [ ]
81- ucb_names = [ ]
82- subdirectory_list ( directory_path ) . each do |sub_dir |
83- hash = name_access_hash ( sub_dir )
84- hash [ :public_access ] ? public_names << hash [ :name ] : ucb_names << hash [ :name ]
85- end
86- { public : public_names , ucb : ucb_names }
87- end
88-
89- def access_type ( dir )
90- json_hash = geoblacklight_hash ( dir )
91- value = json_hash [ 'dct_accessRights_s' ] . downcase
92- value == 'public' ? 'public' : 'UCB'
93- end
94-
95- private
96-
97- def geoblacklight_hash ( dir )
125+ def record_attributes ( dir )
98126 json_filepath = File . join ( dir , 'geoblacklight.json' )
99127 json_data = File . read ( json_filepath )
100- JSON . parse ( json_data )
101- end
102-
103- def name_access_hash ( dir )
104- basename = File . basename ( dir ) . split ( '_' ) . last
105- json_hash = geoblacklight_hash ( dir )
128+ json_hash = JSON . parse ( json_data )
129+ public_access = json_hash [ 'dct_accessRights_s' ] . downcase == 'public'
106130 format = json_hash [ 'dct_format_s' ] . downcase
107- ext = format == 'shapefile' ? '.shp' : '.tif'
108- access_right = json_hash [ 'dct_accessRights_s' ] . downcase
109- { name : "#{ basename } #{ ext } " , public_access : access_right == 'public' }
131+ { public_access :, format : }
110132 end
111133
112134 def unzip_map_files ( dest_dir , map_zipfile )
@@ -120,12 +142,12 @@ def mv_spatial_file(dest_dir, file)
120142 FileUtils . cp ( file , to_file )
121143 end
122144
123- def file_path ( dir_path , root )
145+ def file_path ( root , arkid , public_access )
124146 # geofiles/spatial/{UCB,public}/berkeley-{arkID}
125- arkid = File . basename ( dir_path ) . strip
126- type = access_type ( dir_path )
147+ type = public_access ? 'public' : 'UCB'
127148 File . join ( root , type , "berkeley-#{ arkid } " )
128149 end
150+
129151 end
130152 end
131153end
0 commit comments