[UNSTABLE] Local file storage works. NEXT: GCS

pull/1338/head
Rick Carlino 2019-07-22 14:09:40 -05:00
parent 79bd373260
commit e8ebaf4ed8
3 changed files with 14 additions and 13 deletions

View File

@ -43,11 +43,12 @@ class Image < ApplicationRecord
# validates_attachment_content_type :attachment,
# content_type: CONTENT_TYPES
# ========= /DEPRECATED PAPERCLIP STUFF ========
has_one_attached :attachment
def set_attachment_by_url(url)
# File
# URI::HTTPS
self.attachment = open(url)
attachment.attach(io: open(url), filename: "image_#{self.id}")
self.attachment_processed_at = Time.now
self
end
@ -66,6 +67,15 @@ class Image < ApplicationRecord
end
def attachment_url(size = "x640")
# Detect legacy attachments by way of
# superceded PaperClip-related field.
# If it has an `attachment_file_size`,
# it was made with paperclip.
if !attachment_file_size
return ROOT_PATH +
Rails.application.routes.url_helpers.rails_blob_path(attachment)
end
if attachment_processed_at
url = IMAGE_URL_TPL % {
chunks: id.to_s.rjust(9, "0").scan(/.{3}/).join("/"),
@ -78,12 +88,4 @@ class Image < ApplicationRecord
return DEFAULT_URL
end
end
def attachment=(_)
puts "FIXME: THIS IS DEPRECATED"
end
def attachment
raise "Deprecated"
end
end

View File

@ -12,6 +12,7 @@ module FarmBot
Delayed::Worker.max_attempts = 4
REDIS_ENV_KEY = ENV.fetch("WHERE_IS_REDIS_URL", "REDIS_URL")
REDIS_URL = ENV.fetch(REDIS_ENV_KEY, "redis://redis:6379/0")
config.active_storage.service = :local
config.cache_store = :redis_cache_store, { url: REDIS_URL }
config.middleware.use Rack::Attack
config.active_record.schema_format = :sql

View File

@ -4,15 +4,13 @@ describe Image do
let(:device) { FactoryBot.create(:device) }
it "adds URL attachments", :slow do
pending("WIP - replace this with ActiveStorage stuff")
image = Image.create(device: device)
expect(image.attachment_processed_at).to be_nil
expect(image.attachment.exists?).to be_falsy
expect(image.attachment.attached?).to be false
image.set_attachment_by_url(FAKE_ATTACHMENT_URL)
image.save!
expect(image.attachment.exists?).to be_truthy
expect(image.attachment.attached?).to be true
expect(image.attachment_processed_at).to be_truthy
end
end