]>
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 { buildNSFWFilter, 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.type | |
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 videoPlaylistFieldsSave = videoPlaylistInstance.toJSON() | |
214 | const videoPlaylistInfoToUpdate = req.body as VideoPlaylistUpdate | |
215 | ||
216 | const wasPrivatePlaylist = videoPlaylistInstance.privacy === VideoPlaylistPrivacy.PRIVATE | |
217 | const wasNotPrivatePlaylist = videoPlaylistInstance.privacy !== VideoPlaylistPrivacy.PRIVATE | |
218 | ||
219 | const thumbnailField = req.files['thumbnailfile'] | |
220 | const thumbnailModel = thumbnailField | |
221 | ? await updatePlaylistMiniatureFromExisting({ | |
222 | inputPath: thumbnailField[0].path, | |
223 | playlist: videoPlaylistInstance, | |
224 | automaticallyGenerated: false | |
225 | }) | |
226 | : undefined | |
227 | ||
228 | try { | |
229 | await sequelizeTypescript.transaction(async t => { | |
230 | const sequelizeOptions = { | |
231 | transaction: t | |
232 | } | |
233 | ||
234 | if (videoPlaylistInfoToUpdate.videoChannelId !== undefined) { | |
235 | if (videoPlaylistInfoToUpdate.videoChannelId === null) { | |
236 | videoPlaylistInstance.videoChannelId = null | |
237 | } else { | |
238 | const videoChannel = res.locals.videoChannel | |
239 | ||
240 | videoPlaylistInstance.videoChannelId = videoChannel.id | |
241 | videoPlaylistInstance.VideoChannel = videoChannel | |
242 | } | |
243 | } | |
244 | ||
245 | if (videoPlaylistInfoToUpdate.displayName !== undefined) videoPlaylistInstance.name = videoPlaylistInfoToUpdate.displayName | |
246 | if (videoPlaylistInfoToUpdate.description !== undefined) videoPlaylistInstance.description = videoPlaylistInfoToUpdate.description | |
247 | ||
248 | if (videoPlaylistInfoToUpdate.privacy !== undefined) { | |
249 | videoPlaylistInstance.privacy = forceNumber(videoPlaylistInfoToUpdate.privacy) | |
250 | ||
251 | if (wasNotPrivatePlaylist === true && videoPlaylistInstance.privacy === VideoPlaylistPrivacy.PRIVATE) { | |
252 | await sendDeleteVideoPlaylist(videoPlaylistInstance, t) | |
253 | } | |
254 | } | |
255 | ||
256 | const playlistUpdated = await videoPlaylistInstance.save(sequelizeOptions) | |
257 | ||
258 | if (thumbnailModel) { | |
259 | thumbnailModel.automaticallyGenerated = false | |
260 | await playlistUpdated.setAndSaveThumbnail(thumbnailModel, t) | |
261 | } | |
262 | ||
263 | const isNewPlaylist = wasPrivatePlaylist && playlistUpdated.privacy !== VideoPlaylistPrivacy.PRIVATE | |
264 | ||
265 | if (isNewPlaylist) { | |
266 | await sendCreateVideoPlaylist(playlistUpdated, t) | |
267 | } else { | |
268 | await sendUpdateVideoPlaylist(playlistUpdated, t) | |
269 | } | |
270 | ||
271 | logger.info('Video playlist %s updated.', videoPlaylistInstance.uuid) | |
272 | ||
273 | return playlistUpdated | |
274 | }) | |
275 | } catch (err) { | |
276 | logger.debug('Cannot update the video playlist.', { err }) | |
277 | ||
278 | // Force fields we want to update | |
279 | // If the transaction is retried, sequelize will think the object has not changed | |
280 | // So it will skip the SQL request, even if the last one was ROLLBACKed! | |
281 | resetSequelizeInstance(videoPlaylistInstance, videoPlaylistFieldsSave) | |
282 | ||
283 | throw err | |
284 | } | |
285 | ||
286 | return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end() | |
287 | } | |
288 | ||
289 | async function removeVideoPlaylist (req: express.Request, res: express.Response) { | |
290 | const videoPlaylistInstance = res.locals.videoPlaylistSummary | |
291 | ||
292 | await sequelizeTypescript.transaction(async t => { | |
293 | await videoPlaylistInstance.destroy({ transaction: t }) | |
294 | ||
295 | await sendDeleteVideoPlaylist(videoPlaylistInstance, t) | |
296 | ||
297 | logger.info('Video playlist %s deleted.', videoPlaylistInstance.uuid) | |
298 | }) | |
299 | ||
300 | return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end() | |
301 | } | |
302 | ||
303 | async function addVideoInPlaylist (req: express.Request, res: express.Response) { | |
304 | const body: VideoPlaylistElementCreate = req.body | |
305 | const videoPlaylist = res.locals.videoPlaylistFull | |
306 | const video = res.locals.onlyVideo | |
307 | ||
308 | const playlistElement = await sequelizeTypescript.transaction(async t => { | |
309 | const position = await VideoPlaylistElementModel.getNextPositionOf(videoPlaylist.id, t) | |
310 | ||
311 | const playlistElement = await VideoPlaylistElementModel.create({ | |
312 | position, | |
313 | startTimestamp: body.startTimestamp || null, | |
314 | stopTimestamp: body.stopTimestamp || null, | |
315 | videoPlaylistId: videoPlaylist.id, | |
316 | videoId: video.id | |
317 | }, { transaction: t }) | |
318 | ||
319 | playlistElement.url = getLocalVideoPlaylistElementActivityPubUrl(videoPlaylist, playlistElement) | |
320 | await playlistElement.save({ transaction: t }) | |
321 | ||
322 | videoPlaylist.changed('updatedAt', true) | |
323 | await videoPlaylist.save({ transaction: t }) | |
324 | ||
325 | return playlistElement | |
326 | }) | |
327 | ||
328 | // If the user did not set a thumbnail, automatically take the video thumbnail | |
329 | if (videoPlaylist.hasThumbnail() === false || (videoPlaylist.hasGeneratedThumbnail() && playlistElement.position === 1)) { | |
330 | await generateThumbnailForPlaylist(videoPlaylist, video) | |
331 | } | |
332 | ||
333 | sendUpdateVideoPlaylist(videoPlaylist, undefined) | |
334 | .catch(err => logger.error('Cannot send video playlist update.', { err })) | |
335 | ||
336 | logger.info('Video added in playlist %s at position %d.', videoPlaylist.uuid, playlistElement.position) | |
337 | ||
338 | Hooks.runAction('action:api.video-playlist-element.created', { playlistElement, req, res }) | |
339 | ||
340 | return res.json({ | |
341 | videoPlaylistElement: { | |
342 | id: playlistElement.id | |
343 | } as VideoPlaylistElementCreateResult | |
344 | }) | |
345 | } | |
346 | ||
347 | async function updateVideoPlaylistElement (req: express.Request, res: express.Response) { | |
348 | const body: VideoPlaylistElementUpdate = req.body | |
349 | const videoPlaylist = res.locals.videoPlaylistFull | |
350 | const videoPlaylistElement = res.locals.videoPlaylistElement | |
351 | ||
352 | const playlistElement: VideoPlaylistElementModel = await sequelizeTypescript.transaction(async t => { | |
353 | if (body.startTimestamp !== undefined) videoPlaylistElement.startTimestamp = body.startTimestamp | |
354 | if (body.stopTimestamp !== undefined) videoPlaylistElement.stopTimestamp = body.stopTimestamp | |
355 | ||
356 | const element = await videoPlaylistElement.save({ transaction: t }) | |
357 | ||
358 | videoPlaylist.changed('updatedAt', true) | |
359 | await videoPlaylist.save({ transaction: t }) | |
360 | ||
361 | await sendUpdateVideoPlaylist(videoPlaylist, t) | |
362 | ||
363 | return element | |
364 | }) | |
365 | ||
366 | logger.info('Element of position %d of playlist %s updated.', playlistElement.position, videoPlaylist.uuid) | |
367 | ||
368 | return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end() | |
369 | } | |
370 | ||
371 | async function removeVideoFromPlaylist (req: express.Request, res: express.Response) { | |
372 | const videoPlaylistElement = res.locals.videoPlaylistElement | |
373 | const videoPlaylist = res.locals.videoPlaylistFull | |
374 | const positionToDelete = videoPlaylistElement.position | |
375 | ||
376 | await sequelizeTypescript.transaction(async t => { | |
377 | await videoPlaylistElement.destroy({ transaction: t }) | |
378 | ||
379 | // Decrease position of the next elements | |
380 | await VideoPlaylistElementModel.increasePositionOf(videoPlaylist.id, positionToDelete, -1, t) | |
381 | ||
382 | videoPlaylist.changed('updatedAt', true) | |
383 | await videoPlaylist.save({ transaction: t }) | |
384 | ||
385 | logger.info('Video playlist element %d of playlist %s deleted.', videoPlaylistElement.position, videoPlaylist.uuid) | |
386 | }) | |
387 | ||
388 | // Do we need to regenerate the default thumbnail? | |
389 | if (positionToDelete === 1 && videoPlaylist.hasGeneratedThumbnail()) { | |
390 | await regeneratePlaylistThumbnail(videoPlaylist) | |
391 | } | |
392 | ||
393 | sendUpdateVideoPlaylist(videoPlaylist, undefined) | |
394 | .catch(err => logger.error('Cannot send video playlist update.', { err })) | |
395 | ||
396 | return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end() | |
397 | } | |
398 | ||
399 | async function reorderVideosPlaylist (req: express.Request, res: express.Response) { | |
400 | const videoPlaylist = res.locals.videoPlaylistFull | |
401 | const body: VideoPlaylistReorder = req.body | |
402 | ||
403 | const start: number = body.startPosition | |
404 | const insertAfter: number = body.insertAfterPosition | |
405 | const reorderLength: number = body.reorderLength || 1 | |
406 | ||
407 | if (start === insertAfter) { | |
408 | return res.status(HttpStatusCode.NO_CONTENT_204).end() | |
409 | } | |
410 | ||
411 | // Example: if we reorder position 2 and insert after position 5 (so at position 6): # 1 2 3 4 5 6 7 8 9 | |
412 | // * increase position when position > 5 # 1 2 3 4 5 7 8 9 10 | |
413 | // * update position 2 -> position 6 # 1 3 4 5 6 7 8 9 10 | |
414 | // * decrease position when position position > 2 # 1 2 3 4 5 6 7 8 9 | |
415 | await sequelizeTypescript.transaction(async t => { | |
416 | const newPosition = insertAfter + 1 | |
417 | ||
418 | // Add space after the position when we want to insert our reordered elements (increase) | |
419 | await VideoPlaylistElementModel.increasePositionOf(videoPlaylist.id, newPosition, reorderLength, t) | |
420 | ||
421 | let oldPosition = start | |
422 | ||
423 | // We incremented the position of the elements we want to reorder | |
424 | if (start >= newPosition) oldPosition += reorderLength | |
425 | ||
426 | const endOldPosition = oldPosition + reorderLength - 1 | |
427 | // Insert our reordered elements in their place (update) | |
428 | await VideoPlaylistElementModel.reassignPositionOf({ | |
429 | videoPlaylistId: videoPlaylist.id, | |
430 | firstPosition: oldPosition, | |
431 | endPosition: endOldPosition, | |
432 | newPosition, | |
433 | transaction: t | |
434 | }) | |
435 | ||
436 | // Decrease positions of elements after the old position of our ordered elements (decrease) | |
437 | await VideoPlaylistElementModel.increasePositionOf(videoPlaylist.id, oldPosition, -reorderLength, t) | |
438 | ||
439 | videoPlaylist.changed('updatedAt', true) | |
440 | await videoPlaylist.save({ transaction: t }) | |
441 | ||
442 | await sendUpdateVideoPlaylist(videoPlaylist, t) | |
443 | }) | |
444 | ||
445 | // The first element changed | |
446 | if ((start === 1 || insertAfter === 0) && videoPlaylist.hasGeneratedThumbnail()) { | |
447 | await regeneratePlaylistThumbnail(videoPlaylist) | |
448 | } | |
449 | ||
450 | logger.info( | |
451 | 'Reordered playlist %s (inserted after position %d elements %d - %d).', | |
452 | videoPlaylist.uuid, insertAfter, start, start + reorderLength - 1 | |
453 | ) | |
454 | ||
455 | return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end() | |
456 | } | |
457 | ||
458 | async function getVideoPlaylistVideos (req: express.Request, res: express.Response) { | |
459 | const videoPlaylistInstance = res.locals.videoPlaylistSummary | |
460 | const user = res.locals.oauth ? res.locals.oauth.token.User : undefined | |
461 | const server = await getServerActor() | |
462 | ||
463 | const apiOptions = await Hooks.wrapObject({ | |
464 | start: req.query.start, | |
465 | count: req.query.count, | |
466 | videoPlaylistId: videoPlaylistInstance.id, | |
467 | serverAccount: server.Account, | |
468 | user | |
469 | }, 'filter:api.video-playlist.videos.list.params') | |
470 | ||
471 | const resultList = await Hooks.wrapPromiseFun( | |
472 | VideoPlaylistElementModel.listForApi, | |
473 | apiOptions, | |
474 | 'filter:api.video-playlist.videos.list.result' | |
475 | ) | |
476 | ||
477 | const options = { | |
478 | displayNSFW: buildNSFWFilter(res, req.query.nsfw), | |
479 | accountId: user ? user.Account.id : undefined | |
480 | } | |
481 | return res.json(getFormattedObjects(resultList.data, resultList.total, options)) | |
482 | } | |
483 | ||
484 | async function regeneratePlaylistThumbnail (videoPlaylist: MVideoPlaylistThumbnail) { | |
485 | await videoPlaylist.Thumbnail.destroy() | |
486 | videoPlaylist.Thumbnail = null | |
487 | ||
488 | const firstElement = await VideoPlaylistElementModel.loadFirstElementWithVideoThumbnail(videoPlaylist.id) | |
489 | if (firstElement) await generateThumbnailForPlaylist(videoPlaylist, firstElement.Video) | |
490 | } | |
491 | ||
492 | async function generateThumbnailForPlaylist (videoPlaylist: MVideoPlaylistThumbnail, video: MVideoThumbnail) { | |
493 | logger.info('Generating default thumbnail to playlist %s.', videoPlaylist.url) | |
494 | ||
495 | const videoMiniature = video.getMiniature() | |
496 | if (!videoMiniature) { | |
497 | logger.info('Cannot generate thumbnail for playlist %s because video %s does not have any.', videoPlaylist.url, video.url) | |
498 | return | |
499 | } | |
500 | ||
501 | const inputPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, videoMiniature.filename) | |
502 | const thumbnailModel = await updatePlaylistMiniatureFromExisting({ | |
503 | inputPath, | |
504 | playlist: videoPlaylist, | |
505 | automaticallyGenerated: true, | |
506 | keepOriginal: true | |
507 | }) | |
508 | ||
509 | thumbnailModel.videoPlaylistId = videoPlaylist.id | |
510 | ||
511 | videoPlaylist.Thumbnail = await thumbnailModel.save() | |
512 | } |