This repository has been archived by the owner on Jan 18, 2024. It is now read-only.
forked from zrrrzzt/tfk-api-unoconv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
92 lines (81 loc) · 2.44 KB
/
index.js
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
'use strict'
const fs = require('fs')
const uuid = require('uuid')
const unoconv = require('unoconv3')
const formats = require('../lib/data/formats.json')
const pkg = require('../package.json')
module.exports.handleUpload = (request, reply) => {
const convertToFormat = request.params.format
const data = request.payload
if (data.file) {
const nameArray = data.file.hapi.filename.split('.')
const fileEndingOriginal = nameArray.pop()
const temporaryName = uuid.v4()
const pathPre = process.cwd() + '/uploads/' + temporaryName
const fileNameTempOriginal = pathPre + '.' + fileEndingOriginal
const file = fs.createWriteStream(fileNameTempOriginal)
let filters = []
if (data.filter) {
filters = Array.isArray(data.filter) ? data.filter : [data.filter]
}
const options = {filters}
file.on('error', (error) => {
console.error(error)
})
data.file.pipe(file)
data.file.on('end', (err) => {
if (err) {
console.error(err)
reply(err)
} else {
unoconv.convert(fileNameTempOriginal, convertToFormat, options, (err, result) => {
if (err) {
console.error(err)
fs.unlink(fileNameTempOriginal, error => {
if (error) {
console.error(error)
} else {
console.log(`${fileNameTempOriginal} deleted`)
}
})
reply(err)
} else {
console.log('finished converting')
reply(result)
.on('finish', () => {
fs.unlink(fileNameTempOriginal, error => {
if (error) {
console.error(error)
} else {
console.log(`${fileNameTempOriginal} deleted`)
}
})
})
}
})
}
})
}
}
module.exports.showFormats = (request, reply) => {
reply(formats)
}
module.exports.showFormat = (request, reply) => {
const params = request.params
const format = params ? formats[request.params.type] : false
if (!format) {
reply('Format type not found').code(404)
} else {
reply(format)
}
}
module.exports.showVersions = (request, reply) => {
let versions = {}
Object.keys(pkg.dependencies).forEach((item) => {
versions[item] = pkg.dependencies[item]
})
reply(versions)
}
module.exports.healthcheck = (request, reply) => {
reply({uptime: process.uptime()})
}