]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/video.ts
add title attribute for exact view counters (#3365)
[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'
053aed43 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
97969c4e
C
84async function publishAndFederateIfNeeded (video: MVideoUUID, wasLive = false) {
85 const result = await sequelizeTypescript.transaction(async t => {
d846d99c
C
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
97969c4e
C
95 // Live videos are always federated, so it's not a new video
96 await federateVideoIfNeeded(videoDatabase, !wasLive && videoPublished, t)
d846d99c
C
97
98 return { videoDatabase, videoPublished }
99 })
100
97969c4e
C
101 if (result?.videoPublished) {
102 Notifier.Instance.notifyOnNewVideoIfNeeded(result.videoDatabase)
103 Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(result.videoDatabase)
d846d99c
C
104 }
105}
106
c6c0fa6c
C
107// ---------------------------------------------------------------------------
108
109export {
1ef65f4c 110 buildLocalVideoFromReq,
d846d99c 111 publishAndFederateIfNeeded,
1ef65f4c
C
112 buildVideoThumbnailsFromReq,
113 setVideoTags
c6c0fa6c 114}