Class: Panda::Core::Admin::FilesController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/panda/core/admin/files_controller.rb

Instance Method Summary collapse

Methods inherited from BaseController

#add_breadcrumb, #authenticate_admin_user!, #authenticate_user!, #breadcrumbs, #current_user, #set_current_request_details, #user_signed_in?

Methods included from Panda::Core::Authorizable

#authorize!, #authorized_for?, #authorized_for_admin_access?, #can?

Instance Method Details

#createObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/controllers/panda/core/admin/files_controller.rb', line 55

def create
  # JSON uploads from EditorJS
  if params[:image].present?
    return create_from_editor
  end

  # File uploads from EditorJS AttachesTool
  if params[:file].present?
    return create_file_from_editor
  end

  # HTML form uploads from the file gallery
  file = params.dig(:file_upload, :file)
  category_id = params.dig(:file_upload, :file_category_id)

  if file.blank?
    redirect_to admin_files_path, alert: "Please select a file to upload."
    return
  end

  if category_id.blank?
    redirect_to admin_files_path, alert: "Please select a category."
    return
  end

  unless (category = Panda::Core::FileCategory.find_by(id: category_id))
    redirect_to admin_files_path, alert: "Selected category not found."
    return
  end

  blob = find_existing_blob(file) || ActiveStorage::Blob.create_and_upload!(
    io: file,
    filename: file.original_filename,
    content_type: file.content_type
  )

  stamp_uploader(blob)

  Panda::Core::FileCategorization.find_or_create_by!(file_category: category, blob: blob)

  redirect_to admin_files_path, notice: "File uploaded successfully."
end

#destroyObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/controllers/panda/core/admin/files_controller.rb', line 133

def destroy
  # Remove variant records and their blobs first (each variant has its own blob)
  @blob.variant_records.includes(image_attachment: :blob).find_each do |vr|
    vr.image.purge_later if vr.image.attached?
    vr.destroy
  end

  # Remove remaining FK references before purging the blob
  ActiveStorage::Attachment.where(blob_id: @blob.id).delete_all
  Panda::Core::FileCategorization.where(blob_id: @blob.id).delete_all
  @blob.purge_later

  redirect_to admin_files_path, notice: "File was successfully deleted.", status: :see_other
end

#indexObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/panda/core/admin/files_controller.rb', line 10

def index
  # Exclude variant blobs — they are attached to VariantRecord, not user-uploaded
  variant_blob_ids = ActiveStorage::Attachment
    .where(record_type: "ActiveStorage::VariantRecord")
    .select(:blob_id)

  @files = ActiveStorage::Blob
    .where.not(id: variant_blob_ids)
    .includes(:variant_records)
    .order(created_at: :desc)

  @file_categories = Panda::Core::FileCategory.ordered
  @active_category = params[:category]

  # Apply category filter
  if @active_category == "uncategorized"
    categorized_blob_ids = Panda::Core::FileCategorization.select(:blob_id)
    @files = @files.where.not(id: categorized_blob_ids)
  elsif @active_category.present?
    category = Panda::Core::FileCategory.find_by(slug: @active_category)
    if category
      blob_ids = Panda::Core::FileCategorization.where(file_category: category).select(:blob_id)
      @files = @files.where(id: blob_ids)
    end
  end

  # Preload category names for gallery display
  categorizations = Panda::Core::FileCategorization
    .where(blob_id: @files.select(:id))
    .includes(:file_category)
  @blob_categories = categorizations.each_with_object({}) do |cat, hash|
    hash[cat.blob_id] = cat.file_category
  end
end

#showObject



45
46
47
48
49
50
51
52
53
# File 'app/controllers/panda/core/admin/files_controller.rb', line 45

def show
  @file_categories = Panda::Core::FileCategory.ordered

  if request.xhr? || request.headers["Turbo-Frame"].present?
    render partial: "file_details", locals: {file: @blob, file_categories: @file_categories}
  else
    redirect_to admin_files_path
  end
end

#updateObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/controllers/panda/core/admin/files_controller.rb', line 98

def update
  blob_params = params.require(:blob).permit(:filename, :description, :file_category_id)

  # Update filename — always re-append the original extension
  new_filename = blob_params[:filename].to_s.strip
  if new_filename.present?
    original_ext = File.extname(@blob.filename.to_s)
    new_filename += original_ext if File.extname(new_filename).blank? && original_ext.present?
    @blob.filename = new_filename
  end

  # Update description in metadata
   = @blob. || {}
  ["description"] = blob_params[:description].to_s.strip
  @blob. = 

  @blob.save!
  update_file_category(blob_params[:file_category_id])

  @file_categories = Panda::Core::FileCategory.ordered
  locals = {file: @blob, file_categories: @file_categories, notice: "File updated successfully."}
  respond_to do |format|
    format.turbo_stream do
      render turbo_stream: turbo_stream.update(
        "file-gallery-slideover-content",
        partial: "file_details",
        locals: locals
      )
    end
    format.html do
      redirect_to admin_files_path, notice: "File updated successfully.", status: :see_other
    end
  end
end