1 import * as express from 'express'
2 import { auditLoggerFactory } from '../../../helpers/audit-logger'
5 asyncRetryTransactionMiddleware,
7 videoImportAddValidator
8 } from '../../../middlewares'
9 import { CONFIG, IMAGE_MIMETYPE_EXT, PREVIEWS_SIZE, sequelizeTypescript, THUMBNAILS_SIZE } from '../../../initializers'
10 import { getYoutubeDLInfo, YoutubeDLInfo } from '../../../helpers/youtube-dl'
11 import { createReqFiles } from '../../../helpers/express-utils'
12 import { logger } from '../../../helpers/logger'
13 import { VideoImportCreate, VideoImportState, VideoPrivacy, VideoState } from '../../../../shared'
14 import { VideoModel } from '../../../models/video/video'
15 import { getVideoActivityPubUrl } from '../../../lib/activitypub'
16 import { TagModel } from '../../../models/video/tag'
17 import { VideoImportModel } from '../../../models/video/video-import'
18 import { JobQueue } from '../../../lib/job-queue/job-queue'
19 import { processImage } from '../../../helpers/image-utils'
20 import { join } from 'path'
22 const auditLogger = auditLoggerFactory('video-imports')
23 const videoImportsRouter = express.Router()
25 const reqVideoFileImport = createReqFiles(
26 [ 'thumbnailfile', 'previewfile' ],
29 thumbnailfile: CONFIG.STORAGE.THUMBNAILS_DIR,
30 previewfile: CONFIG.STORAGE.PREVIEWS_DIR
34 videoImportsRouter.post('/imports',
37 asyncMiddleware(videoImportAddValidator),
38 asyncRetryTransactionMiddleware(addVideoImport)
41 // ---------------------------------------------------------------------------
47 // ---------------------------------------------------------------------------
49 async function addVideoImport (req: express.Request, res: express.Response) {
50 const body: VideoImportCreate = req.body
51 const targetUrl = body.targetUrl
53 let youtubeDLInfo: YoutubeDLInfo
55 youtubeDLInfo = await getYoutubeDLInfo(targetUrl)
57 logger.info('Cannot fetch information from import for URL %s.', targetUrl, { err })
59 return res.status(400).json({
60 error: 'Cannot fetch remote information of this URL.'
64 // Create video DB object
66 name: body.name || youtubeDLInfo.name,
68 category: body.category || youtubeDLInfo.category,
69 licence: body.licence || youtubeDLInfo.licence,
71 commentsEnabled: body.commentsEnabled || true,
72 waitTranscoding: body.waitTranscoding || false,
73 state: VideoState.TO_IMPORT,
74 nsfw: body.nsfw || youtubeDLInfo.nsfw || false,
75 description: body.description || youtubeDLInfo.description,
76 support: body.support || null,
77 privacy: body.privacy || VideoPrivacy.PRIVATE,
78 duration: 0, // duration will be set by the import job
79 channelId: res.locals.videoChannel.id
81 const video = new VideoModel(videoData)
82 video.url = getVideoActivityPubUrl(video)
84 // Process thumbnail file?
85 const thumbnailField = req.files['thumbnailfile']
86 let downloadThumbnail = true
88 const thumbnailPhysicalFile = thumbnailField[ 0 ]
89 await processImage(thumbnailPhysicalFile, join(CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName()), THUMBNAILS_SIZE)
90 downloadThumbnail = false
93 // Process preview file?
94 const previewField = req.files['previewfile']
95 let downloadPreview = true
97 const previewPhysicalFile = previewField[0]
98 await processImage(previewPhysicalFile, join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName()), PREVIEWS_SIZE)
99 downloadPreview = false
102 const videoImport: VideoImportModel = await sequelizeTypescript.transaction(async t => {
103 const sequelizeOptions = { transaction: t }
105 // Save video object in database
106 const videoCreated = await video.save(sequelizeOptions)
107 videoCreated.VideoChannel = res.locals.videoChannel
109 // Set tags to the video
110 if (youtubeDLInfo.tags !== undefined) {
111 const tagInstances = await TagModel.findOrCreateTags(youtubeDLInfo.tags, t)
113 await videoCreated.$set('Tags', tagInstances, sequelizeOptions)
114 videoCreated.Tags = tagInstances
117 // Create video import object in database
118 const videoImport = await VideoImportModel.create({
120 state: VideoImportState.PENDING,
121 videoId: videoCreated.id
124 videoImport.Video = videoCreated
129 // Create job to import the video
131 type: 'youtube-dl' as 'youtube-dl',
132 videoImportId: videoImport.id,
133 thumbnailUrl: youtubeDLInfo.thumbnailUrl,
137 await JobQueue.Instance.createJob({ type: 'video-import', payload })
139 return res.json(videoImport.toFormattedJSON())