aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/videos.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/activitypub/videos.ts')
-rw-r--r--server/lib/activitypub/videos.ts54
1 files changed, 38 insertions, 16 deletions
diff --git a/server/lib/activitypub/videos.ts b/server/lib/activitypub/videos.ts
index 66981f43f..a5f6537eb 100644
--- a/server/lib/activitypub/videos.ts
+++ b/server/lib/activitypub/videos.ts
@@ -1,7 +1,7 @@
1import * as Bluebird from 'bluebird' 1import * as Bluebird from 'bluebird'
2import { maxBy, minBy } from 'lodash' 2import { maxBy, minBy } from 'lodash'
3import * as magnetUtil from 'magnet-uri' 3import * as magnetUtil from 'magnet-uri'
4import { join } from 'path' 4import { basename, join } from 'path'
5import * as request from 'request' 5import * as request from 'request'
6import * as sequelize from 'sequelize' 6import * as sequelize from 'sequelize'
7import { VideoLiveModel } from '@server/models/video/video-live' 7import { VideoLiveModel } from '@server/models/video/video-live'
@@ -30,11 +30,11 @@ import { doRequest } from '../../helpers/requests'
30import { fetchVideoByUrl, getExtFromMimetype, VideoFetchByUrlType } from '../../helpers/video' 30import { fetchVideoByUrl, getExtFromMimetype, VideoFetchByUrlType } from '../../helpers/video'
31import { 31import {
32 ACTIVITY_PUB, 32 ACTIVITY_PUB,
33 LAZY_STATIC_PATHS,
33 MIMETYPES, 34 MIMETYPES,
34 P2P_MEDIA_LOADER_PEER_VERSION, 35 P2P_MEDIA_LOADER_PEER_VERSION,
35 PREVIEWS_SIZE, 36 PREVIEWS_SIZE,
36 REMOTE_SCHEME, 37 REMOTE_SCHEME,
37 STATIC_PATHS,
38 THUMBNAILS_SIZE 38 THUMBNAILS_SIZE
39} from '../../initializers/constants' 39} from '../../initializers/constants'
40import { sequelizeTypescript } from '../../initializers/database' 40import { sequelizeTypescript } from '../../initializers/database'
@@ -51,6 +51,8 @@ import {
51 MChannelDefault, 51 MChannelDefault,
52 MChannelId, 52 MChannelId,
53 MStreamingPlaylist, 53 MStreamingPlaylist,
54 MStreamingPlaylistFilesVideo,
55 MStreamingPlaylistVideo,
54 MVideo, 56 MVideo,
55 MVideoAccountLight, 57 MVideoAccountLight,
56 MVideoAccountLightBlacklistAllFiles, 58 MVideoAccountLightBlacklistAllFiles,
@@ -61,7 +63,8 @@ import {
61 MVideoFullLight, 63 MVideoFullLight,
62 MVideoId, 64 MVideoId,
63 MVideoImmutable, 65 MVideoImmutable,
64 MVideoThumbnail 66 MVideoThumbnail,
67 MVideoWithHost
65} from '../../types/models' 68} from '../../types/models'
66import { MThumbnail } from '../../types/models/video/thumbnail' 69import { MThumbnail } from '../../types/models/video/thumbnail'
67import { FilteredModelAttributes } from '../../types/sequelize' 70import { FilteredModelAttributes } from '../../types/sequelize'
@@ -72,6 +75,7 @@ import { PeerTubeSocket } from '../peertube-socket'
72import { createPlaceholderThumbnail, createVideoMiniatureFromUrl } from '../thumbnail' 75import { createPlaceholderThumbnail, createVideoMiniatureFromUrl } from '../thumbnail'
73import { setVideoTags } from '../video' 76import { setVideoTags } from '../video'
74import { autoBlacklistVideoIfNeeded } from '../video-blacklist' 77import { autoBlacklistVideoIfNeeded } from '../video-blacklist'
78import { generateTorrentFileName } from '../video-paths'
75import { getOrCreateActorAndServerAndModel } from './actor' 79import { getOrCreateActorAndServerAndModel } from './actor'
76import { crawlCollectionPage } from './crawl' 80import { crawlCollectionPage } from './crawl'
77import { sendCreateVideo, sendUpdateVideo } from './send' 81import { sendCreateVideo, sendUpdateVideo } from './send'
@@ -405,7 +409,8 @@ async function updateVideoFromAP (options: {
405 409
406 for (const playlistAttributes of streamingPlaylistAttributes) { 410 for (const playlistAttributes of streamingPlaylistAttributes) {
407 const streamingPlaylistModel = await VideoStreamingPlaylistModel.upsert(playlistAttributes, { returning: true, transaction: t }) 411 const streamingPlaylistModel = await VideoStreamingPlaylistModel.upsert(playlistAttributes, { returning: true, transaction: t })
408 .then(([ streamingPlaylist ]) => streamingPlaylist) 412 .then(([ streamingPlaylist ]) => streamingPlaylist as MStreamingPlaylistFilesVideo)
413 streamingPlaylistModel.Video = videoUpdated
409 414
410 const newVideoFiles: MVideoFile[] = videoFileActivityUrlToDBAttributes(streamingPlaylistModel, playlistAttributes.tagAPObject) 415 const newVideoFiles: MVideoFile[] = videoFileActivityUrlToDBAttributes(streamingPlaylistModel, playlistAttributes.tagAPObject)
411 .map(a => new VideoFileModel(a)) 416 .map(a => new VideoFileModel(a))
@@ -637,13 +642,14 @@ async function createVideo (videoObject: VideoObject, channel: MChannelAccountLi
637 videoCreated.VideoStreamingPlaylists = [] 642 videoCreated.VideoStreamingPlaylists = []
638 643
639 for (const playlistAttributes of streamingPlaylistsAttributes) { 644 for (const playlistAttributes of streamingPlaylistsAttributes) {
640 const playlistModel = await VideoStreamingPlaylistModel.create(playlistAttributes, { transaction: t }) 645 const playlist = await VideoStreamingPlaylistModel.create(playlistAttributes, { transaction: t }) as MStreamingPlaylistFilesVideo
646 playlist.Video = videoCreated
641 647
642 const playlistFiles = videoFileActivityUrlToDBAttributes(playlistModel, playlistAttributes.tagAPObject) 648 const playlistFiles = videoFileActivityUrlToDBAttributes(playlist, playlistAttributes.tagAPObject)
643 const videoFilePromises = playlistFiles.map(f => VideoFileModel.create(f, { transaction: t })) 649 const videoFilePromises = playlistFiles.map(f => VideoFileModel.create(f, { transaction: t }))
644 playlistModel.VideoFiles = await Promise.all(videoFilePromises) 650 playlist.VideoFiles = await Promise.all(videoFilePromises)
645 651
646 videoCreated.VideoStreamingPlaylists.push(playlistModel) 652 videoCreated.VideoStreamingPlaylists.push(playlist)
647 } 653 }
648 654
649 // Process tags 655 // Process tags
@@ -766,7 +772,7 @@ function videoActivityObjectToDBAttributes (videoChannel: MChannelId, videoObjec
766} 772}
767 773
768function videoFileActivityUrlToDBAttributes ( 774function videoFileActivityUrlToDBAttributes (
769 videoOrPlaylist: MVideo | MStreamingPlaylist, 775 videoOrPlaylist: MVideo | MStreamingPlaylistVideo,
770 urls: (ActivityTagObject | ActivityUrlObject)[] 776 urls: (ActivityTagObject | ActivityUrlObject)[]
771) { 777) {
772 const fileUrls = urls.filter(u => isAPVideoUrlObject(u)) as ActivityVideoUrlObject[] 778 const fileUrls = urls.filter(u => isAPVideoUrlObject(u)) as ActivityVideoUrlObject[]
@@ -786,6 +792,10 @@ function videoFileActivityUrlToDBAttributes (
786 throw new Error('Cannot parse magnet URI ' + magnet.href) 792 throw new Error('Cannot parse magnet URI ' + magnet.href)
787 } 793 }
788 794
795 const torrentUrl = Array.isArray(parsed.xs)
796 ? parsed.xs[0]
797 : parsed.xs
798
789 // Fetch associated metadata url, if any 799 // Fetch associated metadata url, if any
790 const metadata = urls.filter(isAPVideoFileMetadataObject) 800 const metadata = urls.filter(isAPVideoFileMetadataObject)
791 .find(u => { 801 .find(u => {
@@ -794,18 +804,30 @@ function videoFileActivityUrlToDBAttributes (
794 u.rel.includes(fileUrl.mediaType) 804 u.rel.includes(fileUrl.mediaType)
795 }) 805 })
796 806
797 const mediaType = fileUrl.mediaType 807 const extname = getExtFromMimetype(MIMETYPES.VIDEO.MIMETYPE_EXT, fileUrl.mediaType)
808 const resolution = fileUrl.height
809 const videoId = (videoOrPlaylist as MStreamingPlaylist).playlistUrl ? null : videoOrPlaylist.id
810 const videoStreamingPlaylistId = (videoOrPlaylist as MStreamingPlaylist).playlistUrl ? videoOrPlaylist.id : null
811
798 const attribute = { 812 const attribute = {
799 extname: getExtFromMimetype(MIMETYPES.VIDEO.MIMETYPE_EXT, mediaType), 813 extname,
800 infoHash: parsed.infoHash, 814 infoHash: parsed.infoHash,
801 resolution: fileUrl.height, 815 resolution,
802 size: fileUrl.size, 816 size: fileUrl.size,
803 fps: fileUrl.fps || -1, 817 fps: fileUrl.fps || -1,
804 metadataUrl: metadata?.href, 818 metadataUrl: metadata?.href,
805 819
820 // Use the name of the remote file because we don't proxify video file requests
821 filename: basename(fileUrl.href),
822 fileUrl: fileUrl.href,
823
824 torrentUrl,
825 // Use our own torrent name since we proxify torrent requests
826 torrentFilename: generateTorrentFileName(videoOrPlaylist, resolution),
827
806 // This is a video file owned by a video or by a streaming playlist 828 // This is a video file owned by a video or by a streaming playlist
807 videoId: (videoOrPlaylist as MStreamingPlaylist).playlistUrl ? null : videoOrPlaylist.id, 829 videoId,
808 videoStreamingPlaylistId: (videoOrPlaylist as MStreamingPlaylist).playlistUrl ? videoOrPlaylist.id : null 830 videoStreamingPlaylistId
809 } 831 }
810 832
811 attributes.push(attribute) 833 attributes.push(attribute)
@@ -862,8 +884,8 @@ function getPreviewFromIcons (videoObject: VideoObject) {
862 return maxBy(validIcons, 'width') 884 return maxBy(validIcons, 'width')
863} 885}
864 886
865function getPreviewUrl (previewIcon: ActivityIconObject, video: MVideoAccountLight) { 887function getPreviewUrl (previewIcon: ActivityIconObject, video: MVideoWithHost) {
866 return previewIcon 888 return previewIcon
867 ? previewIcon.url 889 ? previewIcon.url
868 : buildRemoteVideoBaseUrl(video, join(STATIC_PATHS.PREVIEWS, video.generatePreviewName())) 890 : buildRemoteVideoBaseUrl(video, join(LAZY_STATIC_PATHS.PREVIEWS, video.generatePreviewName()))
869} 891}