-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.js
173 lines (158 loc) · 5.14 KB
/
upload.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
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
require('dotenv').config();
const express = require('express');
const { v4: uuidv4 } = require('uuid');
const path = require('path');
const cors = require('cors');
const multer = require('multer');
const { CloudinaryStorage } = require('multer-storage-cloudinary');
const cloudinary = require('cloudinary').v2;
const knex = require('knex');
const knexConfig = require('./knexfile');
const app = express();
const PORT = process.env.PORT || 8080;
const API_KEY = process.env.API_KEY || 'martik';
const db = knex(knexConfig[process.env.NODE_ENV || 'development']);
const docsPath = path.join(__dirname, 'docs');
app.use(cors());
app.use(express.json());
app.use('/docs', express.static(docsPath));
app.get('/', (req, res) => {
res.sendFile(path.join(docsPath, 'index.html'));
});
cloudinary.config({
cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET
});
const storage = new CloudinaryStorage({
cloudinary: cloudinary,
params: {
folder: 'uploads',
resource_type: 'auto', // This allows uploading of any file type
public_id: (req, file) => `${Date.now()}-${file.originalname}`
},
});
const upload = multer({ storage });
const apiKeyMiddleware = (req, res, next) => {
const apiKey = req.query.api_key || req.headers['x-api-key'];
if (apiKey && apiKey === API_KEY) {
next();
} else {
res.status(403).json({ message: 'Forbidden: Invalid API Key' });
}
};
app.use('/videos', apiKeyMiddleware);
app.use('/videos/:id', apiKeyMiddleware);
app.use('/videos/:id/comments', apiKeyMiddleware);
app.use('/videos/:id/comments/:commentId', apiKeyMiddleware);
app.use('/videos/:id/like', apiKeyMiddleware);
app.get('/videos', async (req, res) => {
try {
const videos = await db('videos').select();
videos.forEach(video => {
if (!video.image.startsWith('http')) {
video.image = `${req.protocol}://${req.get('host')}${video.image}`;
}
if (!video.video.startsWith('http')) {
video.video = `${req.protocol}://${req.get('host')}${video.video}`;
}
});
res.json(videos);
} catch (error) {
res.status(500).json({ message: 'Error fetching videos' });
}
});
app.get('/videos/:id', async (req, res) => {
try {
const video = await db('videos').where({ id: req.params.id }).first();
if (video) {
if (!video.image.startsWith('http')) {
video.image = `${req.protocol}://${req.get('host')}${video.image}`;
}
if (!video.video.startsWith('http')) {
video.video = `${req.protocol}://${req.get('host')}${video.video}`;
}
const comments = await db('comments').where({ video_id: req.params.id });
video.comments = comments;
res.json(video);
} else {
res.status(404).json({ message: 'Video not found' });
}
} catch (error) {
res.status(500).json({ message: 'Error fetching video' });
}
});
app.post('/videos', upload.single('file'), async (req, res) => {
try {
const { title, description, channel } = req.body;
const fileUrl = req.file ? req.file.path : 'default-thumbnail-url';
const newVideo = {
id: uuidv4(),
title,
description,
channel: channel || 'Default Channel',
image: fileUrl,
views: '0',
likes: '0',
duration: '0:00',
video: fileUrl, // Assuming the uploaded file is the video file
timestamp: Date.now()
};
await db('videos').insert(newVideo);
res.status(201).json(newVideo);
} catch (error) {
res.status(500).json({ message: 'Error creating video' });
}
});
app.post('/videos/:id/comments', async (req, res) => {
try {
const { name, comment } = req.body;
const video = await db('videos').where({ id: req.params.id }).first();
if (video) {
const newComment = {
id: uuidv4(),
video_id: req.params.id,
name,
comment,
likes: 0,
timestamp: Date.now()
};
await db('comments').insert(newComment);
res.status(201).json(newComment);
} else {
res.status(404).json({ message: 'Video not found' });
}
} catch (error) {
res.status(500).json({ message: 'Error adding comment' });
}
});
app.delete('/videos/:id/comments/:commentId', async (req, res) => {
try {
const video = await db('videos').where({ id: req.params.id }).first();
if (video) {
await db('comments').where({ id: req.params.commentId }).del();
res.status(204).send();
} else {
res.status(404).json({ message: 'Video not found' });
}
} catch (error) {
res.status(500).json({ message: 'Error deleting comment' });
}
});
app.post('/videos/:id/like', async (req, res) => {
try {
const video = await db('videos').where({ id: req.params.id }).first();
if (video) {
video.likes = (parseInt(video.likes, 10) + 1).toString();
await db('videos').where({ id: req.params.id }).update({ likes: video.likes });
res.status(200).json(video);
} else {
res.status(404).json({ message: 'Video not found' });
}
} catch (error) {
res.status(500).json({ message: 'Error liking video' });
}
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});