]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/import.ts
ee11b0741ded6fcacfa56e6e376857167ba1e7a2
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / import.ts
1 import * as express from 'express'
2 import { auditLoggerFactory, VideoImportAuditView } from '../../../helpers/audit-logger'
3 import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videoImportAddValidator } from '../../../middlewares'
4 import { CONFIG, IMAGE_MIMETYPE_EXT, PREVIEWS_SIZE, sequelizeTypescript, THUMBNAILS_SIZE } from '../../../initializers'
5 import { getYoutubeDLInfo, YoutubeDLInfo } from '../../../helpers/youtube-dl'
6 import { createReqFiles } from '../../../helpers/express-utils'
7 import { logger } from '../../../helpers/logger'
8 import { VideoImportCreate, VideoImportState, VideoPrivacy, VideoState } from '../../../../shared'
9 import { VideoModel } from '../../../models/video/video'
10 import { getVideoActivityPubUrl } from '../../../lib/activitypub'
11 import { TagModel } from '../../../models/video/tag'
12 import { VideoImportModel } from '../../../models/video/video-import'
13 import { JobQueue } from '../../../lib/job-queue/job-queue'
14 import { processImage } from '../../../helpers/image-utils'
15 import { join } from 'path'
16
17 const auditLogger = auditLoggerFactory('video-imports')
18 const videoImportsRouter = express.Router()
19
20 const reqVideoFileImport = createReqFiles(
21 [ 'thumbnailfile', 'previewfile' ],
22 IMAGE_MIMETYPE_EXT,
23 {
24 thumbnailfile: CONFIG.STORAGE.THUMBNAILS_DIR,
25 previewfile: CONFIG.STORAGE.PREVIEWS_DIR
26 }
27 )
28
29 videoImportsRouter.post('/imports',
30 authenticate,
31 reqVideoFileImport,
32 asyncMiddleware(videoImportAddValidator),
33 asyncRetryTransactionMiddleware(addVideoImport)
34 )
35
36 // ---------------------------------------------------------------------------
37
38 export {
39 videoImportsRouter
40 }
41
42 // ---------------------------------------------------------------------------
43
44 async function addVideoImport (req: express.Request, res: express.Response) {
45 const body: VideoImportCreate = req.body
46 const targetUrl = body.targetUrl
47
48 let youtubeDLInfo: YoutubeDLInfo
49 try {
50 youtubeDLInfo = await getYoutubeDLInfo(targetUrl)
51 } catch (err) {
52 logger.info('Cannot fetch information from import for URL %s.', targetUrl, { err })
53
54 return res.status(400).json({
55 error: 'Cannot fetch remote information of this URL.'
56 }).end()
57 }
58
59 // Create video DB object
60 const videoData = {
61 name: body.name || youtubeDLInfo.name,
62 remote: false,
63 category: body.category || youtubeDLInfo.category,
64 licence: body.licence || youtubeDLInfo.licence,
65 language: undefined,
66 commentsEnabled: body.commentsEnabled || true,
67 waitTranscoding: body.waitTranscoding || false,
68 state: VideoState.TO_IMPORT,
69 nsfw: body.nsfw || youtubeDLInfo.nsfw || false,
70 description: body.description || youtubeDLInfo.description,
71 support: body.support || null,
72 privacy: body.privacy || VideoPrivacy.PRIVATE,
73 duration: 0, // duration will be set by the import job
74 channelId: res.locals.videoChannel.id
75 }
76 const video = new VideoModel(videoData)
77 video.url = getVideoActivityPubUrl(video)
78
79 // Process thumbnail file?
80 const thumbnailField = req.files ? req.files['thumbnailfile'] : undefined
81 let downloadThumbnail = true
82 if (thumbnailField) {
83 const thumbnailPhysicalFile = thumbnailField[ 0 ]
84 await processImage(thumbnailPhysicalFile, join(CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName()), THUMBNAILS_SIZE)
85 downloadThumbnail = false
86 }
87
88 // Process preview file?
89 const previewField = req.files ? req.files['previewfile'] : undefined
90 let downloadPreview = true
91 if (previewField) {
92 const previewPhysicalFile = previewField[0]
93 await processImage(previewPhysicalFile, join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName()), PREVIEWS_SIZE)
94 downloadPreview = false
95 }
96
97 const videoImport: VideoImportModel = await sequelizeTypescript.transaction(async t => {
98 const sequelizeOptions = { transaction: t }
99
100 // Save video object in database
101 const videoCreated = await video.save(sequelizeOptions)
102 videoCreated.VideoChannel = res.locals.videoChannel
103
104 // Set tags to the video
105 if (youtubeDLInfo.tags !== undefined) {
106 const tagInstances = await TagModel.findOrCreateTags(youtubeDLInfo.tags, t)
107
108 await videoCreated.$set('Tags', tagInstances, sequelizeOptions)
109 videoCreated.Tags = tagInstances
110 }
111
112 // Create video import object in database
113 const videoImport = await VideoImportModel.create({
114 targetUrl,
115 state: VideoImportState.PENDING,
116 videoId: videoCreated.id
117 }, sequelizeOptions)
118
119 videoImport.Video = videoCreated
120
121 return videoImport
122 })
123
124 // Create job to import the video
125 const payload = {
126 type: 'youtube-dl' as 'youtube-dl',
127 videoImportId: videoImport.id,
128 thumbnailUrl: youtubeDLInfo.thumbnailUrl,
129 downloadThumbnail,
130 downloadPreview
131 }
132 await JobQueue.Instance.createJob({ type: 'video-import', payload })
133
134 auditLogger.create(res.locals.oauth.token.User.Account.Actor.getIdentifier(), new VideoImportAuditView(videoImport.toFormattedJSON()))
135
136 return res.json(videoImport.toFormattedJSON())
137 }