-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathfile_handling_for_datasets.rb
72 lines (58 loc) · 2.13 KB
/
file_handling_for_datasets.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
module FileHandlingForDatasets
extend ActiveSupport::Concern
def check_files
if @files.blank?
flash[:no_files] = "You must specify at least one dataset"
end
end
def process_files
logger.info "DatasetsController: In process_files"
@files.each do |f|
if [ActionDispatch::Http::UploadedFile, Rack::Test::UploadedFile].include?(f["file"].class)
Rails.logger.info "file is an Http::UploadedFile (non javascript?)"
key = URI.encode(f["file"].original_filename)
storage_object = FileStorageService.create_and_upload_public_object(key, f["file"].read)
f["storage_key"] = storage_object.key(key)
f["file"] = storage_object.public_url
else
Rails.logger.info "file is not an http uploaded file, it's an s3 storage key"
f["storage_key"] = f["file"] unless f["file"].nil?
end
end
end
def get_multipart
@files = params["files"] || Array.new
if params["data"]
data = ActiveSupport::HashWithIndifferentAccess.new JSON.parse(params.delete("data"))
params["dataset"] = data["dataset"]
data["files"].each_with_index do |f, i|
@files[i]["title"] = f["title"]
@files[i]["description"] = f["description"]
end
end
end
def redirect_to_api
if params[:format] == 'json'
api_routes = {
"index" => "/api/datasets",
"dashboard" => "/api/user/datasets",
"show" => "/api/datasets/#{params[:id]}",
"files" => "/api/datasets/#{params[:id]}/files"
}
route = api_routes[params[:action]]
redirect_to(route) if route.present?
end
end
def get_files_as_array_for_serialisation
@files.map { |file_param_object| file_param_object.to_unsafe_hash }
end
def get_dataset
@dataset = Dataset.find(params["id"])
end
def clear_files
@files.keep_if { |f| f["id"] || (f["file"] && f["title"]) }
end
def dataset_params
params.require(:dataset).permit(:name, :owner, :description, :publisher_name, :publisher_url, :license, :frequency, :schema, :schema_name, :schema_description, :schema_restricted, :dataset_file_schema_id, :publishing_method)
end
end