dawarich/app/models/export.rb

40 lines
994 B
Ruby
Raw Permalink Normal View History

2024-06-12 14:29:38 -04:00
# frozen_string_literal: true
class Export < ApplicationRecord
belongs_to :user
2024-09-05 15:01:59 -04:00
enum :status, { created: 0, processing: 1, completed: 2, failed: 3 }
enum :file_format, { json: 0, gpx: 1, archive: 2 }
enum :file_type, { points: 0, user_data: 1 }
2024-06-12 14:29:38 -04:00
validates :name, presence: true
has_one_attached :file
2024-06-12 14:29:38 -04:00
2025-06-26 13:24:40 -04:00
after_commit -> { ExportJob.perform_later(id) }, on: :create, unless: -> { user_data? || archive? }
after_commit -> { remove_attached_file }, on: :destroy
2024-06-12 14:29:38 -04:00
def process!
Exports::Create.new(export: self).call
2024-06-12 14:29:38 -04:00
end
2025-04-06 10:31:39 -04:00
def migrate_to_new_storage
file.attach(io: File.open("public/#{url}"), filename: name)
update!(url: nil)
File.delete("public/#{url}")
rescue StandardError => e
Rails.logger.debug("Error migrating export #{id}: #{e.message}")
end
private
def remove_attached_file
2025-04-03 12:43:30 -04:00
file.purge_later
2025-04-06 10:31:39 -04:00
File.delete("public/#{url}")
rescue StandardError => e
Rails.logger.debug("Error removing export #{id}: #{e.message}")
end
2024-06-12 14:29:38 -04:00
end