-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
images_controller.rb
72 lines (67 loc) · 2.39 KB
/
images_controller.rb
1
2
3
4
5
6
7
8
9
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
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
require 'open-uri'
class ImagesController < ApplicationController
respond_to :html, :xml, :json
before_action :require_user, only: %i(create new update delete)
def shortlink
params[:size] = params[:size] || params[:s]
params[:size] = params[:size] || :large
params[:size] = :thumb if (params[:size].to_s == "t")
params[:size] = :thumb if (params[:size].to_s == "thumbnail")
params[:size] = :medium if (params[:size].to_s == "m")
params[:size] = :large if (params[:size].to_s == "l")
params[:size] = :original if (params[:size].to_s == "o")
image = Image.find(params[:id])
redirect_to URI.parse(image.path(params[:size])).path
end
def create
if params[:i]
@image = Image.new(remote_url: params[:i],
uid: current_user.uid)
flash[:error] = 'The image could not be saved.' unless @image.save!
elsif params[:data]
filetype = params[:data].split(';').first.split('/').last
@image = Image.new(uid: current_user.uid,
photo: params[:data],
photo_file_name: 'dataurl.' + filetype)
@image.save!
else
@image = Image.new(uid: current_user.uid,
photo: params[:image][:photo],
title: params[:image][:title],
notes: params[:image][:notes])
end
@image.nid = Node.find(params[:nid].to_i).nid unless params[:nid].nil? || params[:nid] == 'undefined' || params[:nid].to_i == 0
if @image.save!
render json: {
id: @image.id,
url: @image.shortlink,
full: 'https://' + request.host.to_s + '/' + @image.path(:large),
filename: @image.photo_file_name,
href: @image.shortlink, # Woofmark/PublicLab.Editor
title: @image.photo_file_name,
results: [{ # Woofmark/PublicLab.Editor
href: @image.shortlink + "." + @image.filetype,
title: @image.photo_file_name
}]
}
else
render plain: 'The image could not be saved.'
end
end
def new
@image = Image.new
end
def delete
@image = Image.find params[:id]
if @image.uid == current_user.uid
if @image.delete
flash[:notice] = 'Image deleted.'
else
flash[:error] = 'The image could not be deleted.'
end
redirect_to '/post'
else
prompt_login 'Only the owner can delete this image.'
end
end
end