Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ gem 'aws-sdk-s3', '~>1'
# Rack::Proxy for S3 Proxy middleware
gem 'rack-proxy'

# Resize images for ActiveStorage
gem 'mini_magick'

group :development, :test do
gem "pry"
gem "pry-byebug"
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ GEM
mimemagic (0.3.10)
nokogiri (~> 1)
rake
mini_magick (4.11.0)
mini_mime (1.1.2)
mini_portile2 (2.8.0)
minitest (5.15.0)
Expand Down Expand Up @@ -376,6 +377,7 @@ DEPENDENCIES
letter_opener
listen
local_time
mini_magick
mocha
moneta
neat (= 1.8.0)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/course_materials_files_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def show

data = AttachmentReader.new(@file).read_attachment_data("file")

file_options = { filename: @file.file_file_name, disposition: "inline" }
file_options = { filename: @file.file.filename.to_s, disposition: "inline" }
send_data data, file_options
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/courses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def complete
def view_attachment
@course = Course.friendly.find(params[:course_id])
@file = @course.attachments.find(params[:attachment_id])
extension = File.extname(@file.document_file_name)
extension = @file.document.filename.extension_with_delimiter

data = AttachmentReader.new(@file).read_attachment_data("document")

Expand Down
7 changes: 4 additions & 3 deletions app/models/attachment.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
class Attachment < ApplicationRecord
belongs_to :course
has_attached_file :document, url: "attachments/documents/:id/:basename.:extension"
has_one_attached :document
# has_attached_file :document, url: "attachments/documents/:id/:basename.:extension"

validates_attachment_content_type :document,
content_type: Constants.attachment_content_types, message: "only PDF, Word, PowerPoint, Excel, or Story files are allowed."
# validates_attachment_content_type :document,
# content_type: Constants.attachment_content_types, message: "only PDF, Word, PowerPoint, Excel, or Story files are allowed."
validates :doc_type, allow_blank: true, inclusion: { in: %w(supplemental post-course), message: "%{value} is not a doc_type" }

scope :supplemental_attachments, -> { where(doc_type: "supplemental") }
Expand Down
9 changes: 5 additions & 4 deletions app/models/course_material_file.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
class CourseMaterialFile < ApplicationRecord
belongs_to :course_material
has_attached_file :file, url: "coursematerialfiles/files/:id/:basename.:extension"
has_one_attached :file
# has_attached_file :file, url: "coursematerialfiles/files/:id/:basename.:extension"

validates :file_file_name, uniqueness: { scope: :course_material, message: "should be unique for the course" }
# validates :file_file_name, uniqueness: { scope: :course_material, message: "should be unique for the course" }

validates_attachment_content_type :file,
content_type: Constants.course_material_file_types, message: "Only PDF, CSV, Word, PowerPoint, or Excel files are allowed."
# validates_attachment_content_type :file,
# content_type: Constants.course_material_file_types, message: "Only PDF, CSV, Word, PowerPoint, or Excel files are allowed."
end
11 changes: 6 additions & 5 deletions app/models/course_material_media.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
class CourseMaterialMedia < ApplicationRecord
belongs_to :course_material
has_attached_file :media, styles: { thumb: "212x140#" },
url: "coursematerialmedia/media/:id/:style/:basename.:extension"
has_one_attached :media
# has_attached_file :media, styles: { thumb: "212x140#" },
# url: "coursematerialmedia/media/:id/:style/:basename.:extension"

validates :media_file_name, uniqueness: { scope: :course_material, message: "should be unique for the course" }
# validates :media_file_name, uniqueness: { scope: :course_material, message: "should be unique for the course" }

validates_attachment_content_type :media, content_type: Constants.course_material_media_types,
message: "Only PNG, JPG and GIF files are allowed."
# validates_attachment_content_type :media, content_type: Constants.course_material_media_types,
# message: "Only PNG, JPG and GIF files are allowed."

end
10 changes: 2 additions & 8 deletions app/services/attachment_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@ def initialize(file)
end

def read_attachment_data(attachment_name)
attachment_path = @file.send(attachment_name).path

if Rails.application.config.s3_enabled
s3 = Aws::S3::Client.new
response = s3.get_object({
bucket: Rails.application.config.s3_bucket_name,
key: attachment_path
})
response.body.read
@file.send(attachment_name).download
else
attachment_path = ActiveStorage::Blob.service.path_for(@file.send(attachment_name).key)
open(attachment_path).read
end
end
Expand Down
12 changes: 8 additions & 4 deletions app/services/attachment_zipper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ def create_zip(attachment_name)

