working on image feature
This commit is contained in:
@@ -1,9 +1,15 @@
|
||||
require "base64"
|
||||
|
||||
class BackupExport
|
||||
FORMAT = "car_tracker_backup".freeze
|
||||
VERSION = 1
|
||||
VERSION = 2
|
||||
|
||||
def self.call
|
||||
new.call
|
||||
def self.call(include_images: false)
|
||||
new(include_images: include_images).call
|
||||
end
|
||||
|
||||
def initialize(include_images: false)
|
||||
@include_images = include_images
|
||||
end
|
||||
|
||||
def call
|
||||
@@ -17,6 +23,8 @@ class BackupExport
|
||||
|
||||
private
|
||||
|
||||
attr_reader :include_images
|
||||
|
||||
def vehicles
|
||||
Vehicle.includes(
|
||||
:maintenance_schedules,
|
||||
@@ -67,7 +75,7 @@ class BackupExport
|
||||
"gallons_pumped" => decimal_string(entry.gallons_pumped),
|
||||
"price_paid" => decimal_string(entry.price_paid),
|
||||
"updated_by_user" => user_data(entry.updated_by_user)
|
||||
}
|
||||
}.tap { |data| data["images"] = images(entry) if include_images }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -80,6 +88,18 @@ class BackupExport
|
||||
"odometer" => entry.odometer,
|
||||
"cost" => decimal_string(entry.cost),
|
||||
"updated_by_user" => user_data(entry.updated_by_user)
|
||||
}.tap { |data| data["images"] = images(entry) if include_images }
|
||||
end
|
||||
end
|
||||
|
||||
def images(entry)
|
||||
entry.images.map do |image|
|
||||
{
|
||||
"filename" => image.filename.to_s,
|
||||
"content_type" => image.content_type,
|
||||
"checksum" => image.checksum,
|
||||
"byte_size" => image.byte_size,
|
||||
"data_base64" => Base64.strict_encode64(image.download)
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
require "base64"
|
||||
require "digest/md5"
|
||||
require "stringio"
|
||||
|
||||
class BackupImport
|
||||
SUPPORTED_VERSION = 1
|
||||
SUPPORTED_VERSIONS = [ 1, 2 ].freeze
|
||||
MODES = %w[merge replace].freeze
|
||||
|
||||
class Error < StandardError; end
|
||||
@@ -46,9 +50,9 @@ class BackupImport
|
||||
|
||||
def migrate_backup(backup)
|
||||
version = backup["version"].to_i
|
||||
return backup if version == SUPPORTED_VERSION
|
||||
return backup if SUPPORTED_VERSIONS.include?(version)
|
||||
|
||||
if version > SUPPORTED_VERSION
|
||||
if version > SUPPORTED_VERSIONS.max
|
||||
raise Error, "Backup version #{version} is newer than this application supports."
|
||||
end
|
||||
|
||||
@@ -57,6 +61,8 @@ class BackupImport
|
||||
|
||||
def replace_app_data!
|
||||
UserSession.delete_all
|
||||
FuelEntry.find_each { |entry| entry.images.purge }
|
||||
MaintenanceEntry.find_each { |entry| entry.images.purge }
|
||||
FuelEntry.delete_all
|
||||
MaintenanceEntry.delete_all
|
||||
MaintenanceSchedule.delete_all
|
||||
@@ -132,18 +138,23 @@ class BackupImport
|
||||
def import_fuel_entries(vehicle, entries)
|
||||
entries.each do |entry_data|
|
||||
user = import_user(entry_data.fetch("updated_by_user"))
|
||||
next if fuel_entry_exists?(vehicle, entry_data, user)
|
||||
entry = matching_fuel_entry(vehicle, entry_data, user)
|
||||
|
||||
FuelEntry.insert!({
|
||||
vehicle_id: vehicle.id,
|
||||
updated_by_user_id: user.id,
|
||||
odometer: integer_value(entry_data.fetch("odometer")),
|
||||
gallons_pumped: decimal_value(entry_data.fetch("gallons_pumped")),
|
||||
price_paid: decimal_value(entry_data.fetch("price_paid")),
|
||||
date: date_value(entry_data.fetch("date")),
|
||||
created_at: Time.current,
|
||||
updated_at: Time.current
|
||||
})
|
||||
unless entry
|
||||
FuelEntry.insert!({
|
||||
vehicle_id: vehicle.id,
|
||||
updated_by_user_id: user.id,
|
||||
odometer: integer_value(entry_data.fetch("odometer")),
|
||||
gallons_pumped: decimal_value(entry_data.fetch("gallons_pumped")),
|
||||
price_paid: decimal_value(entry_data.fetch("price_paid")),
|
||||
date: date_value(entry_data.fetch("date")),
|
||||
created_at: Time.current,
|
||||
updated_at: Time.current
|
||||
})
|
||||
entry = matching_fuel_entry(vehicle, entry_data, user)
|
||||
end
|
||||
|
||||
import_images(entry, array_value(entry_data, "images"))
|
||||
end
|
||||
rescue KeyError => error
|
||||
raise Error, "Fuel entry is missing required field #{error.key}."
|
||||
@@ -152,19 +163,24 @@ class BackupImport
|
||||
def import_maintenance_entries(vehicle, entries)
|
||||
entries.each do |entry_data|
|
||||
user = import_user(entry_data.fetch("updated_by_user"))
|
||||
next if maintenance_entry_exists?(vehicle, entry_data, user)
|
||||
entry = matching_maintenance_entry(vehicle, entry_data, user)
|
||||
|
||||
MaintenanceEntry.insert!({
|
||||
vehicle_id: vehicle.id,
|
||||
updated_by_user_id: user.id,
|
||||
name: string_value(entry_data.fetch("name")),
|
||||
notes: string_value(entry_data.fetch("notes")),
|
||||
odometer: integer_value(entry_data.fetch("odometer")),
|
||||
cost: decimal_value(entry_data.fetch("cost")),
|
||||
date: date_value(entry_data.fetch("date")),
|
||||
created_at: Time.current,
|
||||
updated_at: Time.current
|
||||
})
|
||||
unless entry
|
||||
MaintenanceEntry.insert!({
|
||||
vehicle_id: vehicle.id,
|
||||
updated_by_user_id: user.id,
|
||||
name: string_value(entry_data.fetch("name")),
|
||||
notes: string_value(entry_data.fetch("notes")),
|
||||
odometer: integer_value(entry_data.fetch("odometer")),
|
||||
cost: decimal_value(entry_data.fetch("cost")),
|
||||
date: date_value(entry_data.fetch("date")),
|
||||
created_at: Time.current,
|
||||
updated_at: Time.current
|
||||
})
|
||||
entry = matching_maintenance_entry(vehicle, entry_data, user)
|
||||
end
|
||||
|
||||
import_images(entry, array_value(entry_data, "images"))
|
||||
end
|
||||
rescue KeyError => error
|
||||
raise Error, "Maintenance entry is missing required field #{error.key}."
|
||||
@@ -182,17 +198,17 @@ class BackupImport
|
||||
raise Error, "User is missing required field #{error.key}."
|
||||
end
|
||||
|
||||
def fuel_entry_exists?(vehicle, entry_data, user)
|
||||
def matching_fuel_entry(vehicle, entry_data, user)
|
||||
vehicle.fuel_entries.where(
|
||||
updated_by_user: user,
|
||||
odometer: integer_value(entry_data.fetch("odometer")),
|
||||
gallons_pumped: decimal_value(entry_data.fetch("gallons_pumped")),
|
||||
price_paid: decimal_value(entry_data.fetch("price_paid")),
|
||||
date: date_value(entry_data.fetch("date"))
|
||||
).exists?
|
||||
).first
|
||||
end
|
||||
|
||||
def maintenance_entry_exists?(vehicle, entry_data, user)
|
||||
def matching_maintenance_entry(vehicle, entry_data, user)
|
||||
vehicle.maintenance_entries.where(
|
||||
updated_by_user: user,
|
||||
name: string_value(entry_data.fetch("name")),
|
||||
@@ -200,7 +216,49 @@ class BackupImport
|
||||
odometer: integer_value(entry_data.fetch("odometer")),
|
||||
cost: decimal_value(entry_data.fetch("cost")),
|
||||
date: date_value(entry_data.fetch("date"))
|
||||
).exists?
|
||||
).first
|
||||
end
|
||||
|
||||
def import_images(entry, images)
|
||||
images.each do |image_data|
|
||||
filename = string_value(image_data.fetch("filename"))
|
||||
checksum = string_value(image_data.fetch("checksum"))
|
||||
next if image_attached?(entry, filename, checksum)
|
||||
|
||||
content_type = string_value(image_data.fetch("content_type"))
|
||||
byte_size = integer_value(image_data.fetch("byte_size"))
|
||||
validate_image_metadata!(content_type, byte_size)
|
||||
|
||||
content = Base64.strict_decode64(string_value(image_data.fetch("data_base64")))
|
||||
raise Error, "Image byte size does not match backup metadata." unless content.bytesize == byte_size
|
||||
raise Error, "Image checksum does not match backup metadata." unless Digest::MD5.base64digest(content) == checksum
|
||||
|
||||
entry.images.attach(
|
||||
io: StringIO.new(content),
|
||||
filename: filename,
|
||||
content_type: content_type
|
||||
)
|
||||
end
|
||||
rescue ArgumentError
|
||||
raise Error, "Image data is not valid Base64."
|
||||
rescue KeyError => error
|
||||
raise Error, "Image is missing required field #{error.key}."
|
||||
end
|
||||
|
||||
def image_attached?(entry, filename, checksum)
|
||||
entry.images.any? do |image|
|
||||
image.filename.to_s == filename && image.checksum == checksum
|
||||
end
|
||||
end
|
||||
|
||||
def validate_image_metadata!(content_type, byte_size)
|
||||
unless EntryImageValidation::ALLOWED_IMAGE_CONTENT_TYPES.include?(content_type)
|
||||
raise Error, "Images must be JPEG or PNG."
|
||||
end
|
||||
|
||||
return if byte_size <= EntryImageValidation::MAX_IMAGE_SIZE
|
||||
|
||||
raise Error, "Images must be 10 MB or smaller."
|
||||
end
|
||||
|
||||
def recalculate_odometer(vehicle, incoming_odometer)
|
||||
|
||||
Reference in New Issue
Block a user