Class: VenusMediaLibrary::ImagesController
- Inherits:
-
ApplicationController
- Object
- ActionController::Base
- ApplicationController
- VenusMediaLibrary::ImagesController
- Includes:
- ImagesHelper
- Defined in:
- app/controllers/venus_media_library/images_controller.rb
Overview
Lists and uploads images backed by Active Storage. Storage-agnostic: it uses whatever service the host app configures (Disk in dev, S3 in prod, ...).
Constant Summary collapse
- MAX_PER_PAGE =
100
Instance Method Summary collapse
-
#create ⇒ Object
POST /images Accepts an uploaded file and stores it via Active Storage.
-
#index ⇒ Object
GET /images Lists image blobs, newest first, with simple offset pagination.
Methods included from ImagesHelper
#ml_blob_url, #ml_image_payload, #ml_previewable?, #ml_thumb_url
Instance Method Details
#create ⇒ Object
POST /images Accepts an uploaded file and stores it via Active Storage.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'app/controllers/venus_media_library/images_controller.rb', line 25 def create uploaded = params[:file] || params.dig(:image, :file) if uploaded.blank? return respond_error("No file was uploaded.", :unprocessable_entity) end detected_content_type = validated_content_type(uploaded) return unless detected_content_type return unless field_accepts?(detected_content_type) blob = ActiveStorage::Blob.create_and_upload!( io: uploaded.tempfile, filename: uploaded.original_filename, content_type: detected_content_type, service_name: VenusMediaLibrary.configuration.storage_service ) asset = VenusMediaLibrary::Asset.create!( blob: blob, owner: venus_media_library_user, community_shared: ActiveModel::Type::Boolean.new.cast(params[:community_shared]) || false ) respond_to do |format| format.json { render json: ml_image_payload(asset), status: :created } format.html { redirect_to images_path } end rescue ActiveRecord::RecordInvalid blob&.purge raise end |
#index ⇒ Object
GET /images Lists image blobs, newest first, with simple offset pagination. Responds with an HTML grid or a JSON payload for the picker.
12 13 14 15 16 17 18 19 20 21 |
# File 'app/controllers/venus_media_library/images_controller.rb', line 12 def index load_media_assets(visible_media_assets) respond_to do |format| format.html # index.html.erb format.json do render json: { images: @images, page: @page, has_more: @has_more, total: @total_count } end end end |