aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-02-16 08:50:40 +0100
committerChocobozzz <chocobozzz@cpy.re>2021-02-16 10:36:44 +0100
commita35a22797c99f17924347da9a226068c3dbe4787 (patch)
treeaffb713929145f90f6bda8828ded3ac2f4f73b19 /server/lib/activitypub
parent6302d599cdf98b5a5363a2a1dcdc266447950191 (diff)
downloadPeerTube-a35a22797c99f17924347da9a226068c3dbe4787.tar.gz
PeerTube-a35a22797c99f17924347da9a226068c3dbe4787.tar.zst
PeerTube-a35a22797c99f17924347da9a226068c3dbe4787.zip
Remove previous thumbnail if needed
Diffstat (limited to 'server/lib/activitypub')
-rw-r--r--server/lib/activitypub/playlist.ts2
-rw-r--r--server/lib/activitypub/videos.ts154
2 files changed, 90 insertions, 66 deletions
diff --git a/server/lib/activitypub/playlist.ts b/server/lib/activitypub/playlist.ts
index 53298e968..d5a3ef7c8 100644
--- a/server/lib/activitypub/playlist.ts
+++ b/server/lib/activitypub/playlist.ts
@@ -103,7 +103,7 @@ async function createOrUpdateVideoPlaylist (playlistObject: PlaylistObject, byAc
103 103
104 if (playlistObject.icon) { 104 if (playlistObject.icon) {
105 try { 105 try {
106 const thumbnailModel = await createPlaylistMiniatureFromUrl(playlistObject.icon.url, refreshedPlaylist) 106 const thumbnailModel = await createPlaylistMiniatureFromUrl({ downloadUrl: playlistObject.icon.url, playlist: refreshedPlaylist })
107 await refreshedPlaylist.setAndSaveThumbnail(thumbnailModel, undefined) 107 await refreshedPlaylist.setAndSaveThumbnail(thumbnailModel, undefined)
108 } catch (err) { 108 } catch (err) {
109 logger.warn('Cannot generate thumbnail of %s.', playlistObject.id, { err }) 109 logger.warn('Cannot generate thumbnail of %s.', playlistObject.id, { err })
diff --git a/server/lib/activitypub/videos.ts b/server/lib/activitypub/videos.ts
index 201ef0302..66981f43f 100644
--- a/server/lib/activitypub/videos.ts
+++ b/server/lib/activitypub/videos.ts
@@ -313,7 +313,11 @@ async function updateVideoFromAP (options: {
313 let thumbnailModel: MThumbnail 313 let thumbnailModel: MThumbnail
314 314
315 try { 315 try {
316 thumbnailModel = await createVideoMiniatureFromUrl(getThumbnailFromIcons(videoObject).url, video, ThumbnailType.MINIATURE) 316 thumbnailModel = await createVideoMiniatureFromUrl({
317 downloadUrl: getThumbnailFromIcons(videoObject).url,
318 video,
319 type: ThumbnailType.MINIATURE
320 })
317 } catch (err) { 321 } catch (err) {
318 logger.warn('Cannot generate thumbnail of %s.', videoObject.id, { err }) 322 logger.warn('Cannot generate thumbnail of %s.', videoObject.id, { err })
319 } 323 }
@@ -362,7 +366,12 @@ async function updateVideoFromAP (options: {
362 366
363 if (videoUpdated.getPreview()) { 367 if (videoUpdated.getPreview()) {
364 const previewUrl = getPreviewUrl(getPreviewFromIcons(videoObject), video) 368 const previewUrl = getPreviewUrl(getPreviewFromIcons(videoObject), video)
365 const previewModel = createPlaceholderThumbnail(previewUrl, video, ThumbnailType.PREVIEW, PREVIEWS_SIZE) 369 const previewModel = createPlaceholderThumbnail({
370 fileUrl: previewUrl,
371 video,
372 type: ThumbnailType.PREVIEW,
373 size: PREVIEWS_SIZE
374 })
366 await videoUpdated.addAndSaveThumbnail(previewModel, t) 375 await videoUpdated.addAndSaveThumbnail(previewModel, t)
367 } 376 }
368 377
@@ -585,11 +594,14 @@ async function createVideo (videoObject: VideoObject, channel: MChannelAccountLi
585 const videoData = await videoActivityObjectToDBAttributes(channel, videoObject, videoObject.to) 594 const videoData = await videoActivityObjectToDBAttributes(channel, videoObject, videoObject.to)
586 const video = VideoModel.build(videoData) as MVideoThumbnail 595 const video = VideoModel.build(videoData) as MVideoThumbnail
587 596
588 const promiseThumbnail = createVideoMiniatureFromUrl(getThumbnailFromIcons(videoObject).url, video, ThumbnailType.MINIATURE) 597 const promiseThumbnail = createVideoMiniatureFromUrl({
589 .catch(err => { 598 downloadUrl: getThumbnailFromIcons(videoObject).url,
590 logger.error('Cannot create miniature from url.', { err }) 599 video,
591 return undefined 600 type: ThumbnailType.MINIATURE
592 }) 601 }).catch(err => {
602 logger.error('Cannot create miniature from url.', { err })
603 return undefined
604 })
593 605
594 let thumbnailModel: MThumbnail 606 let thumbnailModel: MThumbnail
595 if (waitThumbnail === true) { 607 if (waitThumbnail === true) {
@@ -597,81 +609,93 @@ async function createVideo (videoObject: VideoObject, channel: MChannelAccountLi
597 } 609 }
598 610
599 const { autoBlacklisted, videoCreated } = await sequelizeTypescript.transaction(async t => { 611 const { autoBlacklisted, videoCreated } = await sequelizeTypescript.transaction(async t => {
600 const sequelizeOptions = { transaction: t } 612 try {
613 const sequelizeOptions = { transaction: t }
601 614
602 const videoCreated = await video.save(sequelizeOptions) as MVideoFullLight 615 const videoCreated = await video.save(sequelizeOptions) as MVideoFullLight
603 videoCreated.VideoChannel = channel 616 videoCreated.VideoChannel = channel
604 617
605 if (thumbnailModel) await videoCreated.addAndSaveThumbnail(thumbnailModel, t) 618 if (thumbnailModel) await videoCreated.addAndSaveThumbnail(thumbnailModel, t)
606 619
607 const previewIcon = getPreviewFromIcons(videoObject) 620 const previewUrl = getPreviewUrl(getPreviewFromIcons(videoObject), videoCreated)
608 const previewUrl = getPreviewUrl(previewIcon, videoCreated) 621 const previewModel = createPlaceholderThumbnail({
609 const previewModel = createPlaceholderThumbnail(previewUrl, videoCreated, ThumbnailType.PREVIEW, PREVIEWS_SIZE) 622 fileUrl: previewUrl,
623 video: videoCreated,
624 type: ThumbnailType.PREVIEW,
625 size: PREVIEWS_SIZE
626 })
610 627
611 if (thumbnailModel) await videoCreated.addAndSaveThumbnail(previewModel, t) 628 if (thumbnailModel) await videoCreated.addAndSaveThumbnail(previewModel, t)
612 629
613 // Process files 630 // Process files
614 const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoCreated, videoObject.url) 631 const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoCreated, videoObject.url)
615 632
616 const videoFilePromises = videoFileAttributes.map(f => VideoFileModel.create(f, { transaction: t })) 633 const videoFilePromises = videoFileAttributes.map(f => VideoFileModel.create(f, { transaction: t }))
617 const videoFiles = await Promise.all(videoFilePromises) 634 const videoFiles = await Promise.all(videoFilePromises)
618 635
619 const streamingPlaylistsAttributes = streamingPlaylistActivityUrlToDBAttributes(videoCreated, videoObject, videoFiles) 636 const streamingPlaylistsAttributes = streamingPlaylistActivityUrlToDBAttributes(videoCreated, videoObject, videoFiles)
620 videoCreated.VideoStreamingPlaylists = [] 637 videoCreated.VideoStreamingPlaylists = []
621 638
622 for (const playlistAttributes of streamingPlaylistsAttributes) { 639 for (const playlistAttributes of streamingPlaylistsAttributes) {
623 const playlistModel = await VideoStreamingPlaylistModel.create(playlistAttributes, { transaction: t }) 640 const playlistModel = await VideoStreamingPlaylistModel.create(playlistAttributes, { transaction: t })
624 641
625 const playlistFiles = videoFileActivityUrlToDBAttributes(playlistModel, playlistAttributes.tagAPObject) 642 const playlistFiles = videoFileActivityUrlToDBAttributes(playlistModel, playlistAttributes.tagAPObject)
626 const videoFilePromises = playlistFiles.map(f => VideoFileModel.create(f, { transaction: t })) 643 const videoFilePromises = playlistFiles.map(f => VideoFileModel.create(f, { transaction: t }))
627 playlistModel.VideoFiles = await Promise.all(videoFilePromises) 644 playlistModel.VideoFiles = await Promise.all(videoFilePromises)
628 645
629 videoCreated.VideoStreamingPlaylists.push(playlistModel) 646 videoCreated.VideoStreamingPlaylists.push(playlistModel)
630 } 647 }
631 648
632 // Process tags 649 // Process tags
633 const tags = videoObject.tag 650 const tags = videoObject.tag
634 .filter(isAPHashTagObject) 651 .filter(isAPHashTagObject)
635 .map(t => t.name) 652 .map(t => t.name)
636 await setVideoTags({ video: videoCreated, tags, transaction: t }) 653 await setVideoTags({ video: videoCreated, tags, transaction: t })
637 654
638 // Process captions 655 // Process captions
639 const videoCaptionsPromises = videoObject.subtitleLanguage.map(c => { 656 const videoCaptionsPromises = videoObject.subtitleLanguage.map(c => {
640 const caption = new VideoCaptionModel({ 657 const caption = new VideoCaptionModel({
641 videoId: videoCreated.id, 658 videoId: videoCreated.id,
642 filename: VideoCaptionModel.generateCaptionName(c.identifier), 659 filename: VideoCaptionModel.generateCaptionName(c.identifier),
643 language: c.identifier, 660 language: c.identifier,
644 fileUrl: c.url 661 fileUrl: c.url
645 }) as MVideoCaption 662 }) as MVideoCaption
646 663
647 return VideoCaptionModel.insertOrReplaceLanguage(caption, t) 664 return VideoCaptionModel.insertOrReplaceLanguage(caption, t)
648 }) 665 })
649 await Promise.all(videoCaptionsPromises) 666 await Promise.all(videoCaptionsPromises)
650 667
651 videoCreated.VideoFiles = videoFiles 668 videoCreated.VideoFiles = videoFiles
652 669
653 if (videoCreated.isLive) { 670 if (videoCreated.isLive) {
654 const videoLive = new VideoLiveModel({ 671 const videoLive = new VideoLiveModel({
655 streamKey: null, 672 streamKey: null,
656 saveReplay: videoObject.liveSaveReplay, 673 saveReplay: videoObject.liveSaveReplay,
657 permanentLive: videoObject.permanentLive, 674 permanentLive: videoObject.permanentLive,
658 videoId: videoCreated.id 675 videoId: videoCreated.id
659 }) 676 })
660 677
661 videoCreated.VideoLive = await videoLive.save({ transaction: t }) 678 videoCreated.VideoLive = await videoLive.save({ transaction: t })
662 } 679 }
663 680
664 const autoBlacklisted = await autoBlacklistVideoIfNeeded({ 681 const autoBlacklisted = await autoBlacklistVideoIfNeeded({
665 video: videoCreated, 682 video: videoCreated,
666 user: undefined, 683 user: undefined,
667 isRemote: true, 684 isRemote: true,
668 isNew: true, 685 isNew: true,
669 transaction: t 686 transaction: t
670 }) 687 })
671 688
672 logger.info('Remote video with uuid %s inserted.', videoObject.uuid) 689 logger.info('Remote video with uuid %s inserted.', videoObject.uuid)
673 690
674 return { autoBlacklisted, videoCreated } 691 return { autoBlacklisted, videoCreated }
692 } catch (err) {
693 // FIXME: Use rollback hook when https://github.com/sequelize/sequelize/pull/13038 is released
694 // Remove thumbnail
695 if (thumbnailModel) await thumbnailModel.removeThumbnail()
696
697 throw err
698 }
675 }) 699 })
676 700
677 if (waitThumbnail === false) { 701 if (waitThumbnail === false) {