-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
Description
Sync files and attach using ActiveStorage
class TopicFileSync
def sync(file_data, topic)
topic_file = TopicFile.find_or_initialize_by(external_id: file_data["id"])
# Skip if checksum unchanged (file hasn't changed on server)
return topic_file if topic_file.external_checksum == file_data["checksum"]
# Download and verify
downloaded_path = downloader.download(file_data["id"], expected_checksum: file_data["checksum"])
verifier.verify!(downloaded_path, file_data["checksum"])
# Attach via ActiveStorage
File.open(downloaded_path, "rb") do |io|
topic_file.file.attach(
io: io,
filename: file_data["filename"],
content_type: file_data["content_type"]
)
end
topic_file.update!(
topic: topic,
filename: file_data["filename"],
file_type: extract_file_type(file_data["content_type"]),
file_size: file_data["size_bytes"],
external_checksum: file_data["checksum"]
)
topic_file
ensure
FileUtils.rm_f(downloaded_path) if downloaded_path
end
endTrack and persist download progress for resume
- Create SyncProgress model:
create_table :sync_progress do |t|
t.string :manifest_version
t.string :status # pending, downloading, completed, failed
t.integer :total_files
t.integer :completed_files
t.integer :total_bytes
t.integer :downloaded_bytes
t.json :pending_file_ids # files remaining to download
t.json :failed_file_ids # files that failed
t.timestamps
end- Update progress after each file download
- Use for resume after interruption/restart