::Zip::File.open(archive_tempfile.path, ::Zip::File::CREATE) do |zipfile|
@files.each do |file|
attachment = file.send(attachment_name)

if Rails.application.config.s3_enabled
s3_tempfile = Tempfile.new("s3_contents", "tmp")
s3_tempfile << AttachmentReader.new(file).read_attachment_data(attachment_name)
zipfile.add(file.send("#{attachment_name}_file_name"), s3_tempfile.path)
blob = attachment.blob
blob.open do |tempfile|
zipfile.add(attachment.filename.to_s, tempfile.filename)
end
else
zipfile.add(file.send("#{attachment_name}_file_name"), file.send(attachment_name).path)
file_path = ActiveStorage::Blob.service.path_for(attachment.key)
zipfile.add(attachment.filename.to_s, file_path)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<div class="nested-fields">
<div class="js-form-upload">
<%= f.label :file do %>
<% if f.object.file.present? %>
<% if f.object.file.attached? %>
<small class="padding-left">
(<%= f.object.file_file_name %> - <%= number_to_human_size(f.object.file.size) %>)
(<%= f.object.file.filename %> - <%= number_to_human_size(f.object.file.byte_size) %>)
</small>
<% else %>
<small>New File</small>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<div class="nested-fields">
<div class="js-form-upload">
<%= f.label :media do %>
<% if f.object.media.present? %>
<% if f.object.media.attached? %>
<small class="padding-left">
(<%= f.object.media_file_name %> - <%= number_to_human_size(f.object.media.size) %>)
(<%= f.object.media.filename %> - <%= number_to_human_size(f.object.media.byte_size) %>)
</small>
<% else %>
<small>New File</small>
Expand Down
4 changes: 2 additions & 2 deletions app/views/admin/courses/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
<ul>
<% @course.attachments.supplemental_attachments.each do |a| %>
<li>
<%= a.document_file_name %> -
<%= a.document.filename %> -
<%= link_to "Delete", admin_attachment_path(a), method: :delete, data: { confirm: "Are you sure you want to delete this attachment?" } %>
</li>
<% end %>
Expand All @@ -83,7 +83,7 @@
<ul>
<% @course.attachments.post_course_attachments.each do |a| %>
<li>
<%= a.document_file_name %> -
<%= a.document.filename %> -
<%= link_to "Delete", admin_attachment_path(a), method: :delete, data: { confirm: "Are you sure you want to delete this attachment?" } %>
</li>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/course_materials/_file_grid.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<%= link_to course_material_course_materials_file_path(@course_material, f), target: "_blank" do %>
<div class="course-material-file-widget">
<div class="file-icon"><%= svg_tag "icon-download", class: "svg-icon-large white" %></div>
<div class="file-type"><%= f.file_file_name.truncate(14) %></div>
<div class="file-type"><%= f.file.filename.to_s.truncate(14) %></div>
<div class="file-name"><%= mime_type_conversion(f.file_content_type) %></div>
</div>
<% end %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/course_materials/_media_grid.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<% course_material.course_material_medias.each do |f| %>
<%= link_to course_material_course_materials_media_path(@course_material, f), target: "_blank" do %>
<div class="course-material-media-widget">
<%= image_tag f.media.url(:thumb), class: "media-image" %>
<div class="media-title note"><%= f.media_file_name %></div>
<%= image_tag f.media.variant(resize: "212x140").processed, class: "media-image" %>
<div class="media-title note"><%= f.media.filename %></div>
</div>
<% end %>
<% end %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/course_materials/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
<div class="eight columns course-material-meta">
<%= svg_tag "icon-files", class: "svg-icon-medium teal" %>
<span style="position: relative; top: -15px;">
<%= @course_material.course_material_files.size %> files and
<%= @course_material.course_material_medias.size %> images
<%= @course_material.course_material_files.count %> files and
<%= @course_material.course_material_medias.count %> images
</span>
</div>

