]>
Commit | Line | Data |
---|---|---|
1bb4c9ab | 1 | import { CreationAttributes, Transaction } from 'sequelize/types' |
764b1a14 | 2 | import { deleteAllModels, filterNonExistingModels } from '@server/helpers/database-utils' |
46320694 | 3 | import { logger, LoggerTagsFn } from '@server/helpers/logger' |
91f8f8db | 4 | import { updatePlaceholderThumbnail, updateVideoMiniatureFromUrl } from '@server/lib/thumbnail' |
08a47c75 C |
5 | import { setVideoTags } from '@server/lib/video' |
6 | import { VideoCaptionModel } from '@server/models/video/video-caption' | |
7 | import { VideoFileModel } from '@server/models/video/video-file' | |
8 | import { VideoLiveModel } from '@server/models/video/video-live' | |
9 | import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist' | |
1bb4c9ab C |
10 | import { |
11 | MStreamingPlaylistFiles, | |
12 | MStreamingPlaylistFilesVideo, | |
13 | MThumbnail, | |
14 | MVideoCaption, | |
15 | MVideoFile, | |
16 | MVideoFullLight, | |
17 | MVideoThumbnail | |
18 | } from '@server/types/models' | |
08a47c75 | 19 | import { ActivityTagObject, ThumbnailType, VideoObject, VideoStreamingPlaylistType } from '@shared/models' |
136d7efd | 20 | import { getOrCreateAPActor } from '../../actors' |
7e98a7df | 21 | import { checkUrlsSameHost } from '../../url' |
08a47c75 C |
22 | import { |
23 | getCaptionAttributesFromObject, | |
24 | getFileAttributesFromUrl, | |
25 | getLiveAttributesFromObject, | |
26 | getPreviewFromIcons, | |
27 | getStreamingPlaylistAttributesFromObject, | |
28 | getTagsFromObject, | |
29 | getThumbnailFromIcons | |
30 | } from './object-to-model-attributes' | |
31 | import { getTrackerUrls, setVideoTrackers } from './trackers' | |
32 | ||
33 | export abstract class APVideoAbstractBuilder { | |
34 | protected abstract videoObject: VideoObject | |
46320694 | 35 | protected abstract lTags: LoggerTagsFn |
08a47c75 | 36 | |
c56faf0d C |
37 | protected async getOrCreateVideoChannelFromVideoObject () { |
38 | const channel = this.videoObject.attributedTo.find(a => a.type === 'Group') | |
39 | if (!channel) throw new Error('Cannot find associated video channel to video ' + this.videoObject.url) | |
40 | ||
41 | if (checkUrlsSameHost(channel.id, this.videoObject.id) !== true) { | |
42 | throw new Error(`Video channel url ${channel.id} does not have the same host than video object id ${this.videoObject.id}`) | |
43 | } | |
44 | ||
136d7efd | 45 | return getOrCreateAPActor(channel.id, 'all') |
c56faf0d C |
46 | } |
47 | ||
08a47c75 | 48 | protected tryToGenerateThumbnail (video: MVideoThumbnail): Promise<MThumbnail> { |
91f8f8db | 49 | return updateVideoMiniatureFromUrl({ |
08a47c75 C |
50 | downloadUrl: getThumbnailFromIcons(this.videoObject).url, |
51 | video, | |
52 | type: ThumbnailType.MINIATURE | |
53 | }).catch(err => { | |
908e6ead | 54 | logger.warn('Cannot generate thumbnail of %s.', this.videoObject.id, { err, ...this.lTags() }) |
08a47c75 C |
55 | |
56 | return undefined | |
57 | }) | |
58 | } | |
59 | ||
28dfb44b | 60 | protected async setPreview (video: MVideoFullLight, t?: Transaction) { |
08a47c75 C |
61 | // Don't fetch the preview that could be big, create a placeholder instead |
62 | const previewIcon = getPreviewFromIcons(this.videoObject) | |
63 | if (!previewIcon) return | |
64 | ||
91f8f8db | 65 | const previewModel = updatePlaceholderThumbnail({ |
08a47c75 C |
66 | fileUrl: previewIcon.url, |
67 | video, | |
68 | type: ThumbnailType.PREVIEW, | |
69 | size: previewIcon | |
70 | }) | |
71 | ||
72 | await video.addAndSaveThumbnail(previewModel, t) | |
73 | } | |
74 | ||
75 | protected async setTags (video: MVideoFullLight, t: Transaction) { | |
76 | const tags = getTagsFromObject(this.videoObject) | |
77 | await setVideoTags({ video, tags, transaction: t }) | |
78 | } | |
79 | ||
80 | protected async setTrackers (video: MVideoFullLight, t: Transaction) { | |
81 | const trackers = getTrackerUrls(this.videoObject, video) | |
82 | await setVideoTrackers({ video, trackers, transaction: t }) | |
83 | } | |
84 | ||
85 | protected async insertOrReplaceCaptions (video: MVideoFullLight, t: Transaction) { | |
57a0a9cd | 86 | const existingCaptions = await VideoCaptionModel.listVideoCaptions(video.id, t) |
08a47c75 | 87 | |
57a0a9cd C |
88 | let captionsToCreate = getCaptionAttributesFromObject(video, this.videoObject) |
89 | .map(a => new VideoCaptionModel(a) as MVideoCaption) | |
90 | ||
91 | for (const existingCaption of existingCaptions) { | |
92 | // Only keep captions that do not already exist | |
93 | const filtered = captionsToCreate.filter(c => !c.isEqual(existingCaption)) | |
94 | ||
95 | // This caption already exists, we don't need to destroy and create it | |
96 | if (filtered.length !== captionsToCreate.length) { | |
97 | captionsToCreate = filtered | |
98 | continue | |
99 | } | |
100 | ||
101 | // Destroy this caption that does not exist anymore | |
102 | await existingCaption.destroy({ transaction: t }) | |
103 | } | |
104 | ||
105 | for (const captionToCreate of captionsToCreate) { | |
106 | await captionToCreate.save({ transaction: t }) | |
107 | } | |
08a47c75 C |
108 | } |
109 | ||
110 | protected async insertOrReplaceLive (video: MVideoFullLight, transaction: Transaction) { | |
111 | const attributes = getLiveAttributesFromObject(video, this.videoObject) | |
112 | const [ videoLive ] = await VideoLiveModel.upsert(attributes, { transaction, returning: true }) | |
113 | ||
114 | video.VideoLive = videoLive | |
115 | } | |
116 | ||
117 | protected async setWebTorrentFiles (video: MVideoFullLight, t: Transaction) { | |
118 | const videoFileAttributes = getFileAttributesFromUrl(video, this.videoObject.url) | |
119 | const newVideoFiles = videoFileAttributes.map(a => new VideoFileModel(a)) | |
120 | ||
121 | // Remove video files that do not exist anymore | |
764b1a14 | 122 | await deleteAllModels(filterNonExistingModels(video.VideoFiles || [], newVideoFiles), t) |
08a47c75 C |
123 | |
124 | // Update or add other one | |
125 | const upsertTasks = newVideoFiles.map(f => VideoFileModel.customUpsert(f, 'video', t)) | |
126 | video.VideoFiles = await Promise.all(upsertTasks) | |
127 | } | |
128 | ||
129 | protected async setStreamingPlaylists (video: MVideoFullLight, t: Transaction) { | |
1e2fe802 | 130 | const streamingPlaylistAttributes = getStreamingPlaylistAttributesFromObject(video, this.videoObject) |
08a47c75 C |
131 | const newStreamingPlaylists = streamingPlaylistAttributes.map(a => new VideoStreamingPlaylistModel(a)) |
132 | ||
133 | // Remove video playlists that do not exist anymore | |
764b1a14 | 134 | await deleteAllModels(filterNonExistingModels(video.VideoStreamingPlaylists || [], newStreamingPlaylists), t) |
08a47c75 | 135 | |
1bb4c9ab | 136 | const oldPlaylists = video.VideoStreamingPlaylists |
08a47c75 C |
137 | video.VideoStreamingPlaylists = [] |
138 | ||
139 | for (const playlistAttributes of streamingPlaylistAttributes) { | |
08a47c75 C |
140 | const streamingPlaylistModel = await this.insertOrReplaceStreamingPlaylist(playlistAttributes, t) |
141 | streamingPlaylistModel.Video = video | |
142 | ||
1bb4c9ab | 143 | await this.setStreamingPlaylistFiles(oldPlaylists, streamingPlaylistModel, playlistAttributes.tagAPObject, t) |
08a47c75 C |
144 | |
145 | video.VideoStreamingPlaylists.push(streamingPlaylistModel) | |
146 | } | |
147 | } | |
148 | ||
1bb4c9ab | 149 | private async insertOrReplaceStreamingPlaylist (attributes: CreationAttributes<VideoStreamingPlaylistModel>, t: Transaction) { |
08a47c75 C |
150 | const [ streamingPlaylist ] = await VideoStreamingPlaylistModel.upsert(attributes, { returning: true, transaction: t }) |
151 | ||
152 | return streamingPlaylist as MStreamingPlaylistFilesVideo | |
153 | } | |
154 | ||
1bb4c9ab C |
155 | private getStreamingPlaylistFiles (oldPlaylists: MStreamingPlaylistFiles[], type: VideoStreamingPlaylistType) { |
156 | const playlist = oldPlaylists.find(s => s.type === type) | |
08a47c75 C |
157 | if (!playlist) return [] |
158 | ||
159 | return playlist.VideoFiles | |
160 | } | |
161 | ||
162 | private async setStreamingPlaylistFiles ( | |
1bb4c9ab | 163 | oldPlaylists: MStreamingPlaylistFiles[], |
08a47c75 C |
164 | playlistModel: MStreamingPlaylistFilesVideo, |
165 | tagObjects: ActivityTagObject[], | |
166 | t: Transaction | |
167 | ) { | |
1bb4c9ab | 168 | const oldStreamingPlaylistFiles = this.getStreamingPlaylistFiles(oldPlaylists || [], playlistModel.type) |
08a47c75 C |
169 | |
170 | const newVideoFiles: MVideoFile[] = getFileAttributesFromUrl(playlistModel, tagObjects).map(a => new VideoFileModel(a)) | |
171 | ||
764b1a14 | 172 | await deleteAllModels(filterNonExistingModels(oldStreamingPlaylistFiles, newVideoFiles), t) |
08a47c75 C |
173 | |
174 | // Update or add other one | |
175 | const upsertTasks = newVideoFiles.map(f => VideoFileModel.customUpsert(f, 'streaming-playlist', t)) | |
176 | playlistModel.VideoFiles = await Promise.all(upsertTasks) | |
177 | } | |
178 | } |