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