Class: ModelFilesController

Inherits:
ApplicationController show all
Includes:
ActionController::Live
Defined in:
app/controllers/model_files_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#index

Instance Method Details

#bulk_edit ⇒ Object



105
106
107
108
# File 'app/controllers/model_files_controller.rb', line 105

def bulk_edit
  authorize @model, :edit?
  @files = policy_scope(@model.model_files.without_special)
end

#bulk_update ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/controllers/model_files_controller.rb', line 110

def bulk_update
  authorize @model, :edit?
  hash = bulk_update_params
  ids_to_update = params[:model_files].keep_if { |key, value| value == "1" }.keys
  files = policy_scope(@model.model_files.without_special).where(public_id: ids_to_update)
  files.each do |file|
    ActiveRecord::Base.transaction do
      current_user.set_list_state(file, :printed, params[:printed] === "1")
      options = {}
      if params[:pattern].present?
        options[:filename] =
          file.filename.split(file.extension).first.gsub(params[:pattern], params[:replacement]) +
          file.extension
      end
      file.update(hash.merge(options))
    end
  end
  if params[:split]
    new_model = @model.split! files: files
    redirect_to model_path(new_model), notice: t(".success")
  else
    redirect_back_or_to model_path(@model), notice: t(".success")
  end
end

#configure_content_security_policy ⇒ Object



13
14
15
16
# File 'app/controllers/model_files_controller.rb', line 13

def configure_content_security_policy
  # If embed mode, allow any frame ancestor
  content_security_policy.frame_ancestors [:https, :http] if embedded?
end

#create ⇒ Object



54
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
# File 'app/controllers/model_files_controller.rb', line 54

def create
  authorize @model, :edit?
  if params[:convert]
    file = ModelFile.find_param(params[:convert][:id])
    authorize(file, :show?)
    file.convert_later params[:convert][:to]
    redirect_back_or_to [@model, file], notice: t(".conversion_started")
  elsif !(p = upload_params).empty?
    p.dig(:model, :file).each_pair do |_id, file|
      ProcessUploadedFileJob.perform_later(
        @model.library.id,
        {
          id: file[:id],
          storage: "cache",
          metadata: {
            filename: Zaru.sanitize!(File.basename(file[:name]))
          }
        },
        model: @model
      )
    end
    respond_to do |format|
      format.html { redirect_to @model, notice: t(".success") }
      format.manyfold_api_v0 { head :accepted }
    end
  else
    head :unprocessable_content
  end
end

#destroy ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'app/controllers/model_files_controller.rb', line 135

def destroy
  authorize @file
  @file.delete_from_disk_and_destroy
  respond_to do |format|
    format.html do
      if request.referer && (URI.parse(request.referer).path == model_model_file_path(@model, @file))
        # If we're coming from the file page itself, we can't go back there
        redirect_to model_path(@model), notice: t(".success")
      else
        redirect_back_or_to model_path(@model), notice: t(".success")
      end
    end
    format.manyfold_api_v0 { head :no_content }
  end
end

#raw ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/model_files_controller.rb', line 41

def raw
  @file = @model.model_files.find_by!(
    filename: [
      params[:filename],
      [params[:filename], params[:format]].join(".")
    ]
  )
  authorize @file
  request.format = params[:format].downcase
  respond_to @file.mime_type.to_sym
  send_file_content @file.attachment, disposition: (params[:download] == "true") ? :attachment : :inline
end

#show ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/model_files_controller.rb', line 18

def show
  if embedded?
    respond_to do |format|
      format.html { render "embedded", layout: "embed" }
    end
  elsif stale?(@file)
    @duplicates = @file.duplicates
    @related = Naturally.sort([@file, @file.related_files, @file.files_related_to_me].flatten.uniq, by: :name_and_filename) if @file.relationships.any? || @file.reverse_relationships.any?
    respond_to do |format|
      format.html
      format.manyfold_api_v0 { render json: ManyfoldApi::V0::ModelFileSerializer.new(@file).serialize }
      format.any(*MediaType.indexable_types.map(&:to_sym)) do
        attachment = @file.attachment(params[:derivative]) || @file.attachment
        send_file_content attachment, derivative: params[:derivative], disposition: (params[:download] == "true") ? :attachment : :inline
      end
    end
  end
  # i18n-tasks-use t("activerecord.attributes.model_file.filename")
  # i18n-tasks-use t("activerecord.attributes.model_file.digest")
  # i18n-tasks-use t("activerecord.attributes.model_file.size")
  # i18n-tasks-use t("activerecord.attributes.model_file.dimensions")
end

#update ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/controllers/model_files_controller.rb', line 84

def update
  result = @file.update(file_params)
  respond_to do |format|
    format.html do
      if result
        current_user.set_list_state(@file, :printed, params[:model_file][:printed] === "1")
        redirect_to [@model, @file], notice: t(".success")
      else
        render :edit, alert: t(".failure"), status: :unprocessable_content
      end
    end
    format.manyfold_api_v0 do
      if result
        render json: ManyfoldApi::V0::ModelFileSerializer.new(@file).serialize
      else
        render json: @file.errors.to_json, status: :unprocessable_content
      end
    end
  end
end