generated from CollectionBuilder/collectionbuilder-sa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
158 lines (140 loc) · 5.36 KB
/
Rakefile
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
# Tasks for creating derivatives for spec-lumber blog, modified from collectionbuilder-sa
###############################################################################
# TASK: deploy
###############################################################################
desc "Build site with production env"
task :deploy do
ENV["JEKYLL_ENV"] = "production"
sh "bundle exec jekyll build"
end
###############################################################################
# Helper Functions
###############################################################################
$ensure_dir_exists = ->(dir) { if !Dir.exists?(dir) then Dir.mkdir(dir) end }
def prompt_user_for_confirmation message
response = nil
while true do
# Use print instead of puts to avoid trailing \n.
print "#{message} (Y/n): "
$stdout.flush
response =
case STDIN.gets.chomp.downcase
when "", "y"
true
when "n"
false
else
nil
end
if response != nil
return response
end
puts "Please enter \"y\" or \"n\""
end
end
###############################################################################
# TASK: generate_derivatives
#
# this task assumes you put scanned objects in objects/archives folder
# newly added objects are then processed (filenames downcased, tifs to jpgs), copied to objects/
# and generates small and thumb image for image and pdf objects
#
###############################################################################
desc "Generate derivative image files from collection objects"
task :generate_derivatives, [:thumbs_size, :small_size, :density, :missing, :im_executable] do |t, args|
args.with_defaults(
:thumbs_size => "300x300",
:small_size => "800x800",
:density => "300",
:missing => "true",
:im_executable => "magick"
#:im_executable => "convert"
)
# set the various directories to be used
archives_dir = "objects/archives"
objects_dir = "objects"
thumb_image_dir = "objects/thumbs"
small_image_dir = "objects/small"
# Ensure that the output directories exist.
[thumb_image_dir, small_image_dir].each &$ensure_dir_exists
# support these file types
EXTNAME_TYPE_MAP = {
'.tiff' => :image,
'.tif' => :image,
'.jpg' => :image,
'.png' => :image,
'.pdf' => :pdf
}
# Generate derivatives.
Dir.glob(File.join([archives_dir, '*'])).each do |filename|
# Ignore subdirectories.
if File.directory? filename
next
end
# Ignore readme.
if filename == File.join([archives_dir, 'README.md'])
next
end
# Get the lowercase filename without any leading path and extension.
extname = File.extname(filename).downcase
base_filename = File.basename(filename)[0..-(extname.length + 1)].downcase
# Determine the file type and skip if unsupported.
file_type = EXTNAME_TYPE_MAP[extname]
if !file_type
access_filename=File.join([objects_dir, "#{base_filename}#{extname}"])
puts "Skipping conversion of file with unsupported extension: #{extname}."
if args.missing == 'false' or !File.exists?(access_filename)
puts "Copying #{filename} to objects dir for access as #{access_filename}."
system("cp #{filename} #{access_filename}")
end
next
end
# Define the file-type-specific ImageMagick command prefix.
cmd_prefix =
case file_type
when :image then "#{args.im_executable} #{filename}"
when :pdf then "#{args.im_executable} -density #{args.density} #{filename}[0]"
end
# Generate or copy the access image.
if extname == ".tif" || extname == ".tiff"
access_filename=File.join([objects_dir, "#{base_filename}.jpg"])
if args.missing == 'false' or !File.exists?(access_filename)
puts "Creating #{access_filename}"
system("#{cmd_prefix} -flatten #{access_filename}")
else
puts "Skipping existing file"
end
elsif extname == ".pdf"
access_imagefilename=File.join([objects_dir, "#{base_filename}.jpg"])
access_filename=File.join([objects_dir, "#{base_filename}#{extname}"])
if args.missing == 'false' or !File.exists?(access_filename)
puts "Creating #{access_imagefilename}"
system("#{cmd_prefix} -flatten #{access_imagefilename}")
puts "Copying #{filename} to objects dir for access as #{access_filename}."
system("cp #{filename} #{access_filename}")
else
puts "Skipping existing file"
end
else
access_filename=File.join([objects_dir, "#{base_filename}#{extname}"])
if args.missing == 'false' or !File.exists?(access_filename)
puts "Copying #{filename} to objects dir for access as #{access_filename}."
system("cp #{filename} #{access_filename}")
else
puts "Skipping existing file"
end
end
# Generate the thumb image.
thumb_filename=File.join([thumb_image_dir, "#{base_filename}_th.jpg"])
if args.missing == 'false' or !File.exists?(thumb_filename)
puts "Creating: #{thumb_filename}";
system("#{cmd_prefix} -resize #{args.thumbs_size} -flatten #{thumb_filename}")
end
# Generate the small image.
small_filename = File.join([small_image_dir, "#{base_filename}_sm.jpg"])
if args.missing == 'false' or !File.exists?(small_filename)
puts "Creating: #{small_filename}";
system("#{cmd_prefix} -resize #{args.small_size} -flatten #{small_filename}")
end
end
end