]>
Commit | Line | Data |
---|---|---|
1 | import express from 'express' | |
2 | import { join } from 'path' | |
3 | import { scheduleRefreshIfNeeded } from '@server/lib/activitypub/playlists' | |
4 | import { Hooks } from '@server/lib/plugins/hooks' | |
5 | import { getServerActor } from '@server/models/application/application' | |
6 | import { MVideoPlaylistFull, MVideoPlaylistThumbnail, MVideoThumbnail } from '@server/types/models' | |
7 | import { forceNumber } from '@shared/core-utils' | |
8 | import { uuidToShort } from '@shared/extra-utils' | |
9 | import { VideoPlaylistCreateResult, VideoPlaylistElementCreateResult } from '@shared/models' | |
10 | import { HttpStatusCode } from '../../../shared/models/http/http-error-codes' | |
11 | import { VideoPlaylistCreate } from '../../../shared/models/videos/playlist/video-playlist-create.model' | |
12 | import { VideoPlaylistElementCreate } from '../../../shared/models/videos/playlist/video-playlist-element-create.model' | |
13 | import { VideoPlaylistElementUpdate } from '../../../shared/models/videos/playlist/video-playlist-element-update.model' | |
14 | import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model' | |
15 | import { VideoPlaylistReorder } from '../../../shared/models/videos/playlist/video-playlist-reorder.model' | |
16 | import { VideoPlaylistUpdate } from '../../../shared/models/videos/playlist/video-playlist-update.model' | |
17 | import { resetSequelizeInstance } from '../../helpers/database-utils' | |
18 | import { createReqFiles } from '../../helpers/express-utils' | |
19 | import { logger } from '../../helpers/logger' | |
20 | import { getFormattedObjects } from '../../helpers/utils' | |
21 | import { CONFIG } from '../../initializers/config' | |
22 | import { MIMETYPES, VIDEO_PLAYLIST_PRIVACIES } from '../../initializers/constants' | |
23 | import { sequelizeTypescript } from '../../initializers/database' | |
24 | import { sendCreateVideoPlaylist, sendDeleteVideoPlaylist, sendUpdateVideoPlaylist } from '../../lib/activitypub/send' | |
25 | import { getLocalVideoPlaylistActivityPubUrl, getLocalVideoPlaylistElementActivityPubUrl } from '../../lib/activitypub/url' | |
26 | import { updatePlaylistMiniatureFromExisting } from '../../lib/thumbnail' | |
27 | import { | |
28 | asyncMiddleware, | |
29 | asyncRetryTransactionMiddleware, | |
30 | authenticate, | |
31 | optionalAuthenticate, | |
32 | paginationValidator, | |
33 | setDefaultPagination, | |
34 | setDefaultSort | |
35 | } from '../../middlewares' | |
36 | import { videoPlaylistsSortValidator } from '../../middlewares/validators' | |
37 | import { | |
38 | commonVideoPlaylistFiltersValidator, | |
39 | videoPlaylistsAddValidator, | |
40 | videoPlaylistsAddVideoValidator, | |
41 | videoPlaylistsDeleteValidator, | |
42 | videoPlaylistsGetValidator, | |
43 | videoPlaylistsReorderVideosValidator, | |
44 | videoPlaylistsUpdateOrRemoveVideoValidator, | |
45 | videoPlaylistsUpdateValidator | |
46 | } from '../../middlewares/validators/videos/video-playlists' | |
47 | import { AccountModel } from '../../models/account/account' | |
48 | import { VideoPlaylistModel } from '../../models/video/video-playlist' | |
49 | import { VideoPlaylistElementModel } from '../../models/video/video-playlist-element' | |
50 | ||
51 | const reqThumbnailFile = createReqFiles([ 'thumbnailfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT) | |
52 | ||
53 | const videoPlaylistRouter = express.Router() | |
54 | ||
55 | videoPlaylistRouter.get('/privacies', listVideoPlaylistPrivacies) | |
56 | ||
57 | videoPlaylistRouter.get('/', | |
58 | paginationValidator, | |
59 | videoPlaylistsSortValidator, | |
60 | setDefaultSort, | |
61 | setDefaultPagination, | |
62 | commonVideoPlaylistFiltersValidator, | |
63 | asyncMiddleware(listVideoPlaylists) | |
64 | ) | |
65 | ||
66 | videoPlaylistRouter.get('/:playlistId', | |
67 | asyncMiddleware(videoPlaylistsGetValidator('summary')), | |
68 | getVideoPlaylist | |
69 | ) | |
70 | ||
71 | videoPlaylistRouter.post('/', | |
72 | authenticate, | |
73 | reqThumbnailFile, | |
74 | asyncMiddleware(videoPlaylistsAddValidator), | |
75 | asyncRetryTransactionMiddleware(addVideoPlaylist) | |
76 | ) | |
77 | ||
78 | videoPlaylistRouter.put('/:playlistId', | |
79 | authenticate, | |
80 | reqThumbnailFile, | |
81 | asyncMiddleware(videoPlaylistsUpdateValidator), | |
82 | asyncRetryTransactionMiddleware(updateVideoPlaylist) | |
83 | ) | |
84 | ||
85 | videoPlaylistRouter.delete('/:playlistId', | |
86 | authenticate, | |
87 | asyncMiddleware(videoPlaylistsDeleteValidator), | |
88 | asyncRetryTransactionMiddleware(removeVideoPlaylist) | |
89 | ) | |
90 | ||
91 | videoPlaylistRouter.get('/:playlistId/videos', | |
92 | asyncMiddleware(videoPlaylistsGetValidator('summary')), | |
93 | paginationValidator, | |
94 | setDefaultPagination, | |
95 | optionalAuthenticate, | |
96 | asyncMiddleware(getVideoPlaylistVideos) | |
97 | ) | |
98 | ||
99 | videoPlaylistRouter.post('/:playlistId/videos', | |
100 | authenticate, | |
101 | asyncMiddleware(videoPlaylistsAddVideoValidator), | |
102 | asyncRetryTransactionMiddleware(addVideoInPlaylist) | |
103 | ) | |
104 | ||
105 | videoPlaylistRouter.post('/:playlistId/videos/reorder', | |
106 | authenticate, | |
107 | asyncMiddleware(videoPlaylistsReorderVideosValidator), | |
108 | asyncRetryTransactionMiddleware(reorderVideosPlaylist) | |
109 | ) | |
110 | ||
111 | videoPlaylistRouter.put('/:playlistId/videos/:playlistElementId', | |
112 | authenticate, | |
113 | asyncMiddleware(videoPlaylistsUpdateOrRemoveVideoValidator), | |
114 | asyncRetryTransactionMiddleware(updateVideoPlaylistElement) | |
115 | ) | |
116 | ||
117 | videoPlaylistRouter.delete('/:playlistId/videos/:playlistElementId', | |
118 | authenticate, | |
119 | asyncMiddleware(videoPlaylistsUpdateOrRemoveVideoValidator), | |
120 | asyncRetryTransactionMiddleware(removeVideoFromPlaylist) | |
121 | ) | |
122 | ||
123 | // --------------------------------------------------------------------------- | |
124 | ||
125 | export { | |
126 | videoPlaylistRouter | |
127 | } | |
128 | ||
129 | // --------------------------------------------------------------------------- | |
130 | ||
131 | function listVideoPlaylistPrivacies (req: express.Request, res: express.Response) { | |
132 | res.json(VIDEO_PLAYLIST_PRIVACIES) | |
133 | } | |
134 | ||
135 | async function listVideoPlaylists (req: express.Request, res: express.Response) { | |
136 | const serverActor = await getServerActor() | |
137 | const resultList = await VideoPlaylistModel.listForApi({ | |
138 | followerActorId: serverActor.id, | |
139 | start: req.query.start, | |
140 | count: req.query.count, | |
141 | sort: req.query.sort, | |
142 | type: req.query.playlistType | |
143 | }) | |
144 | ||
145 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
146 | } | |
147 | ||
148 | function getVideoPlaylist (req: express.Request, res: express.Response) { | |
149 | const videoPlaylist = res.locals.videoPlaylistSummary | |
150 | ||
151 | scheduleRefreshIfNeeded(videoPlaylist) | |
152 | ||
153 | return res.json(videoPlaylist.toFormattedJSON()) | |
154 | } | |
155 | ||
156 | async function addVideoPlaylist (req: express.Request, res: express.Response) { | |
157 | const videoPlaylistInfo: VideoPlaylistCreate = req.body | |
158 | const user = res.locals.oauth.token.User | |
159 | ||
160 | const videoPlaylist = new VideoPlaylistModel({ | |
161 | name: videoPlaylistInfo.displayName, | |
162 | description: videoPlaylistInfo.description, | |
163 | privacy: videoPlaylistInfo.privacy || VideoPlaylistPrivacy.PRIVATE, | |
164 | ownerAccountId: user.Account.id | |
165 | }) as MVideoPlaylistFull | |
166 | ||
167 | videoPlaylist.url = getLocalVideoPlaylistActivityPubUrl(videoPlaylist) // We use the UUID, so set the URL after building the object | |
168 | ||
169 | if (videoPlaylistInfo.videoChannelId) { | |
170 | const videoChannel = res.locals.videoChannel | |
171 | ||
172 | videoPlaylist.videoChannelId = videoChannel.id | |
173 | videoPlaylist.VideoChannel = videoChannel | |
174 | } | |
175 | ||
176 | const thumbnailField = req.files['thumbnailfile'] | |
177 | const thumbnailModel = thumbnailField | |
178 | ? await updatePlaylistMiniatureFromExisting({ | |
179 | inputPath: thumbnailField[0].path, | |
180 | playlist: videoPlaylist, | |
181 | automaticallyGenerated: false | |
182 | }) | |
183 | : undefined | |
184 | ||
185 | const videoPlaylistCreated = await sequelizeTypescript.transaction(async t => { | |
186 | const videoPlaylistCreated = await videoPlaylist.save({ transaction: t }) as MVideoPlaylistFull | |
187 | ||
188 | if (thumbnailModel) { | |
189 | thumbnailModel.automaticallyGenerated = false | |
190 | await videoPlaylistCreated.setAndSaveThumbnail(thumbnailModel, t) | |
191 | } | |
192 | ||
193 | // We need more attributes for the federation | |
194 | videoPlaylistCreated.OwnerAccount = await AccountModel.load(user.Account.id, t) | |
195 | await sendCreateVideoPlaylist(videoPlaylistCreated, t) | |
196 | ||
197 | return videoPlaylistCreated | |
198 | }) | |
199 | ||
200 | logger.info('Video playlist with uuid %s created.', videoPlaylist.uuid) | |
201 | ||
202 | return res.json({ | |
203 | videoPlaylist: { | |
204 | id: videoPlaylistCreated.id, | |
205 | shortUUID: uuidToShort(videoPlaylistCreated.uuid), | |
206 | uuid: videoPlaylistCreated.uuid | |
207 | } as VideoPlaylistCreateResult | |
208 | }) | |
209 | } | |
210 | ||
211 | async function updateVideoPlaylist (req: express.Request, res: express.Response) { | |
212 | const videoPlaylistInstance = res.locals.videoPlaylistFull | |
213 | const videoPlaylistInfoToUpdate = req.body as VideoPlaylistUpdate | |
214 | ||
215 | const wasPrivatePlaylist = videoPlaylistInstance.privacy === VideoPlaylistPrivacy.PRIVATE | |
216 | const wasNotPrivatePlaylist = videoPlaylistInstance.privacy !== VideoPlaylistPrivacy.PRIVATE | |
217 | ||
218 | const thumbnailField = req.files['thumbnailfile'] | |
219 | const thumbnailModel = thumbnailField | |
220 | ? await updatePlaylistMiniatureFromExisting({ | |
221 | inputPath: thumbnailField[0].path, | |
222 | playlist: videoPlaylistInstance, | |
223 | automaticallyGenerated: false | |
224 | }) | |
225 | : undefined | |
226 | ||
227 | try { | |
228 | await sequelizeTypescript.transaction(async t => { | |
229 | const sequelizeOptions = { | |
230 | transaction: t | |
231 | } | |
232 | ||
233 | if (videoPlaylistInfoToUpdate.videoChannelId !== undefined) { | |
234 | if (videoPlaylistInfoToUpdate.videoChannelId === null) { | |
235 | videoPlaylistInstance.videoChannelId = null | |
236 | } else { | |
237 | const videoChannel = res.locals.videoChannel | |
238 | ||
239 | videoPlaylistInstance.videoChannelId = videoChannel.id | |
240 | videoPlaylistInstance.VideoChannel = videoChannel | |
241 | } | |
242 | } | |
243 | ||
244 | if (videoPlaylistInfoToUpdate.displayName !== undefined) videoPlaylistInstance.name = videoPlaylistInfoToUpdate.displayName | |
245 | if (videoPlaylistInfoToUpdate.description !== undefined) videoPlaylistInstance.description = videoPlaylistInfoToUpdate.description | |
246 | ||
247 | if (videoPlaylistInfoToUpdate.privacy !== undefined) { | |
248 | videoPlaylistInstance.privacy = forceNumber(videoPlaylistInfoToUpdate.privacy) | |
249 | ||
250 | if (wasNotPrivatePlaylist === true && videoPlaylistInstance.privacy === VideoPlaylistPrivacy.PRIVATE) { | |
251 | await sendDeleteVideoPlaylist(videoPlaylistInstance, t) | |
252 | } | |
253 | } | |
254 | ||
255 | const playlistUpdated = await videoPlaylistInstance.save(sequelizeOptions) | |
256 | ||
257 | if (thumbnailModel) { | |
258 | thumbnailModel.automaticallyGenerated = false | |
259 | await playlistUpdated.setAndSaveThumbnail(thumbnailModel, t) | |
260 | } | |
261 | ||
262 | const isNewPlaylist = wasPrivatePlaylist && playlistUpdated.privacy !== VideoPlaylistPrivacy.PRIVATE | |
263 | ||
264 | if (isNewPlaylist) { | |
265 | await sendCreateVideoPlaylist(playlistUpdated, t) | |
266 | } else { | |
267 | await sendUpdateVideoPlaylist(playlistUpdated, t) | |
268 | } | |
269 | ||
270 | logger.info('Video playlist %s updated.', videoPlaylistInstance.uuid) | |
271 | ||
272 | return playlistUpdated | |
273 | }) | |
274 | } catch (err) { | |
275 | logger.debug('Cannot update the video playlist.', { err }) | |
276 | ||
277 | // If the transaction is retried, sequelize will think the object has not changed | |
278 | // So we need to restore the previous fields | |
279 | await resetSequelizeInstance(videoPlaylistInstance) | |
280 | ||
281 | throw err | |
282 | } | |
283 | ||
284 | return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end() | |
285 | } | |
286 | ||
287 | async function removeVideoPlaylist (req: express.Request, res: express.Response) { | |
288 | const videoPlaylistInstance = res.locals.videoPlaylistSummary | |
289 | ||
290 | await sequelizeTypescript.transaction(async t => { | |
291 | await videoPlaylistInstance.destroy({ transaction: t }) | |
292 | ||
293 | await sendDeleteVideoPlaylist(videoPlaylistInstance, t) | |
294 | ||
295 | logger.info('Video playlist %s deleted.', videoPlaylistInstance.uuid) | |
296 | }) | |
297 | ||
298 | return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end() | |
299 | } | |
300 | ||
301 | async function addVideoInPlaylist (req: express.Request, res: express.Response) { | |
302 | const body: VideoPlaylistElementCreate = req.body | |
303 | const videoPlaylist = res.locals.videoPlaylistFull | |
304 | const video = res.locals.onlyVideo | |
305 | ||
306 | const playlistElement = await sequelizeTypescript.transaction(async t => { | |
307 | const position = await VideoPlaylistElementModel.getNextPositionOf(videoPlaylist.id, t) | |
308 | ||
309 | const playlistElement = await VideoPlaylistElementModel.create({ | |
310 | position, | |
311 | startTimestamp: body.startTimestamp || null, | |
312 | stopTimestamp: body.stopTimestamp || null, | |
313 | videoPlaylistId: videoPlaylist.id, | |
314 | videoId: video.id | |
315 | }, { transaction: t }) | |
316 | ||
317 | playlistElement.url = getLocalVideoPlaylistElementActivityPubUrl(videoPlaylist, playlistElement) | |
318 | await playlistElement.save({ transaction: t }) | |
319 | ||
320 | videoPlaylist.changed('updatedAt', true) | |
321 | await videoPlaylist.save({ transaction: t }) | |
322 | ||
323 | return playlistElement | |
324 | }) | |
325 | ||
326 | // If the user did not set a thumbnail, automatically take the video thumbnail | |
327 | if (videoPlaylist.hasThumbnail() === false || (videoPlaylist.hasGeneratedThumbnail() && playlistElement.position === 1)) { | |
328 | await generateThumbnailForPlaylist(videoPlaylist, video) | |
329 | } | |
330 | ||
331 | sendUpdateVideoPlaylist(videoPlaylist, undefined) | |
332 | .catch(err => logger.error('Cannot send video playlist update.', { err })) | |
333 | ||
334 | logger.info('Video added in playlist %s at position %d.', videoPlaylist.uuid, playlistElement.position) | |
335 | ||
336 | Hooks.runAction('action:api.video-playlist-element.created', { playlistElement, req, res }) | |
337 | ||
338 | return res.json({ | |
339 | videoPlaylistElement: { | |
340 | id: playlistElement.id | |
341 | } as VideoPlaylistElementCreateResult | |
342 | }) | |
343 | } | |
344 | ||
345 | async function updateVideoPlaylistElement (req: express.Request, res: express.Response) { | |
346 | const body: VideoPlaylistElementUpdate = req.body | |
347 | const videoPlaylist = res.locals.videoPlaylistFull | |
348 | const videoPlaylistElement = res.locals.videoPlaylistElement | |
349 | ||
350 | const playlistElement: VideoPlaylistElementModel = await sequelizeTypescript.transaction(async t => { | |
351 | if (body.startTimestamp !== undefined) videoPlaylistElement.startTimestamp = body.startTimestamp | |
352 | if (body.stopTimestamp !== undefined) videoPlaylistElement.stopTimestamp = body.stopTimestamp | |
353 | ||
354 | const element = await videoPlaylistElement.save({ transaction: t }) | |
355 | ||
356 | videoPlaylist.changed('updatedAt', true) | |
357 | await videoPlaylist.save({ transaction: t }) | |
358 | ||
359 | await sendUpdateVideoPlaylist(videoPlaylist, t) | |
360 | ||
361 | return element | |
362 | }) | |
363 | ||
364 | logger.info('Element of position %d of playlist %s updated.', playlistElement.position, videoPlaylist.uuid) | |
365 | ||
366 | return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end() | |
367 | } | |
368 | ||
369 | async function removeVideoFromPlaylist (req: express.Request, res: express.Response) { | |
370 | const videoPlaylistElement = res.locals.videoPlaylistElement | |
371 | const videoPlaylist = res.locals.videoPlaylistFull | |
372 | const positionToDelete = videoPlaylistElement.position | |
373 | ||
374 | await sequelizeTypescript.transaction(async t => { | |
375 | await videoPlaylistElement.destroy({ transaction: t }) | |
376 | ||
377 | // Decrease position of the next elements | |
378 | await VideoPlaylistElementModel.increasePositionOf(videoPlaylist.id, positionToDelete, -1, t) | |
379 | ||
380 | videoPlaylist.changed('updatedAt', true) | |
381 | await videoPlaylist.save({ transaction: t }) | |
382 | ||
383 | logger.info('Video playlist element %d of playlist %s deleted.', videoPlaylistElement.position, videoPlaylist.uuid) | |
384 | }) | |
385 | ||
386 | // Do we need to regenerate the default thumbnail? | |
387 | if (positionToDelete === 1 && videoPlaylist.hasGeneratedThumbnail()) { | |
388 | await regeneratePlaylistThumbnail(videoPlaylist) | |
389 | } | |
390 | ||
391 | sendUpdateVideoPlaylist(videoPlaylist, undefined) | |
392 | .catch(err => logger.error('Cannot send video playlist update.', { err })) | |
393 | ||
394 | return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end() | |
395 | } | |
396 | ||
397 | async function reorderVideosPlaylist (req: express.Request, res: express.Response) { | |
398 | const videoPlaylist = res.locals.videoPlaylistFull | |
399 | const body: VideoPlaylistReorder = req.body | |
400 | ||
401 | const start: number = body.startPosition | |
402 | const insertAfter: number = body.insertAfterPosition | |
403 | const reorderLength: number = body.reorderLength || 1 | |
404 | ||
405 | if (start === insertAfter) { | |
406 | return res.status(HttpStatusCode.NO_CONTENT_204).end() | |
407 | } | |
408 | ||
409 | // Example: if we reorder position 2 and insert after position 5 (so at position 6): # 1 2 3 4 5 6 7 8 9 | |
410 | // * increase position when position > 5 # 1 2 3 4 5 7 8 9 10 | |
411 | // * update position 2 -> position 6 # 1 3 4 5 6 7 8 9 10 | |
412 | // * decrease position when position position > 2 # 1 2 3 4 5 6 7 8 9 | |
413 | await sequelizeTypescript.transaction(async t => { | |
414 | const newPosition = insertAfter + 1 | |
415 | ||
416 | // Add space after the position when we want to insert our reordered elements (increase) | |
417 | await VideoPlaylistElementModel.increasePositionOf(videoPlaylist.id, newPosition, reorderLength, t) | |
418 | ||
419 | let oldPosition = start | |
420 | ||
421 | // We incremented the position of the elements we want to reorder | |
422 | if (start >= newPosition) oldPosition += reorderLength | |
423 | ||
424 | const endOldPosition = oldPosition + reorderLength - 1 | |
425 | // Insert our reordered elements in their place (update) | |
426 | await VideoPlaylistElementModel.reassignPositionOf({ | |
427 | videoPlaylistId: videoPlaylist.id, | |
428 | firstPosition: oldPosition, | |
429 | endPosition: endOldPosition, | |
430 | newPosition, | |
431 | transaction: t | |
432 | }) | |
433 | ||
434 | // Decrease positions of elements after the old position of our ordered elements (decrease) | |
435 | await VideoPlaylistElementModel.increasePositionOf(videoPlaylist.id, oldPosition, -reorderLength, t) | |
436 | ||
437 | videoPlaylist.changed('updatedAt', true) | |
438 | await videoPlaylist.save({ transaction: t }) | |
439 | ||
440 | await sendUpdateVideoPlaylist(videoPlaylist, t) | |
441 | }) | |
442 | ||
443 | // The first element changed | |
444 | if ((start === 1 || insertAfter === 0) && videoPlaylist.hasGeneratedThumbnail()) { | |
445 | await regeneratePlaylistThumbnail(videoPlaylist) | |
446 | } | |
447 | ||
448 | logger.info( | |
449 | 'Reordered playlist %s (inserted after position %d elements %d - %d).', | |
450 | videoPlaylist.uuid, insertAfter, start, start + reorderLength - 1 | |
451 | ) | |
452 | ||
453 | return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end() | |
454 | } | |
455 | ||
456 | async function getVideoPlaylistVideos (req: express.Request, res: express.Response) { | |
457 | const videoPlaylistInstance = res.locals.videoPlaylistSummary | |
458 | const user = res.locals.oauth ? res.locals.oauth.token.User : undefined | |
459 | const server = await getServerActor() | |
460 | ||
461 | const apiOptions = await Hooks.wrapObject({ | |
462 | start: req.query.start, | |
463 | count: req.query.count, | |
464 | videoPlaylistId: videoPlaylistInstance.id, | |
465 | serverAccount: server.Account, | |
466 | user | |
467 | }, 'filter:api.video-playlist.videos.list.params') | |
468 | ||
469 | const resultList = await Hooks.wrapPromiseFun( | |
470 | VideoPlaylistElementModel.listForApi, | |
471 | apiOptions, | |
472 | 'filter:api.video-playlist.videos.list.result' | |
473 | ) | |
474 | ||
475 | const options = { accountId: user?.Account?.id } | |
476 | return res.json(getFormattedObjects(resultList.data, resultList.total, options)) | |
477 | } | |
478 | ||
479 | async function regeneratePlaylistThumbnail (videoPlaylist: MVideoPlaylistThumbnail) { | |
480 | await videoPlaylist.Thumbnail.destroy() | |
481 | videoPlaylist.Thumbnail = null | |
482 | ||
483 | const firstElement = await VideoPlaylistElementModel.loadFirstElementWithVideoThumbnail(videoPlaylist.id) | |
484 | if (firstElement) await generateThumbnailForPlaylist(videoPlaylist, firstElement.Video) | |
485 | } | |
486 | ||
487 | async function generateThumbnailForPlaylist (videoPlaylist: MVideoPlaylistThumbnail, video: MVideoThumbnail) { | |
488 | logger.info('Generating default thumbnail to playlist %s.', videoPlaylist.url) | |
489 | ||
490 | const videoMiniature = video.getMiniature() | |
491 | if (!videoMiniature) { | |
492 | logger.info('Cannot generate thumbnail for playlist %s because video %s does not have any.', videoPlaylist.url, video.url) | |
493 | return | |
494 | } | |
495 | ||
496 | const inputPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, videoMiniature.filename) | |
497 | const thumbnailModel = await updatePlaylistMiniatureFromExisting({ | |
498 | inputPath, | |
499 | playlist: videoPlaylist, | |
500 | automaticallyGenerated: true, | |
501 | keepOriginal: true | |
502 | }) | |
503 | ||
504 | thumbnailModel.videoPlaylistId = videoPlaylist.id | |
505 | ||
506 | videoPlaylist.Thumbnail = await thumbnailModel.save() | |
507 | } |