@@ -43,8 +43,7 @@ def create
43
43
raw_files = Array ( files_params ) . reject ( &:blank? )
44
44
45
45
if raw_files . empty?
46
- redirect_to new_import_path , alert : 'No files were selected for upload' , status : :unprocessable_entity
47
- return
46
+ redirect_to new_import_path , alert : 'No files were selected for upload' , status : :unprocessable_entity and return
48
47
end
49
48
50
49
created_imports = [ ]
@@ -59,11 +58,11 @@ def create
59
58
if created_imports . any?
60
59
redirect_to imports_url ,
61
60
notice : "#{ created_imports . size } files are queued to be imported in background" ,
62
- status : :see_other
61
+ status : :see_other and return
63
62
else
64
63
redirect_to new_import_path ,
65
64
alert : 'No valid file references were found. Please upload files using the file selector.' ,
66
- status : :unprocessable_entity
65
+ status : :unprocessable_entity and return
67
66
end
68
67
rescue StandardError => e
69
68
if created_imports . present?
@@ -95,26 +94,37 @@ def authorize_import
95
94
end
96
95
97
96
def import_params
98
- params . require ( :import ) . permit ( :name , :source , files : [ ] )
97
+ params . require ( :import ) . permit ( :name , files : [ ] )
99
98
end
100
99
101
100
def create_import_from_signed_id ( signed_id )
102
101
Rails . logger . debug "Creating import from signed ID: #{ signed_id [ 0 ..20 ] } ..."
103
102
104
103
blob = ActiveStorage ::Blob . find_signed ( signed_id )
105
104
106
- import = current_user . imports . build (
107
- name : blob . filename . to_s ,
108
- source : params [ :import ] [ :source ]
109
- )
110
-
105
+ import = current_user . imports . build ( name : blob . filename . to_s )
111
106
import . file . attach ( blob )
107
+ import . source = detect_import_source ( import . file ) if import . source . blank?
112
108
113
109
import . save!
114
110
115
111
import
116
112
end
117
113
114
+ def detect_import_source ( file_attachment )
115
+ temp_file_path = Imports ::SecureFileDownloader . new ( file_attachment ) . download_to_temp_file
116
+
117
+ Imports ::SourceDetector . new_from_file_header ( temp_file_path ) . detect_source
118
+ rescue StandardError => e
119
+ Rails . logger . warn "Failed to auto-detect import source for #{ file_attachment . filename } : #{ e . message } "
120
+ nil
121
+ ensure
122
+ # Cleanup temp file
123
+ if temp_file_path && File . exist? ( temp_file_path )
124
+ File . unlink ( temp_file_path )
125
+ end
126
+ end
127
+
118
128
def validate_points_limit
119
129
limit_exceeded = PointsLimitExceeded . new ( current_user ) . call
120
130
0 commit comments