]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/video.ts
Fix live ending
[github/Chocobozzz/PeerTube.git] / server / lib / video.ts
CommitLineData
1ef65f4c 1import { Transaction } from 'sequelize/types'
d846d99c 2import { sequelizeTypescript } from '@server/initializers/database'
1ef65f4c 3import { TagModel } from '@server/models/video/tag'
c6c0fa6c
C
4import { VideoModel } from '@server/models/video/video'
5import { FilteredModelAttributes } from '@server/types'
d846d99c 6import { MTag, MThumbnail, MVideoTag, MVideoThumbnail, MVideoUUID } from '@server/types/models'
1ef65f4c 7import { ThumbnailType, VideoCreate, VideoPrivacy } from '@shared/models'
d846d99c
C
8import { federateVideoIfNeeded } from './activitypub/videos'
9import { Notifier } from './notifier'
1ef65f4c 10import { createVideoMiniatureFromExisting } from './thumbnail'
c6c0fa6c 11
1ef65f4c 12function buildLocalVideoFromReq (videoInfo: VideoCreate, channelId: number): FilteredModelAttributes<VideoModel> {
c6c0fa6c
C
13 return {
14 name: videoInfo.name,
15 remote: false,
16 category: videoInfo.category,
17 licence: videoInfo.licence,
18 language: videoInfo.language,
19 commentsEnabled: videoInfo.commentsEnabled !== false, // If the value is not "false", the default is "true"
20 downloadEnabled: videoInfo.downloadEnabled !== false,
21 waitTranscoding: videoInfo.waitTranscoding || false,
c6c0fa6c
C
22 nsfw: videoInfo.nsfw || false,
23 description: videoInfo.description,
24 support: videoInfo.support,
25 privacy: videoInfo.privacy || VideoPrivacy.PRIVATE,
c6c0fa6c
C
26 channelId: channelId,
27 originallyPublishedAt: videoInfo.originallyPublishedAt
28 }
29}
30
1ef65f4c
C
31async function buildVideoThumbnailsFromReq (options: {
32 video: MVideoThumbnail
33 files: { [fieldname: string]: Express.Multer.File[] } | Express.Multer.File[]
34 fallback: (type: ThumbnailType) => Promise<MThumbnail>
35 automaticallyGenerated?: boolean
36}) {
37 const { video, files, fallback, automaticallyGenerated } = options
38
39 const promises = [
40 {
41 type: ThumbnailType.MINIATURE,
42 fieldName: 'thumbnailfile'
43 },
44 {
45 type: ThumbnailType.PREVIEW,
46 fieldName: 'previewfile'
47 }
48 ].map(p => {
49 const fields = files?.[p.fieldName]
50
51 if (fields) {
52 return createVideoMiniatureFromExisting({
53 inputPath: fields[0].path,
54 video,
55 type: p.type,
56 automaticallyGenerated: automaticallyGenerated || false
57 })
58 }
59
60 return fallback(p.type)
61 })
62
63 return Promise.all(promises)
64}
65
66async function setVideoTags (options: {
67 video: MVideoTag
68 tags: string[]
69 transaction?: Transaction
70 defaultValue?: MTag[]
71}) {
72 const { video, tags, transaction, defaultValue } = options
73 // Set tags to the video
74 if (tags) {
75 const tagInstances = await TagModel.findOrCreateTags(tags, transaction)
76
77 await video.$set('Tags', tagInstances, { transaction })
78 video.Tags = tagInstances
79 } else {
80 video.Tags = defaultValue || []
81 }
82}
83
d846d99c
C
84async function publishAndFederateIfNeeded (video: MVideoUUID) {
85 const { videoDatabase, videoPublished } = await sequelizeTypescript.transaction(async t => {
86 // Maybe the video changed in database, refresh it
87 const videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t)
88 // Video does not exist anymore
89 if (!videoDatabase) return undefined
90
91 // We transcoded the video file in another format, now we can publish it
92 const videoPublished = await videoDatabase.publishIfNeededAndSave(t)
93
94 // If the video was not published, we consider it is a new one for other instances
95 await federateVideoIfNeeded(videoDatabase, videoPublished, t)
96
97 return { videoDatabase, videoPublished }
98 })
99
100 if (videoPublished) {
101 Notifier.Instance.notifyOnNewVideoIfNeeded(videoDatabase)
102 Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(videoDatabase)
103 }
104}
105
c6c0fa6c
C
106// ---------------------------------------------------------------------------
107
108export {
1ef65f4c 109 buildLocalVideoFromReq,
d846d99c 110 publishAndFederateIfNeeded,
1ef65f4c
C
111 buildVideoThumbnailsFromReq,
112 setVideoTags
c6c0fa6c 113}