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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
const config = require('../config.js')
const db = require('knex')(config.database)
const randomstring = require('randomstring')
const path = require('path')
const fs = require('fs')
const ffmpeg = require('fluent-ffmpeg')
const gm = require('gm')
let albumsController = {}
albumsController.list = function(req, res, next){
let token = req.headers.token
if(token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
db.table('users').where('token', token).then((user) => {
if(user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token'})
let fields = ['id', 'name']
if(req.params.sidebar === undefined){
fields.push('timestamp')
fields.push('identifier')
}
db.table('albums').select(fields).where({enabled: 1, userid: user[0].id}).then((albums) => {
if(req.params.sidebar !== undefined)
return res.json({ success: true, albums })
let ids = []
for(let album of albums){
album.date = new Date(album.timestamp * 1000)
album.date = album.date.getFullYear() + '-' + (album.date.getMonth() + 1) + '-' + album.date.getDate() + ' ' + (album.date.getHours() < 10 ? '0' : '') + album.date.getHours() + ':' + (album.date.getMinutes() < 10 ? '0' : '') + album.date.getMinutes() + ':' + (album.date.getSeconds() < 10 ? '0' : '') + album.date.getSeconds()
let basedomain = req.get('host')
for(let domain of config.domains)
if(domain.host === req.get('host'))
if(domain.hasOwnProperty('resolve'))
basedomain = domain.resolve
album.identifier = basedomain + '/a/' + album.identifier
ids.push(album.id)
}
db.table('files').whereIn('albumid', ids).select('albumid').then((files) => {
let albumsCount = {}
for(let id of ids) albumsCount[id] = 0
for(let file of files) albumsCount[file.albumid] += 1
for(let album of albums) album.files = albumsCount[album.id]
return res.json({ success: true, albums })
}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
}
albumsController.create = function(req, res, next){
let token = req.headers.token
if(token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
db.table('users').where('token', token).then((user) => {
if(user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token'})
let name = req.body.name
if(name === undefined || name === '')
return res.json({ success: false, description: 'No album name specified' })
db.table('albums').where({
name: name,
enabled: 1,
userid: user[0].id
}).then((album) => {
if(album.length !== 0) return res.json({ success: false, description: 'There\'s already an album with that name' })
db.table('albums').insert({
name: name,
enabled: 1,
userid: user[0].id,
identifier: randomstring.generate(8),
timestamp: Math.floor(Date.now() / 1000)
}).then(() => {
return res.json({ success: true })
})
}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
}
albumsController.delete = function(req, res, next){
let token = req.headers.token
if(token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
db.table('users').where('token', token).then((user) => {
if(user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token'})
let id = req.body.id
if(id === undefined || id === '')
return res.json({ success: false, description: 'No album specified' })
db.table('albums').where({id: id, userid: user[0].id}).update({ enabled: 0 }).then(() => {
return res.json({ success: true })
}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
}
albumsController.rename = function(req, res, next){
let token = req.headers.token
if(token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
db.table('users').where('token', token).then((user) => {
if(user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token'})
let id = req.body.id
if(id === undefined || id === '')
return res.json({ success: false, description: 'No album specified' })
let name = req.body.name
if(name === undefined || name === '')
return res.json({ success: false, description: 'No name specified' })
db.table('albums').where({name: name, userid: user[0].id}).then((results) => {
if(results.length !== 0) return res.json({ success: false, description: 'Name already in use' })
db.table('albums').where({id: id, userid: user[0].id}).update({ name: name }).then(() => {
return res.json({ success: true })
}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
}
albumsController.get = function(req, res, next){
let identifier = req.params.identifier
if(identifier === undefined) return res.status(401).json({ success: false, description: 'No identifier provided' })
db.table('albums')
.where('identifier', identifier)
.then((albums) => {
if(albums.length === 0) return res.json({ success: false, description: 'Album not found' })
let title = albums[0].name
db.table('files').select('name').where('albumid', albums[0].id).orderBy('id', 'DESC').then((files) => {
let basedomain = req.get('host')
for(let domain of config.domains)
if(domain.host === req.get('host'))
if(domain.hasOwnProperty('resolve'))
basedomain = domain.resolve
for(let file of files){
file.file = basedomain + '/' + file.name
if(config.uploads.generateThumbnails === true){
let extensions = ['.jpg', '.jpeg', '.bmp', '.gif', '.png', '.webm', '.mp4']
for(let ext of extensions){
if(path.extname(file.name) === ext){
file.thumb = basedomain + '/thumbs/' + file.name.slice(0, -ext.length) + '.png'
let thumbname = path.join(__dirname, '..', config.uploads.folder, 'thumbs') + '/' + file.name.slice(0, -ext.length) + '.png'
fs.access(thumbname, function(err) {
if (err && err.code === 'ENOENT') {
// File doesnt exist
if (ext === '.webm' || ext === '.mp4') {
ffmpeg('./' + config.uploads.folder + '/' + file.name)
.thumbnail({
timestamps: [0],
filename: '%b.png',
folder: './' + config.uploads.folder + '/thumbs',
size: '200x?'
})
.on('error', function(error) {
console.log('Error - ', error.message)
})
}
else {
let size = {
width: 200,
height: 200
}
gm('./' + config.uploads.folder + '/' + file.name)
.resize(size.width, size.height + '>')
.gravity('Center')
.extent(size.width, size.height)
.background('transparent')
.write(thumbname, function (error) {
if (error) console.log('Error - ', error)
})
}
}
})
}
}
}
}
return res.json({
success: true,
title: title,
count: files.length,
files
})
}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
}
module.exports = albumsController
|