]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/import.ts
680d8665fdfcf0eaea0f4b2b5201d8dfef6b3714
[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 videoImportDeleteValidator
9 } from '../../../middlewares'
10 import { CONFIG, IMAGE_MIMETYPE_EXT, PREVIEWS_SIZE, sequelizeTypescript, THUMBNAILS_SIZE } from '../../../initializers'
11 import { getYoutubeDLInfo, YoutubeDLInfo } from '../../../helpers/youtube-dl'
12 import { createReqFiles } from '../../../helpers/express-utils'
13 import { logger } from '../../../helpers/logger'
14 import { VideoImportCreate, VideoImportState, VideoPrivacy, VideoState } from '../../../../shared'
15 import { VideoModel } from '../../../models/video/video'
16 import { getVideoActivityPubUrl } from '../../../lib/activitypub'
17 import { TagModel } from '../../../models/video/tag'
18 import { VideoImportModel } from '../../../models/video/video-import'
19 import { JobQueue } from '../../../lib/job-queue/job-queue'
20 import { processImage } from '../../../helpers/image-utils'
21 import { join } from 'path'
22
23 const auditLogger = auditLoggerFactory('video-imports')
24 const videoImportsRouter = express.Router()
25
26 const reqVideoFileImport = createReqFiles(
27 [ 'thumbnailfile', 'previewfile' ],
28 IMAGE_MIMETYPE_EXT,
29 {
30 thumbnailfile: CONFIG.STORAGE.THUMBNAILS_DIR,
31 previewfile: CONFIG.STORAGE.PREVIEWS_DIR
32 }
33 )
34
35 videoImportsRouter.post('/imports',
36 authenticate,
37 reqVideoFileImport,
38 asyncMiddleware(videoImportAddValidator),
39 asyncRetryTransactionMiddleware(addVideoImport)
40 )
41
42 videoImportsRouter.delete('/imports/:id',
43 authenticate,
44 asyncMiddleware(videoImportDeleteValidator),
45 asyncRetryTransactionMiddleware(deleteVideoImport)
46 )
47
48 // ---------------------------------------------------------------------------
49
50 export {
51 videoImportsRouter
52 }
53
54 // ---------------------------------------------------------------------------
55
56 async function addVideoImport (req: express.Request, res: express.Response) {
57 const body: VideoImportCreate = req.body
58 const targetUrl = body.targetUrl
59
60 let youtubeDLInfo: YoutubeDLInfo
61 try {
62 youtubeDLInfo = await getYoutubeDLInfo(targetUrl)
63 } catch (err) {
64 logger.info('Cannot fetch information from import for URL %s.', targetUrl, { err })
65
66 return res.status(400).json({
67 error: 'Cannot fetch remote information of this URL.'
68 }).end()
69 }
70
71 // Create video DB object
72 const videoData = {
73 name: body.name || youtubeDLInfo.name,
74 remote: false,
75 category: body.category || youtubeDLInfo.category,
76 licence: body.licence || youtubeDLInfo.licence,
77 language: undefined,
78 commentsEnabled: body.commentsEnabled || true,
79 waitTranscoding: body.waitTranscoding || false,
80 state: VideoState.TO_IMPORT,
81 nsfw: body.nsfw || youtubeDLInfo.nsfw || false,
82 description: body.description || youtubeDLInfo.description,
83 support: body.support || null,
84 privacy: body.privacy || VideoPrivacy.PRIVATE,
85 duration: 0, // duration will be set by the import job
86 channelId: res.locals.videoChannel.id
87 }
88 const video = new VideoModel(videoData)
89 video.url = getVideoActivityPubUrl(video)
90
91 // Process thumbnail file?
92 const thumbnailField = req.files['thumbnailfile']
93 let downloadThumbnail = true
94 if (thumbnailField) {
95 const thumbnailPhysicalFile = thumbnailField[ 0 ]
96 await processImage(thumbnailPhysicalFile, join(CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName()), THUMBNAILS_SIZE)
97 downloadThumbnail = false
98 }
99
100 // Process preview file?
101 const previewField = req.files['previewfile']
102 let downloadPreview = true
103 if (previewField) {
104 const previewPhysicalFile = previewField[0]
105 await processImage(previewPhysicalFile, join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName()), PREVIEWS_SIZE)
106 downloadPreview = false
107 }
108
109 const videoImport: VideoImportModel = await sequelizeTypescript.transaction(async t => {
110 const sequelizeOptions = { transaction: t }
111
112 // Save video object in database
113 const videoCreated = await video.save(sequelizeOptions)
114 videoCreated.VideoChannel = res.locals.videoChannel
115
116 // Set tags to the video
117 if (youtubeDLInfo.tags !== undefined) {
118 const tagInstances = await TagModel.findOrCreateTags(youtubeDLInfo.tags, t)
119
120 await videoCreated.$set('Tags', tagInstances, sequelizeOptions)
121 videoCreated.Tags = tagInstances
122 }
123
124 // Create video import object in database
125 const videoImport = await VideoImportModel.create({
126 targetUrl,
127 state: VideoImportState.PENDING,
128 videoId: videoCreated.id
129 }, sequelizeOptions)
130
131 videoImport.Video = videoCreated
132
133 return videoImport
134 })
135
136 // Create job to import the video
137 const payload = {
138 type: 'youtube-dl' as 'youtube-dl',
139 videoImportId: videoImport.id,
140 thumbnailUrl: youtubeDLInfo.thumbnailUrl,
141 downloadThumbnail,
142 downloadPreview
143 }
144 await JobQueue.Instance.createJob({ type: 'video-import', payload })
145
146 return res.json(videoImport.toFormattedJSON())
147 }
148
149 async function deleteVideoImport (req: express.Request, res: express.Response) {
150 await sequelizeTypescript.transaction(async t => {
151 const videoImport = res.locals.videoImport
152 const video = videoImport.Video
153
154 await videoImport.destroy({ transaction: t })
155 await video.destroy({ transaction: t })
156 })
157
158 return res.status(204).end()
159 }