From 34460ad3db4afddcd2bbef19f99fe49204d9a084 Mon Sep 17 00:00:00 2001 From: Jacob Michalskie Date: Thu, 18 Apr 2024 09:40:01 +0200 Subject: [PATCH] Install activestorage for storing user uploaded files --- .gitignore | 1 + dist/obs-server.spec | 2 + src/api/Makefile | 1 + .../app/controllers/webui/webui_controller.rb | 1 + src/api/config/application.rb | 1 + src/api/config/environments/development.rb | 2 +- src/api/config/environments/production.rb | 2 +- src/api/config/environments/test.rb | 2 +- src/api/config/storage.yml | 7 ++ ...te_active_storage_tables.active_storage.rb | 64 +++++++++++++++++++ src/api/db/schema.rb | 32 +++++++++- 11 files changed, 111 insertions(+), 4 deletions(-) create mode 100644 src/api/config/storage.yml create mode 100644 src/api/db/migrate/20240418065204_create_active_storage_tables.active_storage.rb diff --git a/.gitignore b/.gitignore index 5504f6752153..17a0328d8d83 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/dist/obs-server.spec b/dist/obs-server.spec index 3825c8900410..eaf2e30f1242 100644 --- a/dist/obs-server.spec +++ b/dist/obs-server.spec @@ -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 @@ -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: diff --git a/src/api/Makefile b/src/api/Makefile index 46c974d74a85..257950e4ad37 100644 --- a/src/api/Makefile +++ b/src/api/Makefile @@ -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 diff --git a/src/api/app/controllers/webui/webui_controller.rb b/src/api/app/controllers/webui/webui_controller.rb index 0a137b7889e7..de059c95f795 100644 --- a/src/api/app/controllers/webui/webui_controller.rb +++ b/src/api/app/controllers/webui/webui_controller.rb @@ -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 diff --git a/src/api/config/application.rb b/src/api/config/application.rb index fc0af305144a..44873c761e92 100644 --- a/src/api/config/application.rb +++ b/src/api/config/application.rb @@ -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' diff --git a/src/api/config/environments/development.rb b/src/api/config/environments/development.rb index cd6978b423cc..dc8f34238f94 100644 --- a/src/api/config/environments/development.rb +++ b/src/api/config/environments/development.rb @@ -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 diff --git a/src/api/config/environments/production.rb b/src/api/config/environments/production.rb index f797884d5129..fc5ac7084862 100644 --- a/src/api/config/environments/production.rb +++ b/src/api/config/environments/production.rb @@ -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 diff --git a/src/api/config/environments/test.rb b/src/api/config/environments/test.rb index fee2c2be992f..253801b58320 100644 --- a/src/api/config/environments/test.rb +++ b/src/api/config/environments/test.rb @@ -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 diff --git a/src/api/config/storage.yml b/src/api/config/storage.yml new file mode 100644 index 000000000000..1f93f732379a --- /dev/null +++ b/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") %> diff --git a/src/api/db/migrate/20240418065204_create_active_storage_tables.active_storage.rb b/src/api/db/migrate/20240418065204_create_active_storage_tables.active_storage.rb new file mode 100644 index 000000000000..12a7313fe169 --- /dev/null +++ b/src/api/db/migrate/20240418065204_create_active_storage_tables.active_storage.rb @@ -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 diff --git a/src/api/db/schema.rb b/src/api/db/schema.rb index 40339780b1db..e6605a4c43cd 100644 --- a/src/api/db/schema.rb +++ b/src/api/db/schema.rb @@ -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 @@ -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"