mirror of
https://github.com/Freika/dawarich.git
synced 2026-01-11 09:41:40 -05:00
Use direct upload for user import.
This commit is contained in:
parent
fb4eedfe92
commit
0f7e66e136
2 changed files with 63 additions and 19 deletions
|
|
@ -54,30 +54,36 @@ class Settings::UsersController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def import
|
def import
|
||||||
unless params[:archive].present?
|
files_params = params.dig(:user_import, :files)
|
||||||
|
raw_files = Array(files_params).reject(&:blank?)
|
||||||
|
|
||||||
|
if raw_files.empty?
|
||||||
redirect_to edit_user_registration_path, alert: 'Please select a ZIP archive to import.'
|
redirect_to edit_user_registration_path, alert: 'Please select a ZIP archive to import.'
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
archive_file = params[:archive]
|
created_imports = []
|
||||||
|
|
||||||
validate_archive_file(archive_file)
|
raw_files.each do |item|
|
||||||
|
next if item.is_a?(ActionDispatch::Http::UploadedFile)
|
||||||
|
|
||||||
import = current_user.imports.build(
|
import = create_import_from_signed_id(item)
|
||||||
name: archive_file.original_filename,
|
created_imports << import if import.present?
|
||||||
source: :user_data_archive
|
end
|
||||||
)
|
|
||||||
|
|
||||||
import.file.attach(archive_file)
|
if created_imports.any?
|
||||||
|
|
||||||
if import.save
|
|
||||||
redirect_to edit_user_registration_path,
|
redirect_to edit_user_registration_path,
|
||||||
notice: 'Your data import has been started. You will receive a notification when it completes.'
|
notice: 'Your data import has been started. You will receive a notification when it completes.'
|
||||||
else
|
else
|
||||||
redirect_to edit_user_registration_path,
|
redirect_to edit_user_registration_path,
|
||||||
alert: 'Failed to start import. Please try again.'
|
alert: 'No valid file references were found. Please upload files using the file selector.'
|
||||||
end
|
end
|
||||||
rescue StandardError => e
|
rescue StandardError => e
|
||||||
|
if created_imports.present?
|
||||||
|
import_ids = created_imports.map(&:id).compact
|
||||||
|
Import.where(id: import_ids).destroy_all if import_ids.any?
|
||||||
|
end
|
||||||
|
|
||||||
ExceptionReporter.call(e, 'User data import failed to start')
|
ExceptionReporter.call(e, 'User data import failed to start')
|
||||||
redirect_to edit_user_registration_path,
|
redirect_to edit_user_registration_path,
|
||||||
alert: 'An error occurred while starting the import. Please try again.'
|
alert: 'An error occurred while starting the import. Please try again.'
|
||||||
|
|
@ -89,10 +95,29 @@ class Settings::UsersController < ApplicationController
|
||||||
params.require(:user).permit(:email, :password)
|
params.require(:user).permit(:email, :password)
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_archive_file(archive_file)
|
def create_import_from_signed_id(signed_id)
|
||||||
unless archive_file.content_type == 'application/zip' ||
|
Rails.logger.debug "Creating user data import from signed ID: #{signed_id[0..20]}..."
|
||||||
archive_file.content_type == 'application/x-zip-compressed' ||
|
|
||||||
File.extname(archive_file.original_filename).downcase == '.zip'
|
blob = ActiveStorage::Blob.find_signed(signed_id)
|
||||||
|
|
||||||
|
validate_archive_blob(blob)
|
||||||
|
|
||||||
|
import = current_user.imports.build(
|
||||||
|
name: blob.filename.to_s,
|
||||||
|
source: :user_data_archive
|
||||||
|
)
|
||||||
|
|
||||||
|
import.file.attach(blob)
|
||||||
|
|
||||||
|
import.save!
|
||||||
|
|
||||||
|
import
|
||||||
|
end
|
||||||
|
|
||||||
|
def validate_archive_blob(blob)
|
||||||
|
unless blob.content_type == 'application/zip' ||
|
||||||
|
blob.content_type == 'application/x-zip-compressed' ||
|
||||||
|
File.extname(blob.filename.to_s).downcase == '.zip'
|
||||||
|
|
||||||
redirect_to edit_user_registration_path, alert: 'Please upload a valid ZIP file.' and return
|
redirect_to edit_user_registration_path, alert: 'Please upload a valid ZIP file.' and return
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -76,16 +76,35 @@
|
||||||
<h3 class="font-bold text-lg mb-4">Import your data</h3>
|
<h3 class="font-bold text-lg mb-4">Import your data</h3>
|
||||||
<p class="mb-4 text-sm text-gray-600">Upload a ZIP file containing your exported Dawarich data to restore your points, trips, and settings.</p>
|
<p class="mb-4 text-sm text-gray-600">Upload a ZIP file containing your exported Dawarich data to restore your points, trips, and settings.</p>
|
||||||
|
|
||||||
<%= form_with url: import_settings_users_path, method: :post, multipart: true, class: 'space-y-4', data: { turbo: false } do |f| %>
|
<%= form_with url: import_settings_users_path, method: :post, class: 'space-y-4', data: {
|
||||||
|
controller: "direct-upload",
|
||||||
|
direct_upload_url_value: rails_direct_uploads_url,
|
||||||
|
direct_upload_target: "form",
|
||||||
|
turbo: false
|
||||||
|
} do |f| %>
|
||||||
<div class="form-control">
|
<div class="form-control">
|
||||||
<%= f.label :archive, class: 'label' do %>
|
<%= f.label "user_import[files]", class: 'label' do %>
|
||||||
<span class="label-text">Select ZIP archive</span>
|
<span class="label-text">Select ZIP archive</span>
|
||||||
<% end %>
|
<% end %>
|
||||||
<%= f.file_field :archive, accept: '.zip', required: true, class: 'file-input file-input-bordered w-full' %>
|
<%= f.file_field "user_import[files][]",
|
||||||
|
multiple: false,
|
||||||
|
direct_upload: true,
|
||||||
|
accept: '.zip',
|
||||||
|
required: true,
|
||||||
|
class: 'file-input file-input-bordered w-full',
|
||||||
|
data: { direct_upload_target: "input" } %>
|
||||||
|
<div class="text-sm text-gray-500 mt-2">
|
||||||
|
Files will be uploaded directly to storage. Please be patient during upload.
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="modal-action">
|
<div class="modal-action">
|
||||||
<%= f.submit "Import Data", class: 'btn btn-primary', data: { disable_with: 'Importing...' } %>
|
<%= f.submit "Import Data",
|
||||||
|
class: 'btn btn-primary',
|
||||||
|
data: {
|
||||||
|
direct_upload_target: "submit",
|
||||||
|
disable_with: 'Importing...'
|
||||||
|
} %>
|
||||||
<button type="button" class="btn" onclick="import_modal.close()">Cancel</button>
|
<button type="button" class="btn" onclick="import_modal.close()">Cancel</button>
|
||||||
</div>
|
</div>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue