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'
17 const auditLogger = auditLoggerFactory('video-imports')
18 const videoImportsRouter = express.Router()
20 const reqVideoFileImport = createReqFiles(
21 [ 'thumbnailfile', 'previewfile' ],
24 thumbnailfile: CONFIG.STORAGE.THUMBNAILS_DIR,
25 previewfile: CONFIG.STORAGE.PREVIEWS_DIR
29 videoImportsRouter.post('/imports',
32 asyncMiddleware(videoImportAddValidator),
33 asyncRetryTransactionMiddleware(addVideoImport)
36 // ---------------------------------------------------------------------------
42 // ---------------------------------------------------------------------------
44 async function addVideoImport (req: express.Request, res: express.Response) {
45 const body: VideoImportCreate = req.body
46 const targetUrl = body.targetUrl
48 let youtubeDLInfo: YoutubeDLInfo
50 youtubeDLInfo = await getYoutubeDLInfo(targetUrl)
52 logger.info('Cannot fetch information from import for URL %s.', targetUrl, { err })
54 return res.status(400).json({
55 error: 'Cannot fetch remote information of this URL.'
59 // Create video DB object
61 name: body.name || youtubeDLInfo.name,
63 category: body.category || youtubeDLInfo.category,
64 licence: body.licence || youtubeDLInfo.licence,
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
76 const video = new VideoModel(videoData)
77 video.url = getVideoActivityPubUrl(video)
79 // Process thumbnail file?
80 const thumbnailField = req.files ? req.files['thumbnailfile'] : undefined
81 let downloadThumbnail = true
83 const thumbnailPhysicalFile = thumbnailField[ 0 ]
84 await processImage(thumbnailPhysicalFile, join(CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName()), THUMBNAILS_SIZE)
85 downloadThumbnail = false
88 // Process preview file?
89 const previewField = req.files ? req.files['previewfile'] : undefined
90 let downloadPreview = true
92 const previewPhysicalFile = previewField[0]
93 await processImage(previewPhysicalFile, join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName()), PREVIEWS_SIZE)
94 downloadPreview = false
97 const videoImport: VideoImportModel = await sequelizeTypescript.transaction(async t => {
98 const sequelizeOptions = { transaction: t }
100 // Save video object in database
101 const videoCreated = await video.save(sequelizeOptions)
102 videoCreated.VideoChannel = res.locals.videoChannel
104 // Set tags to the video
105 if (youtubeDLInfo.tags !== undefined) {
106 const tagInstances = await TagModel.findOrCreateTags(youtubeDLInfo.tags, t)
108 await videoCreated.$set('Tags', tagInstances, sequelizeOptions)
109 videoCreated.Tags = tagInstances
112 // Create video import object in database
113 const videoImport = await VideoImportModel.create({
115 state: VideoImportState.PENDING,
116 videoId: videoCreated.id
119 videoImport.Video = videoCreated
124 // Create job to import the video
126 type: 'youtube-dl' as 'youtube-dl',
127 videoImportId: videoImport.id,
128 thumbnailUrl: youtubeDLInfo.thumbnailUrl,
132 await JobQueue.Instance.createJob({ type: 'video-import', payload })
134 auditLogger.create(res.locals.oauth.token.User.Account.Actor.getIdentifier(), new VideoImportAuditView(videoImport.toFormattedJSON()))
136 return res.json(videoImport.toFormattedJSON())