Skip to content

Commit

Permalink
Install activestorage for storing user uploaded files
Browse files Browse the repository at this point in the history
  • Loading branch information
hellcp-work committed Apr 18, 2024
1 parent c2ef1bd commit 34460ad
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -9,6 +9,7 @@
/src/api/config/options.yml
/src/api/config/secret.key
/src/api/config/thinking_sphinx.yml
/src/api/storage
/src/api/db/sphinx
/src/api/config/*.sphinx.conf
/src/api/coverage
Expand Down
2 changes: 2 additions & 0 deletions dist/obs-server.spec
Expand Up @@ -999,6 +999,7 @@ usermod -a -G docker obsservicerun
%config(noreplace) %{__obs_api_prefix}/config/puma.rb
%config(noreplace) %{__obs_api_prefix}/config/secrets.yml
%config(noreplace) %{__obs_api_prefix}/config/spring.rb
%config(noreplace) %{__obs_api_prefix}/config/storage.yml
%config(noreplace) %{__obs_api_prefix}/config/crawler-user-agents.json
%{__obs_api_prefix}/config/initializers
%dir %{__obs_api_prefix}/config/environments
Expand Down Expand Up @@ -1080,6 +1081,7 @@ usermod -a -G docker obsservicerun
%config %{__obs_api_prefix}/config/environments/stage.rb

%dir %attr(-,%{apache_user},%{apache_group}) %{__obs_api_prefix}/log
%dir %attr(-,%{apache_user},%{apache_group}) %{__obs_api_prefix}/storage
%attr(-,%{apache_user},%{apache_group}) %{__obs_api_prefix}/tmp

# these dirs primarily belong to apache2:
Expand Down
1 change: 1 addition & 0 deletions src/api/Makefile
Expand Up @@ -11,6 +11,7 @@ install: prepare_dirs prepare_rake docs config log_files build
prepare_dirs:
$(INSTALL) -d -m 755 $(DESTDIR)$(OBS_API_PREFIX)
$(INSTALL) -d -m 755 $(DESTDIR)$(OBS_API_PREFIX)/log
$(INSTALL) -d -m 755 $(DESTDIR)$(OBS_API_PREFIX)/storage
$(INSTALL) -d -m 755 $(DESTDIR)$(OBS_API_PREFIX)/tmp
$(INSTALL) -d -m 755 $(DESTDIR)$(OBS_API_PREFIX)/config
# prepare for running sphinx daemon
Expand Down
1 change: 1 addition & 0 deletions src/api/app/controllers/webui/webui_controller.rb
Expand Up @@ -12,6 +12,7 @@ class Webui::WebuiController < ActionController::Base
include RescueAuthorizationHandler
include SetCurrentRequestDetails
include Webui::ElisionsHelper
include ActiveStorage::SetCurrent
protect_from_forgery

before_action :setup_view_path
Expand Down
1 change: 1 addition & 0 deletions src/api/config/application.rb
Expand Up @@ -7,6 +7,7 @@
require 'action_mailer/railtie'
require 'action_controller/railtie'
require 'action_view/railtie'
require 'active_storage/engine'
require 'sprockets/railtie'
require 'rails/test_unit/railtie'

Expand Down
2 changes: 1 addition & 1 deletion src/api/config/environments/development.rb
Expand Up @@ -45,7 +45,7 @@
end

# Store uploaded files on the local file system (see config/storage.yml for options).
# config.active_storage.service = :local
config.active_storage.service = :local

# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
Expand Down
2 changes: 1 addition & 1 deletion src/api/config/environments/production.rb
Expand Up @@ -43,7 +43,7 @@
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX

# Store uploaded files on the local file system (see config/storage.yml for options).
# config.active_storage.service = :local
config.active_storage.service = :local

# Mount Action Cable outside main process or domain.
# config.action_cable.mount_path = nil
Expand Down
2 changes: 1 addition & 1 deletion src/api/config/environments/test.rb
Expand Up @@ -36,7 +36,7 @@
# config.action_dispatch.show_exceptions = false

# Store uploaded files on the local file system in a temporary directory.
# config.active_storage.service = :test
config.active_storage.service = :test

# Tell Action Mailer not to deliver emails to the real world.
# The :test delivery method accumulates sent emails in the
Expand Down
7 changes: 7 additions & 0 deletions src/api/config/storage.yml
@@ -0,0 +1,7 @@
local:
service: Disk
root: <%= Rails.root.join("storage") %>

test:
service: Disk
root: <%= Rails.root.join("tmp/storage") %>
@@ -0,0 +1,64 @@
# This migration comes from active_storage (originally 20170806125915)
class CreateActiveStorageTables < ActiveRecord::Migration[7.0]
def change
# Use Active Record's configured type for primary and foreign keys
primary_key_type, foreign_key_type = primary_and_foreign_key_types

create_table :active_storage_blobs, id: primary_key_type do |t|
t.string :key, null: false
t.string :filename, null: false
t.string :content_type
t.text :metadata
t.string :service_name, null: false
t.bigint :byte_size, null: false
t.string :checksum

if connection.supports_datetime_with_precision?
t.datetime :created_at, precision: 6, null: false
else
t.datetime :created_at, null: false
end

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

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

if connection.supports_datetime_with_precision?
t.datetime :created_at, precision: 6, null: false
else
t.datetime :created_at, null: false
end

t.index %i[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

create_table :active_storage_variant_records, id: primary_key_type do |t|
t.belongs_to :blob, null: false, index: false, type: foreign_key_type
t.string :variation_digest, null: false

if connection.supports_datetime_with_precision?
t.datetime :created_at, precision: 6, null: false
else
t.datetime :created_at, null: false
end

t.index %i[blob_id variation_digest], name: :index_active_storage_variant_records_uniqueness, unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end
end

private

def primary_and_foreign_key_types
config = Rails.configuration.generators
setting = config.options[config.orm][:primary_key_type]
primary_key_type = setting || :primary_key
foreign_key_type = setting || :bigint
[primary_key_type, foreign_key_type]
end
end
32 changes: 31 additions & 1 deletion src/api/db/schema.rb
Expand Up @@ -10,7 +10,35 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2024_04_11_150057) do
ActiveRecord::Schema[7.0].define(version: 2024_04_18_065204) do
create_table "active_storage_attachments", id: :integer, charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
t.integer "record_id", null: false
t.integer "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", id: :integer, charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t|
t.string "key", null: false
t.string "filename", null: false
t.string "content_type"
t.text "metadata"
t.string "service_name", null: false
t.bigint "byte_size", null: false
t.string "checksum"
t.datetime "created_at", null: false
t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
end

create_table "active_storage_variant_records", id: :integer, charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t|
t.integer "blob_id", null: false
t.string "variation_digest", null: false
t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
end

create_table "appeals", charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t|
t.text "reason", null: false
t.integer "appellant_id", null: false
Expand Down Expand Up @@ -1213,6 +1241,8 @@
t.index ["user_id"], name: "index_workflow_token_users_on_user_id"
end

add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
add_foreign_key "appeals", "decisions"
add_foreign_key "appeals", "users", column: "appellant_id"
add_foreign_key "attrib_allowed_values", "attrib_types", name: "attrib_allowed_values_ibfk_1"
Expand Down

0 comments on commit 34460ad

Please sign in to comment.