From b2f84307b3876711a808b39bd1a51fe85f284b66 Mon Sep 17 00:00:00 2001 From: Chris Beer Date: Wed, 4 Nov 2020 21:13:26 -0800 Subject: [PATCH] Support PUT requests to create new uploads with a given slug --- app/controllers/uploads_controller.rb | 22 +++++++++++++++++-- app/models/upload.rb | 2 ++ .../20201105045358_add_slug_to_upload.rb | 6 +++++ db/schema.rb | 4 +++- 4 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20201105045358_add_slug_to_upload.rb diff --git a/app/controllers/uploads_controller.rb b/app/controllers/uploads_controller.rb index 1c65fe4c..0c493516 100644 --- a/app/controllers/uploads_controller.rb +++ b/app/controllers/uploads_controller.rb @@ -5,7 +5,8 @@ class UploadsController < ApplicationController load_and_authorize_resource :organization before_action :load_stream authorize_resource :stream - load_and_authorize_resource through: :stream + before_action :load_upload + authorize_resource through: :stream protect_from_forgery with: :null_session, if: :jwt_token # GET /uploads @@ -62,12 +63,29 @@ def destroy private + def load_upload + @upload = @stream.uploads.find_or_create_by(slug: params[:id]) do |upload| + upload.name = params[:id] + end + end + def load_stream @stream = @organization.default_stream end # Only allow a list of trusted parameters through. def upload_params - params.require(:upload).permit(:name, :stream_id, files: []) + if request.form_data? + params.require(:upload).permit(:name, :stream_id, files: []) + elsif request.put? + { + name: params[:id], + files: [{ + io: request.body_stream, + filename: params[:id], + content_type: request.content_type + }] + } + end end end diff --git a/app/models/upload.rb b/app/models/upload.rb index c1e9f2f2..7ea0b9a9 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -3,6 +3,8 @@ # :nodoc: class Upload < ApplicationRecord has_paper_trail + extend FriendlyId + friendly_id :name, use: %i[finders slugged scoped], scope: %i[stream] belongs_to :stream, touch: true has_one :organization, through: :stream has_many :marc_records, dependent: :delete_all diff --git a/db/migrate/20201105045358_add_slug_to_upload.rb b/db/migrate/20201105045358_add_slug_to_upload.rb new file mode 100644 index 00000000..74ec08b1 --- /dev/null +++ b/db/migrate/20201105045358_add_slug_to_upload.rb @@ -0,0 +1,6 @@ +class AddSlugToUpload < ActiveRecord::Migration[6.0] + def change + add_column :uploads, :slug, :string + add_index :uploads, :slug + end +end diff --git a/db/schema.rb b/db/schema.rb index dc59717d..ac9b28f8 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2020_11_04_143930) do +ActiveRecord::Schema.define(version: 2020_11_05_045358) do create_table "active_storage_attachments", force: :cascade do |t| t.string "name", null: false @@ -117,6 +117,8 @@ t.integer "stream_id" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false + t.string "slug" + t.index ["slug"], name: "index_uploads_on_slug" t.index ["stream_id"], name: "index_uploads_on_stream_id" end