Expand Down
2 changes: 1 addition & 1 deletion app/views/courses/_post_course_materials.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<ul class="unmarked no-padding">
<% course.attachments.post_course_attachments.each do |a| %>
<%= link_to course_attachment_path(course, a), target: "_blank" do %>
<li><i class="icon-download download-color"></i><%= a.document_file_name %></li>
<li><i class="icon-download download-color"></i><%= a.document.filename %></li>
<% end %>
<% end %>
</ul>
6 changes: 3 additions & 3 deletions app/views/shared/courses/_supplemental_materials.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<ul class="unmarked no-padding">
<% course.supplemental_attachments.each do |a| %>
<%= link_to course_attachment_path(course, a), target: "_blank" do %>
<li data-document-name="<%= a.document_file_name %>">
<i class="icon-download download-color"></i><%= a.document_file_name %>
<li data-document-name="<%= a.document.filename %>">
<i class="icon-download download-color"></i><%= a.document.filename %>
<p class="note file-description"><%= a.file_description %></p>
</li>
<% end %>
Expand All @@ -25,7 +25,7 @@
<% @course.post_course_attachments.each do |a| %>
<%= link_to course_attachment_path(@course, a), target: "_blank" do %>
<li >
<i class="icon-download download-color"></i><%= truncate(a.document_file_name, length: 28) %>
<i class="icon-download download-color"></i><%= truncate(a.document.filename, length: 28) %>
<p class="note file-description"><%= a.file_description %></p>
</li>
<% end %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/shared/lessons/_supplemental_materials.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<ul class="unmarked no-padding">
<% course.supplemental_attachments.each do |a| %>
<%= link_to course_attachment_path(course, a), target: "_blank" do %>
<li data-document-name="<%= a.document_file_name %>">
<i class="icon-download download-color"></i><%= a.document_file_name %>
<li data-document-name="<%= a.document.filename %>">
<i class="icon-download download-color"></i><%= a.document.filename %>
<p class="note file-description"><%= a.file_description %></p>
</li>
<% end %>
Expand Down
3 changes: 2 additions & 1 deletion config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,6 @@
end

# S3 Overrides
#config.s3_enabled = false
# config.s3_enabled = false
# config.active_storage.service = :local
end
3 changes: 3 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
# S3 configuration
config.s3_enabled = false

# Active storage test config
config.active_storage.service = :test

# Paperclip test path
Paperclip::Attachment.default_options[:path] = "#{Rails.root}/spec/test_files/:url"
end
5 changes: 5 additions & 0 deletions config/s3_enabled_environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ class Application < Rails::Application
config.s3_bucket_name = "dl-training-uploads-#{Rails.env}"
config.s3_region = "us-west-2"

# Active storage S3 config for normal attachments
config.active_storage.service = :s3

# Paperclip for storyline files
# TODO? migrate storyline files into shared s3 bucket
config.paperclip_defaults = {
path: ":url",
storage: :s3,
Expand Down
12 changes: 12 additions & 0 deletions config/storage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
local:
service: Disk
root: <%= Rails.root.join("storage") %>

test:
service: Disk
root: <%= Rails.root.join("tmp/storage") %>

s3:
service: S3
region: us-west-2
bucket: <%= "dl-training-uploads-#{Rails.env}" %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# This migration comes from active_storage (originally 20170806125915)
class CreateActiveStorageTables < ActiveRecord::Migration[5.2]
def change
create_table :active_storage_blobs do |t|
t.string :key, null: false
t.string :filename, null: false
t.string :content_type
t.text :metadata
t.bigint :byte_size, null: false
t.string :checksum, null: false
t.datetime :created_at, null: false

t.index [ :key ], unique: true
end

create_table :active_storage_attachments do |t|
t.string :name, null: false
t.references :record, null: false, polymorphic: true, index: false
t.references :blob, null: false

t.datetime :created_at, null: false

t.index [ :record_type, :record_id, :name, :blob_id ], name: "index_active_storage_attachments_uniqueness", unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end
end
end
24 changes: 23 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,32 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2021_09_24_080820) do
ActiveRecord::Schema.define(version: 2022_06_16_234453) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
t.bigint "record_id", null: false
t.bigint "blob_id", null: false
t.datetime "created_at", null: false
t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
end

create_table "active_storage_blobs", force: :cascade do |t|
t.string "key", null: false
t.string "filename", null: false
t.string "content_type"
t.text "metadata"
t.bigint "byte_size", null: false
t.string "checksum", null: false
t.datetime "created_at", null: false
t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
end

create_table "attachments", force: :cascade do |t|
t.integer "course_id"
t.string "title"
Expand Down Expand Up @@ -188,4 +209,5 @@
t.index ["unlock_token"], name: "index_users_on_unlock_token", unique: true
end

add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
end
Empty file removed lib/tasks/.keep
Empty file.
Loading