]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/import.ts
Don't expose constants directly in initializers/
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / import.ts
1 import * as express from 'express'
2 import * as magnetUtil from 'magnet-uri'
3 import 'multer'
4 import { auditLoggerFactory, getAuditIdFromRes, VideoImportAuditView } from '../../../helpers/audit-logger'
5 import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videoImportAddValidator } from '../../../middlewares'
6 import { MIMETYPES, PREVIEWS_SIZE, THUMBNAILS_SIZE } from '../../../initializers/constants'
7 import { getYoutubeDLInfo, YoutubeDLInfo } from '../../../helpers/youtube-dl'
8 import { createReqFiles } from '../../../helpers/express-utils'
9 import { logger } from '../../../helpers/logger'
10 import { VideoImportCreate, VideoImportState, VideoPrivacy, VideoState } from '../../../../shared'
11 import { VideoModel } from '../../../models/video/video'
12 import { getVideoActivityPubUrl } from '../../../lib/activitypub'
13 import { TagModel } from '../../../models/video/tag'
14 import { VideoImportModel } from '../../../models/video/video-import'
15 import { JobQueue } from '../../../lib/job-queue/job-queue'
16 import { processImage } from '../../../helpers/image-utils'
17 import { join } from 'path'
18 import { isArray } from '../../../helpers/custom-validators/misc'
19 import { FilteredModelAttributes } from 'sequelize-typescript/lib/models/Model'
20 import { VideoChannelModel } from '../../../models/video/video-channel'
21 import { UserModel } from '../../../models/account/user'
22 import * as Bluebird from 'bluebird'
23 import * as parseTorrent from 'parse-torrent'
24 import { getSecureTorrentName } from '../../../helpers/utils'
25 import { move, readFile } from 'fs-extra'
26 import { autoBlacklistVideoIfNeeded } from '../../../lib/video-blacklist'
27 import { CONFIG } from '../../../initializers/config'
28 import { sequelizeTypescript } from '../../../initializers/database'
29
30 const auditLogger = auditLoggerFactory('video-imports')
31 const videoImportsRouter = express.Router()
32
33 const reqVideoFileImport = createReqFiles(
34 [ 'thumbnailfile', 'previewfile', 'torrentfile' ],
35 Object.assign({}, MIMETYPES.TORRENT.MIMETYPE_EXT, MIMETYPES.IMAGE.MIMETYPE_EXT),
36 {
37 thumbnailfile: CONFIG.STORAGE.TMP_DIR,
38 previewfile: CONFIG.STORAGE.TMP_DIR,
39 torrentfile: CONFIG.STORAGE.TMP_DIR
40 }
41 )
42
43 videoImportsRouter.post('/imports',
44 authenticate,
45 reqVideoFileImport,
46 asyncMiddleware(videoImportAddValidator),
47 asyncRetryTransactionMiddleware(addVideoImport)
48 )
49
50 // ---------------------------------------------------------------------------
51
52 export {
53 videoImportsRouter
54 }
55
56 // ---------------------------------------------------------------------------
57
58 function addVideoImport (req: express.Request, res: express.Response) {
59 if (req.body.targetUrl) return addYoutubeDLImport(req, res)
60
61 const file = req.files && req.files['torrentfile'] ? req.files['torrentfile'][0] : undefined
62 if (req.body.magnetUri || file) return addTorrentImport(req, res, file)
63 }
64
65 async function addTorrentImport (req: express.Request, res: express.Response, torrentfile: Express.Multer.File) {
66 const body: VideoImportCreate = req.body
67 const user = res.locals.oauth.token.User
68
69 let videoName: string
70 let torrentName: string
71 let magnetUri: string
72
73 if (torrentfile) {
74 torrentName = torrentfile.originalname
75
76 // Rename the torrent to a secured name
77 const newTorrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, getSecureTorrentName(torrentName))
78 await move(torrentfile.path, newTorrentPath)
79 torrentfile.path = newTorrentPath
80
81 const buf = await readFile(torrentfile.path)
82 const parsedTorrent = parseTorrent(buf)
83
84 videoName = isArray(parsedTorrent.name) ? parsedTorrent.name[ 0 ] : parsedTorrent.name as string
85 } else {
86 magnetUri = body.magnetUri
87
88 const parsed = magnetUtil.decode(magnetUri)
89 videoName = isArray(parsed.name) ? parsed.name[ 0 ] : parsed.name as string
90 }
91
92 const video = buildVideo(res.locals.videoChannel.id, body, { name: videoName }, user)
93
94 await processThumbnail(req, video)
95 await processPreview(req, video)
96
97 const tags = body.tags || undefined
98 const videoImportAttributes = {
99 magnetUri,
100 torrentName,
101 state: VideoImportState.PENDING,
102 userId: user.id
103 }
104 const videoImport = await insertIntoDB(video, res.locals.videoChannel, tags, videoImportAttributes)
105
106 // Create job to import the video
107 const payload = {
108 type: torrentfile ? 'torrent-file' as 'torrent-file' : 'magnet-uri' as 'magnet-uri',
109 videoImportId: videoImport.id,
110 magnetUri
111 }
112 await JobQueue.Instance.createJob({ type: 'video-import', payload })
113
114 auditLogger.create(getAuditIdFromRes(res), new VideoImportAuditView(videoImport.toFormattedJSON()))
115
116 return res.json(videoImport.toFormattedJSON()).end()
117 }
118
119 async function addYoutubeDLImport (req: express.Request, res: express.Response) {
120 const body: VideoImportCreate = req.body
121 const targetUrl = body.targetUrl
122 const user = res.locals.oauth.token.User
123
124 let youtubeDLInfo: YoutubeDLInfo
125 try {
126 youtubeDLInfo = await getYoutubeDLInfo(targetUrl)
127 } catch (err) {
128 logger.info('Cannot fetch information from import for URL %s.', targetUrl, { err })
129
130 return res.status(400).json({
131 error: 'Cannot fetch remote information of this URL.'
132 }).end()
133 }
134
135 const video = buildVideo(res.locals.videoChannel.id, body, youtubeDLInfo, user)
136
137 const downloadThumbnail = !await processThumbnail(req, video)
138 const downloadPreview = !await processPreview(req, video)
139
140 const tags = body.tags || youtubeDLInfo.tags
141 const videoImportAttributes = {
142 targetUrl,
143 state: VideoImportState.PENDING,
144 userId: user.id
145 }
146 const videoImport = await insertIntoDB(video, res.locals.videoChannel, tags, videoImportAttributes)
147
148 // Create job to import the video
149 const payload = {
150 type: 'youtube-dl' as 'youtube-dl',
151 videoImportId: videoImport.id,
152 thumbnailUrl: youtubeDLInfo.thumbnailUrl,
153 downloadThumbnail,
154 downloadPreview
155 }
156 await JobQueue.Instance.createJob({ type: 'video-import', payload })
157
158 auditLogger.create(getAuditIdFromRes(res), new VideoImportAuditView(videoImport.toFormattedJSON()))
159
160 return res.json(videoImport.toFormattedJSON()).end()
161 }
162
163 function buildVideo (channelId: number, body: VideoImportCreate, importData: YoutubeDLInfo, user: UserModel) {
164 const videoData = {
165 name: body.name || importData.name || 'Unknown name',
166 remote: false,
167 category: body.category || importData.category,
168 licence: body.licence || importData.licence,
169 language: body.language || undefined,
170 commentsEnabled: body.commentsEnabled || true,
171 downloadEnabled: body.downloadEnabled || true,
172 waitTranscoding: body.waitTranscoding || false,
173 state: VideoState.TO_IMPORT,
174 nsfw: body.nsfw || importData.nsfw || false,
175 description: body.description || importData.description,
176 support: body.support || null,
177 privacy: body.privacy || VideoPrivacy.PRIVATE,
178 duration: 0, // duration will be set by the import job
179 channelId: channelId,
180 originallyPublishedAt: importData.originallyPublishedAt
181 }
182 const video = new VideoModel(videoData)
183 video.url = getVideoActivityPubUrl(video)
184
185 return video
186 }
187
188 async function processThumbnail (req: express.Request, video: VideoModel) {
189 const thumbnailField = req.files ? req.files['thumbnailfile'] : undefined
190 if (thumbnailField) {
191 const thumbnailPhysicalFile = thumbnailField[ 0 ]
192 await processImage(thumbnailPhysicalFile, join(CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName()), THUMBNAILS_SIZE)
193
194 return true
195 }
196
197 return false
198 }
199
200 async function processPreview (req: express.Request, video: VideoModel) {
201 const previewField = req.files ? req.files['previewfile'] : undefined
202 if (previewField) {
203 const previewPhysicalFile = previewField[0]
204 await processImage(previewPhysicalFile, join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName()), PREVIEWS_SIZE)
205
206 return true
207 }
208
209 return false
210 }
211
212 function insertIntoDB (
213 video: VideoModel,
214 videoChannel: VideoChannelModel,
215 tags: string[],
216 videoImportAttributes: FilteredModelAttributes<VideoImportModel>
217 ): Bluebird<VideoImportModel> {
218 return sequelizeTypescript.transaction(async t => {
219 const sequelizeOptions = { transaction: t }
220
221 // Save video object in database
222 const videoCreated = await video.save(sequelizeOptions)
223 videoCreated.VideoChannel = videoChannel
224
225 await autoBlacklistVideoIfNeeded(video, videoChannel.Account.User, t)
226
227 // Set tags to the video
228 if (tags) {
229 const tagInstances = await TagModel.findOrCreateTags(tags, t)
230
231 await videoCreated.$set('Tags', tagInstances, sequelizeOptions)
232 videoCreated.Tags = tagInstances
233 } else {
234 videoCreated.Tags = []
235 }
236
237 // Create video import object in database
238 const videoImport = await VideoImportModel.create(
239 Object.assign({ videoId: videoCreated.id }, videoImportAttributes),
240 sequelizeOptions
241 )
242 videoImport.Video = videoCreated
243
244 return videoImport
245 })
246 }