]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/import.ts
Add tests regarding video import
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / import.ts
CommitLineData
fbad87b0 1import * as express from 'express'
7e5f9f00
C
2import { auditLoggerFactory, VideoImportAuditView } from '../../../helpers/audit-logger'
3import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videoImportAddValidator } from '../../../middlewares'
fbad87b0
C
4import { CONFIG, IMAGE_MIMETYPE_EXT, PREVIEWS_SIZE, sequelizeTypescript, THUMBNAILS_SIZE } from '../../../initializers'
5import { getYoutubeDLInfo, YoutubeDLInfo } from '../../../helpers/youtube-dl'
6import { createReqFiles } from '../../../helpers/express-utils'
7import { logger } from '../../../helpers/logger'
8import { VideoImportCreate, VideoImportState, VideoPrivacy, VideoState } from '../../../../shared'
9import { VideoModel } from '../../../models/video/video'
10import { getVideoActivityPubUrl } from '../../../lib/activitypub'
11import { TagModel } from '../../../models/video/tag'
12import { VideoImportModel } from '../../../models/video/video-import'
13import { JobQueue } from '../../../lib/job-queue/job-queue'
14import { processImage } from '../../../helpers/image-utils'
15import { join } from 'path'
16
17const auditLogger = auditLoggerFactory('video-imports')
18const videoImportsRouter = express.Router()
19
20const 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
29videoImportsRouter.post('/imports',
30 authenticate,
31 reqVideoFileImport,
32 asyncMiddleware(videoImportAddValidator),
33 asyncRetryTransactionMiddleware(addVideoImport)
34)
35
fbad87b0
C
36// ---------------------------------------------------------------------------
37
38export {
39 videoImportsRouter
40}
41
42// ---------------------------------------------------------------------------
43
44async 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,
590fb506 65 language: body.language || undefined,
fbad87b0
C
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?
5d08a6a7 80 const thumbnailField = req.files ? req.files['thumbnailfile'] : undefined
fbad87b0
C
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?
5d08a6a7 89 const previewField = req.files ? req.files['previewfile'] : undefined
fbad87b0
C
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
590fb506
C
105 const tags = body.tags ? body.tags : youtubeDLInfo.tags
106 if (tags !== undefined) {
107 const tagInstances = await TagModel.findOrCreateTags(tags, t)
fbad87b0
C
108
109 await videoCreated.$set('Tags', tagInstances, sequelizeOptions)
110 videoCreated.Tags = tagInstances
111 }
112
113 // Create video import object in database
114 const videoImport = await VideoImportModel.create({
115 targetUrl,
116 state: VideoImportState.PENDING,
117 videoId: videoCreated.id
118 }, sequelizeOptions)
119
120 videoImport.Video = videoCreated
121
122 return videoImport
123 })
124
125 // Create job to import the video
126 const payload = {
127 type: 'youtube-dl' as 'youtube-dl',
128 videoImportId: videoImport.id,
129 thumbnailUrl: youtubeDLInfo.thumbnailUrl,
130 downloadThumbnail,
131 downloadPreview
132 }
133 await JobQueue.Instance.createJob({ type: 'video-import', payload })
134
7e5f9f00
C
135 auditLogger.create(res.locals.oauth.token.User.Account.Actor.getIdentifier(), new VideoImportAuditView(videoImport.toFormattedJSON()))
136
590fb506 137 return res.json(videoImport.toFormattedJSON()).end()
fbad87b0 138}