-
Notifications
You must be signed in to change notification settings - Fork 0
/
pictor.rb
executable file
·272 lines (222 loc) · 5.92 KB
/
pictor.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/usr/bin/ruby
require 'erb'
require 'cgi'
require 'ostruct'
require 'json'
require 'pathname'
# TEMPLATES
PAGE_TEMPLATE = <<-HTML
<!DOCTYPE html>
<html>
<head>
<title>Pictor</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript">
'use strict';
var Gallery = function(gallery, navigator) {
var
files,
directory = [];
var _update = function() {
var images = [];
var subdirectories = [];
for (var i=0; i<files.length; i++) {
var file = files[i];
// file is not in the current path
if (file.slice(0, directory.length).join('/') != directory.join('/')) { continue; }
// file is in the current directory
if (file.length == directory.length + 1) { images.push(file.join('/')); }
// file is in a subdirectory
if (file.length == directory.length + 2) {
var subdirectory = file[directory.length];
if (subdirectories.indexOf(subdirectory) == -1) { subdirectories.push(subdirectory); }
}
}
_loadImages(images);
_loadNavigator(subdirectories);
};
var _loadImages = function(files) {
gallery.innerHTML = '';
files.forEach(function(file) {
var div = document.createElement('div');
var a = document.createElement('a');
a.href = file;
a.target = "_blank";
var img = document.createElement('img');
img.src = file;
a.appendChild(img)
div.appendChild(a);
gallery.appendChild(div);
});
};
var _loadNavigator = function(subdirectories) {
navigator.innerHTML = '';
if (directory.length > 0) {
var span = document.createElement('span');
span.textContent = 'up';
span.onclick = function() {
_up();
};
navigator.appendChild(span);
navigator.appendChild(document.createElement('hr'));
}
subdirectories.forEach(function(directory) {
var span = document.createElement('span');
span.textContent = directory;
span.onclick = function() {
_enter(directory);
};
navigator.appendChild(span);
});
};
var _enter = function(subdirectory) {
directory.push(subdirectory)
_update();
};
var _up = function() {
directory.pop();
_update();
};
return {
load: function(data) {
files = data;
directory = [];
_update();
}
};
};
window.onload = function() {
var container = document.getElementById('gallery');
var navigator = document.getElementById('navigator');
var gallery = Gallery(container, navigator);
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState != 4) { return; }
if (xhr.status == 200) { gallery.load(JSON.parse(xhr.responseText)); }
else { console.log('request fail', xhr.status, xhr.responseText); }
};
xhr.open("GET", window.location.href + '?action=list', true);
xhr.send();
}
</script>
<style type="text/css">
#pictures div {
width: 400px;
margin: 10px;
}
img {
padding: 10px;
height: auto;
max-width: 380px;
border: 1px solid gray;
}
#navigator {
position: fixed;
top: 50px;
left: 500px;
border: 1px solid gray;
padding: 0 5px 0 5px;
}
#navigator span {
padding: 5px;
display: block;
text-decoration: none;
color: gray;
}
#navigator span:hover {
color: red;
cursor: pointer;
}
</style>
</head>
<body>
<div id="gallery">
</div>
<div id="navigator">
</div>
</body>
</html>
HTML
# Lib
class ImageMagick
def initialize(src, out)
@src = src
@out = out
@operations = []
end
def <<(operation)
@operations << operation
end
def run
system(cmdline)
$? == 0
end
def self.found?
`which convert`
$? == 0
end
private
def cmdline
operations = @operations.join(' ')
"convert #{operations} #{Shellwords.escape(@src)} #{Shellwords.escape(@out)}"
end
end
class Image
attr_reader :path
def initialize(path)
@path = path
end
def thumbnail
return unless Configuration.create_thumbnails && ImageMagick.found?
@thumbnail ||= Thumbnail.create(@path)
end
def optimal_path
@thumbnail ? @thumbnail.path : @path
end
end
class Thumbnail
attr_reader :path
def initialize(path)
@path = path
end
def self.create(path)
dir = "#{File.dirname(path)}/.pictor"
filename = File.basename(path)
Dir.mkdir(dir) unless File.directory?(dir)
thumbnail_path = "#{dir}/#{filename}"
# the thumbnail exists and is up to date, no need for conversion
if File.exists?(thumbnail_path) && File.ctime(path) <= File.ctime(thumbnail_path)
return new(thumbnail_path)
end
convert = ImageMagick.new(path, thumbnail_path)
convert << '-scale 380'
success = convert.run
raise "converting picture failed" unless success
new(thumbnail_path)
end
end
class ImageLister
def images
Dir['**/*.{jpg,gif,png}'].map { |path| Pathname(path).each_filename.to_a }
end
end
class App
def initialize(cgi)
@cgi = cgi
end
def index
@cgi.out { PAGE_TEMPLATE }
end
def list
list = ImageLister.new.images.to_json
@cgi.out('application/json') { list }
end
end
# CONFIG
Configuration = OpenStruct.new(
create_thumbnails: false
)
# MAIN
cgi = CGI.new
action = cgi.params['action'].first || 'index'
App.new(cgi).send(action)