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