forked from cozy/cozy-contacts-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.coffee
50 lines (45 loc) · 1.55 KB
/
init.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
Photo = require './server/models/photo'
File = require './server/models/file'
thumb = require('./server/helpers/thumb').create
async = require 'async'
onThumbCreation = false
percent = null
total_files = 0
thumb_files = 0
module.exports.onThumbCreation = () ->
return [thumb_files isnt total_files, (thumb_files / total_files) * 100]
convertImage = (cb) ->
convert = (doc, callback) ->
if doc._attachments?
try
console.log "Convert #{doc.title} ..."
doc.convertBinary (err, res, body) ->
console.log err if err?
callback err
catch error
console.log "Cannot convert #{doc.title}"
callback()
else
callback()
Photo.all (err, docs) ->
async.eachSeries(docs, convert, cb)
createThumb = (socket, cb) ->
# Recover all file without thumb
File.withoutThumb (err, files) ->
total_files = files.length
# Create thumb and check progress
async.eachSeries files, (file, callback) ->
thumb file, () ->
thumb_files += 1
percent = Math.floor((thumb_files / total_files) * 100)
# Emit thumb creation progress
socket.emit 'progress', {"percent": percent}
callback()
, cb
# Create all requests and upload directory
module.exports.convert = (socket, done=->null) ->
#convertImage (err) ->
onThumbCreation = true
createThumb socket, ->
onThumbCreation = false
done()