-
Notifications
You must be signed in to change notification settings - Fork 0
/
thumb code.rb
184 lines (151 loc) · 5.49 KB
/
thumb code.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
require 'RMagick'
include Magick
def index
walk_tree((File::join Rails.root, "cache/"))
getCategoryTreeView()
end
def viewDirectoy()
@list = []
directory = params[:directory]
Dir.foreach(directory) {|x|
if x == '.' or x == '..' or x == 'thumb' or x == 'display'
next
elsif Dir.exist?((File::join directory, x))
temp = 0
else
@list << (File::join directory,'display', x)
end
}
end
def precompilelist()
@precompilestrList = "Rails.application.config.assets.precompile += [" + (File::join Rails.root, "cache/") + "]"
end
def walk_tree_precompile(directory)
structureFileList = {}
i = 1
Dir.foreach(directory) {|x|
if x == '.' or x == '..' or x == 'thumb' or x == 'display'
next
elsif Dir.exist?((File::join directory, x))
list += walk_tree((File::join directory, x))
else
list += "'" + (File::join directory, x).to_s + "',\r\n"
end
i += 1
}
return list
end
def walk_tree(directory)
list = []
structureFileList = {}
Dir.foreach(directory) {|x|
if x == '.' or x == '..' or x == 'thumb' or x == 'display'
next
elsif Dir.exist?((File::join directory, x))
catalog((File::join directory, x), "thumb", 150)
catalog((File::join directory, x), "display", 2048)
list << { :children => walk_tree((File::join directory, x)), :name =>(File::join directory, x).to_s }
else
list << x
end
}
structureFileList[directory] = list
return structureFileList
end
def getCategoryTreeView()
data = []
data[0] = {:html => "<span>Category</span>", :type => 'html',
:labelElId => "CategoryMenuLabel", :expanded => true,
:children => walkTreeView((File::join Rails.root, "cache/"), 0, {})
}
@CategoryTree = data
end
def walkTreeView(directory, level, nodeList)
itor = 0
data = []
listDirectoyBaseUrl = '/main_page/viewDirectoy'
listDirectoyParamSet = {:instanceObj => 'args.mv',
:treeviewId => 'treeview_placeHolder',
:mainViewId => 'DirectoyPanel_mainView',
:loadingImageId => 'loading',
:loadingReadyClassName => 'ready',
:loadingloadingClassName => 'loading',
:baseUrl => listDirectoyBaseUrl}
Dir.foreach(directory) {|x|
if x == '.' or x == '..' or x == 'thumb' or x == 'display'
next
elsif Dir.exist?((File::join directory, x))
html_key = level.to_s + directory + "_" + (File::join directory, x).to_s
data[itor] = yuiTreeviewItem(x, html_key, "new mainViewClass().tvListDirectoy",
listDirectoyParamSet.merge({:id => (File::join directory, x), :name => x }),
false)
nodeList[ directory + "_" + (File::join directory, x).to_s] = level
data[itor][:children] = walkTreeView(Dir.exist?((File::join directory, x), level + 1, nodeList)
end
itor += 1
}
end
return data
end
def yuiTreeviewItem(label, idSrufix, func = nil, paramObject = nil, checked = nil, children = nil, url = nil, target = nil)
item = {'type' => 'HTML'}
idSur = idSrufix.gsub('(\&| )','')
paramJson = paramObject.to_json.html_safe.gsub("\"",'\'')
if !label
label = ""
end
if url
item[:html] = '<a class="tvLink" id="treeViewItem'+idSur + '"'
item[:html] += ' href="'+url + '"'
if target
item[:html] += ' target="'+target + '"'
end
item[:html] += '>'+ label + '</a>'
else
item[:html] = '<span id="treeViewItem'+idSur + '"'
if func
item[:html] += ' param="'+ paramJson+'"'
end
item[:html] += '>'+ label + '</span>'
end
item[:checked] = true if checked
if children
item[:children] = children
end
return item
end
def catalog(directory, directoryName, sizeConst)
Dir.foreach(directory) {|x|
if x == '.' or x == '..'
next
end
file_name = (File::join directory, x)
resultfile = (File::join directory, directoryName, x)
if !Dir.exist?((File::join directory, directoryName))
Dir.mkdir((File::join directory, directoryName))
end
if !File.exist?(resultfile)
begin
img = Image.read(file_name).first
if img.columns > sizeConst or img.rows > sizeConst
if img.columns > img.rows
ratio = img.columns / img.rows
width = ratio * sizeConst
height = sizeConst
else
ratio = img.rows / img.columns
height = ratio * sizeConst
width = sizeConst
end
thumbnail = img.thumbnail(width,height)
thumbnail.write(resultfile)
else
thumbnail = img.thumbnail( img.columns , img.rows)
thumbnail.write(resultfile)
end
rescue
end
end
}
render :text => "saved"
end