-
- const video = await buildVideo(res.locals.videoChannel.id, body, youtubeDLInfo)
-
- // Process video thumbnail from request.files
- let thumbnailModel = await processThumbnail(req, video)
-
- // Process video thumbnail from url if processing from request.files failed
- if (!thumbnailModel && youtubeDLInfo.thumbnailUrl) {
- try {
- thumbnailModel = await processThumbnailFromUrl(youtubeDLInfo.thumbnailUrl, video)
- } catch (err) {
- logger.warn('Cannot process thumbnail %s from youtubedl.', youtubeDLInfo.thumbnailUrl, { err })
- }
- }
-
- // Process video preview from request.files
- let previewModel = await processPreview(req, video)
-
- // Process video preview from url if processing from request.files failed
- if (!previewModel && youtubeDLInfo.thumbnailUrl) {
- try {
- previewModel = await processPreviewFromUrl(youtubeDLInfo.thumbnailUrl, video)
- } catch (err) {
- logger.warn('Cannot process preview %s from youtubedl.', youtubeDLInfo.thumbnailUrl, { err })
- }
- }
-
- const videoImport = await insertIntoDB({
- video,
- thumbnailModel,
- previewModel,
- videoChannel: res.locals.videoChannel,
- tags: body.tags || youtubeDLInfo.tags,
- user,
- videoImportAttributes: {
- targetUrl,
- state: VideoImportState.PENDING,
- userId: user.id
- }
- })
-
- // Get video subtitles
- await processYoutubeSubtitles(youtubeDL, targetUrl, video.id)
-
- let fileExt = `.${youtubeDLInfo.ext}`
- if (!isVideoFileExtnameValid(fileExt)) fileExt = '.mp4'
-
- // Create job to import the video
- const payload = {
- type: 'youtube-dl' as 'youtube-dl',
- videoImportId: videoImport.id,
- fileExt
- }
- await JobQueue.Instance.createJobWithPromise({ type: 'video-import', payload })
-
- auditLogger.create(getAuditIdFromRes(res), new VideoImportAuditView(videoImport.toFormattedJSON()))
-
- return res.json(videoImport.toFormattedJSON()).end()
-}
-
-async function buildVideo (channelId: number, body: VideoImportCreate, importData: YoutubeDLInfo): Promise<MVideoThumbnail> {
- let videoData = {
- name: body.name || importData.name || 'Unknown name',
- remote: false,
- category: body.category || importData.category,
- licence: body.licence ?? importData.licence ?? CONFIG.DEFAULTS.PUBLISH.LICENCE,
- language: body.language || importData.language,
- commentsEnabled: body.commentsEnabled ?? CONFIG.DEFAULTS.PUBLISH.COMMENTS_ENABLED,
- downloadEnabled: body.downloadEnabled ?? CONFIG.DEFAULTS.PUBLISH.DOWNLOAD_ENABLED,
- waitTranscoding: body.waitTranscoding || false,
- state: VideoState.TO_IMPORT,
- nsfw: body.nsfw || importData.nsfw || false,
- description: body.description || importData.description,
- support: body.support || null,
- privacy: body.privacy || VideoPrivacy.PRIVATE,
- duration: 0, // duration will be set by the import job
- channelId: channelId,
- originallyPublishedAt: body.originallyPublishedAt
- ? new Date(body.originallyPublishedAt)
- : importData.originallyPublishedAt
- }
-
- videoData = await Hooks.wrapObject(
- videoData,
- body.targetUrl
- ? 'filter:api.video.import-url.video-attribute.result'
- : 'filter:api.video.import-torrent.video-attribute.result'
- )
-
- const video = new VideoModel(videoData)
- video.url = getLocalVideoActivityPubUrl(video)
-
- return video