-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheroes.coffee
127 lines (104 loc) · 3.38 KB
/
heroes.coffee
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
fs = require 'fs'
path = require 'path'
async = require 'async'
gm = require 'gm'
SOURCE = 'source'
OUTPUT = 'output'
QUALITY = 60
RETINA_QUALITY = 40
HOTELS_SUFFIX = '01'
TRAVEL_GUIDE_SUFFIX = '03'
FLIGHTS_SUFFIX = '07'
processImage = (imagePath, callback) ->
originalName = path.basename(imagePath, '.jpg').split('_')[0]
hotels = (done) ->
console.log "#{imagePath}: Hotels"
imageName = "#{originalName}.jpg"
copyImage imagePath, imageName, (err) ->
done(null)
travelGuide = (done) ->
console.log "#{imagePath}: Travel Guide"
imageName = "travel-guide-#{originalName}.jpg"
copyImage imagePath, imageName, (err) ->
done(null)
desktopRetina = (done) ->
console.log "#{imagePath}: Flights desktopRetina"
imageName = "#{originalName}-desktopRetina.jpg"
gm "#{SOURCE}/#{imagePath}"
.quality(RETINA_QUALITY)
.write "#{OUTPUT}/#{imageName}", (err) ->
done(null)
desktop = (done) ->
console.log "#{imagePath}: Flights desktop"
imageName = "#{originalName}-desktop.jpg"
gm "#{SOURCE}/#{imagePath}"
.resize(1320, 264)
.quality(QUALITY)
.write "#{OUTPUT}/#{imageName}", (err) ->
done(null)
tabletRetina = (done) ->
console.log "#{imagePath}: Flights tabletRetina"
imageName = "#{originalName}-tabletRetina.jpg"
gm "#{SOURCE}/#{imagePath}"
.resize(1918, 384)
.quality(RETINA_QUALITY)
.write "#{OUTPUT}/#{imageName}", (err) ->
done(null)
tablet = (done) ->
console.log "#{imagePath}: Flights tablet"
imageName = "#{originalName}-tablet.jpg"
gm "#{SOURCE}/#{imagePath}"
.resize(959, 192)
.quality(QUALITY)
.write "#{OUTPUT}/#{imageName}", (err) ->
done(null)
smallTabletRetina = (done) ->
console.log "#{imagePath}: Flights smallTabletRetina"
imageName = "#{originalName}-smallTabletRetina.jpg"
gm "#{SOURCE}/#{imagePath}"
.resize(1450, 290)
.quality(RETINA_QUALITY)
.write "#{OUTPUT}/#{imageName}", (err) ->
done(null)
smallTablet = (done) ->
console.log "#{imagePath}: Flights smallTablet"
imageName = "#{originalName}-smallTablet.jpg"
gm "#{SOURCE}/#{imagePath}"
.resize(725, 145)
.quality(QUALITY)
.write "#{OUTPUT}/#{imageName}", (err) ->
done(null)
tasks = null
suffix = path.basename(imagePath, '.jpg').split('_').pop()
switch suffix
when HOTELS_SUFFIX
tasks = [hotels]
when TRAVEL_GUIDE_SUFFIX
tasks = [travelGuide]
when FLIGHTS_SUFFIX
tasks = [desktopRetina, desktop, tabletRetina, tablet, smallTabletRetina, smallTablet]
async.series tasks, (err) ->
callback(err)
copyImage = (imagePath, imageName, callback) ->
readStream = fs.createReadStream "#{SOURCE}/#{imagePath}"
.on 'error', (err) ->
console.log 'error opening file', err
writeStream = fs.createWriteStream "#{OUTPUT}/#{imageName}"
.on 'finish', (err) ->
callback(null)
readStream.pipe writeStream
fs.readdir SOURCE, (err, files) ->
if err
console.log err
return process.exit()
# exclude any non-image files and hidden files
images = (item for item in files when path.extname(item) is '.jpg')
async.eachSeries images, (imagepath, callback) ->
processImage imagepath, (err) ->
callback(null)
, (err) ->
if err
console.log 'error'
else
console.log 'success'
process.exit()