-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
108 lines (86 loc) · 2.48 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
'use strict'
var TelegramBot = require('node-telegram-bot-api')
var express = require('express')
var request = require('request')
var token = require('./lib/token')
var model = require('./lib/item')
var config = require('./config.json')
var bot = new TelegramBot(config.token, config.options)
bot.on('message', msg => {
console.log(msg)
if (msg.text === '/start') {
return bot.sendMessage(msg.from.id, 'Send me any file or picture and I will host it online!')
}
var file = msg.document
var filetype = file && file.file_type
if (file && !filetype) {
var fileNameParts = file.file_name.split('.')
if (fileNameParts.length > 1) {
filetype = fileNameParts.pop()
}
}
if (!file && msg.photo) {
file = msg.photo[msg.photo.length - 1]
}
if (!file && msg.audio) {
file = msg.audio
filetype = 'mp3'
}
if (!file && msg.voice) {
file = msg.voice
filetype = 'ogg'
}
if (!file && msg.sticker) {
file = msg.sticker
filetype = 'webp'
}
if (!file) {
return bot.sendMessage(msg.from.id, 'Just send me anything, dude.')
}
var item = new model({
id: token(),
author: [msg.from.first_name, msg.from.last_name].join(' '),
message: msg,
file_id: file.file_id,
file_type: filetype || 'jpg',
date: msg.date
})
item.save().then((item) => {
bot.sendMessage(msg.from.id, [
config.url,
[
item.id,
item.file_type
].join('.')
].join('/'))
})
})
var app = express()
app.use(express.static(require('path').join(__dirname, 'static')))
app.get('/:id.:type', (req, res, next) => {
if (!req.params || !req.params.id) {
return
}
model.findOne({
id: req.params.id
}).then(item => {
bot.getFileLink(item.file_id).then(link => request(link).on('response', function(resp) {
delete resp.headers['content-disposition'];
var type = item.file_type && item.file_type.toLowerCase()
if (['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp'].indexOf(type) > -1) {
resp.headers['content-type'] = ['image', type].join('/')
}
if (type === 'html') {
resp.headers['content-type'] = ['text', 'html'].join('/')
}
if (type === 'js') {
resp.headers['content-type'] = ['text', 'javascript'].join('/')
}
if (type === 'css') {
resp.headers['content-type'] = ['text', 'css'].join('/')
}
}).pipe(res)).catch(next)
}).catch(next)
})
app.listen(3000)
console.log('Server launched at', new Date())