|
1 |
| -import express from 'express'; |
| 1 | +import 'dotenv/config'; |
| 2 | +import express, {Request, Response} from 'express'; |
2 | 3 | import fs from 'fs';
|
3 |
| -import { getDb } from '../db/mongo'; |
| 4 | +import {ObjectId} from 'mongodb'; |
| 5 | +import {getDb} from '../db/mongo.js'; |
4 | 6 |
|
| 7 | +/* eslint new-cap: ["error", { "capIsNewExceptions": ["Router"] }] */ |
5 | 8 | const videos = express.Router();
|
6 | 9 |
|
7 |
| -videos.get('/', function (_req, res) { |
| 10 | +videos.get('/', function(req: Request, res: Response) { |
| 11 | + let page = 1; |
| 12 | + const rpp = 12; |
8 | 13 | const db = getDb();
|
9 | 14 |
|
| 15 | + if (req.query.page !== undefined) { |
| 16 | + page = parseInt(req.query.page as string); |
| 17 | + } |
| 18 | + |
| 19 | + const skip = (page - 1) * rpp; |
| 20 | + |
10 | 21 | db.collection('videos')
|
11 |
| - .find({}) |
12 |
| - .toArray(function (err: any, result: any) { |
13 |
| - if (err) { |
14 |
| - res.status(400).send('Error fetching listings!'); |
15 |
| - } else { |
16 |
| - res.json({items: result}); |
17 |
| - } |
18 |
| - }); |
| 22 | + .aggregate([ |
| 23 | + { |
| 24 | + $facet: { |
| 25 | + items: [ |
| 26 | + {$match: {status: 2}}, |
| 27 | + {$sort: {ts: -1}}, |
| 28 | + {$skip: skip}, |
| 29 | + {$limit: rpp}, |
| 30 | + ], |
| 31 | + total: [{$count: 'count'}], |
| 32 | + }, |
| 33 | + }, |
| 34 | + ]) |
| 35 | + .toArray(function(err: any, result: any) { |
| 36 | + if (err) { |
| 37 | + res.status(400).send('Error fetching listings!'); |
| 38 | + } else { |
| 39 | + res.json(result); |
| 40 | + } |
| 41 | + }); |
19 | 42 | });
|
20 | 43 |
|
21 |
| -videos.get('/thumb/:videoId', function (req, res) { |
| 44 | +videos.get('/thumb/:videoId', function(req: Request, res: Response) { |
22 | 45 | const videoId = req.params.videoId;
|
23 |
| - let filename = videoId; |
| 46 | + const db = getDb(); |
24 | 47 |
|
25 |
| - var img = fs.readFileSync('store/videos/thumbs/' + filename); |
26 |
| - res.writeHead(200, { 'Content-Type': 'image/png' }); |
27 |
| - res.end(img, 'binary'); |
| 48 | + db.collection('videos') |
| 49 | + .findOne({_id: new ObjectId(videoId)}) |
| 50 | + .then((result: any) => { |
| 51 | + const img = fs.readFileSync( |
| 52 | + `${process.env.STORAGE}/videos${result.path}/thumbs/${videoId}`, |
| 53 | + ); |
| 54 | + res.writeHead(200, {'Content-Type': 'image/png'}); |
| 55 | + res.end(img, 'binary'); |
| 56 | + }); |
28 | 57 | });
|
29 | 58 |
|
30 | 59 | export default videos;
|
0 commit comments