Class: Mosaico::ImagesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/mosaico/images_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/mosaico/images_controller.rb', line 7

def create
  files = params[:files].map do |file|
    dest_file = File.basename(file.tempfile.path)
    UploadedImage.backend.store(file.tempfile.path, as: dest_file)
    image = MiniMagick::Image.new(file.tempfile.path)

    record = UploadedImage.create!(
      file: dest_file,
      width: image[:width],
      height: image[:height],
      filesize: File.size(file.path),
      mime_type: MIME::Types.type_for(image.path).first.to_s
    )

    record.to_json
  end

  render json: { files: files }
end

#destroyObject



85
86
87
88
# File 'app/controllers/mosaico/images_controller.rb', line 85

def destroy
  UploadedImage.find(id_from_params).destroy
  head :ok
end

#showObject



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
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
# File 'app/controllers/mosaico/images_controller.rb', line 27

def show
  case params[:method]
    when 'placeholder'
      placeholder = find_or_create_placeholder(*params[:params].split(','))
      send_image(placeholder)

    when 'resize', 'cover'
      original_image = UploadedImage.find(id_from_params)
      width, height = params[:params].split(',')
      width = original_image.width if width == 'null'
      height = original_image.height if height == 'null'
      width = width.to_i
      height = height.to_i

      image = UploadedImage.where(parent_id: id_from_params, width: width, height: height).first
      send_image(image) and return if image

      image_path = UploadedImage.backend.retrieve(original_image.file)
      resized_image = MiniMagick::Image.open(image_path)

      if params[:method] == 'resize'
        resized_image.combine_options do |c|
          c.resize("#{width}x#{height}")
          c.antialias
        end
      else
        resized_image.combine_options do |c|
          c.resize("#{width}x#{height}^")
          c.gravity(:center)
          c.extent("#{width}x#{height}>")
          c.antialias
        end
      end

      dest_file = File.basename(resized_image.path)
      UploadedImage.backend.store(resized_image.path, as: dest_file)

      resized_record = UploadedImage.create!(
        file: dest_file,
        width: width,
        height: height,
        filesize: File.size(resized_image.path),
        mime_type: MIME::Types.type_for(resized_image.path).first.to_s,
        parent_id: original_image.id
      )

      send_image(resized_record)

    else
      if id_from_params
        image = UploadedImage.find(id_from_params)
        send_image(image)
      else
        render json: { files: UploadedImage.where(parent_id: nil).map(&:to_json) }
      end
  end
end