]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/video.ts
Save
[github/Chocobozzz/PeerTube.git] / server / lib / video.ts
1 import { Transaction } from 'sequelize/types'
2 import { TagModel } from '@server/models/video/tag'
3 import { VideoModel } from '@server/models/video/video'
4 import { FilteredModelAttributes } from '@server/types'
5 import { MTag, MThumbnail, MVideoTag, MVideoThumbnail } from '@server/types/models'
6 import { ThumbnailType, VideoCreate, VideoPrivacy } from '@shared/models'
7 import { createVideoMiniatureFromExisting } from './thumbnail'
8
9 function buildLocalVideoFromReq (videoInfo: VideoCreate, channelId: number): FilteredModelAttributes<VideoModel> {
10 return {
11 name: videoInfo.name,
12 remote: false,
13 category: videoInfo.category,
14 licence: videoInfo.licence,
15 language: videoInfo.language,
16 commentsEnabled: videoInfo.commentsEnabled !== false, // If the value is not "false", the default is "true"
17 downloadEnabled: videoInfo.downloadEnabled !== false,
18 waitTranscoding: videoInfo.waitTranscoding || false,
19 nsfw: videoInfo.nsfw || false,
20 description: videoInfo.description,
21 support: videoInfo.support,
22 privacy: videoInfo.privacy || VideoPrivacy.PRIVATE,
23 channelId: channelId,
24 originallyPublishedAt: videoInfo.originallyPublishedAt
25 }
26 }
27
28 async function buildVideoThumbnailsFromReq (options: {
29 video: MVideoThumbnail
30 files: { [fieldname: string]: Express.Multer.File[] } | Express.Multer.File[]
31 fallback: (type: ThumbnailType) => Promise<MThumbnail>
32 automaticallyGenerated?: boolean
33 }) {
34 const { video, files, fallback, automaticallyGenerated } = options
35
36 const promises = [
37 {
38 type: ThumbnailType.MINIATURE,
39 fieldName: 'thumbnailfile'
40 },
41 {
42 type: ThumbnailType.PREVIEW,
43 fieldName: 'previewfile'
44 }
45 ].map(p => {
46 const fields = files?.[p.fieldName]
47
48 if (fields) {
49 return createVideoMiniatureFromExisting({
50 inputPath: fields[0].path,
51 video,
52 type: p.type,
53 automaticallyGenerated: automaticallyGenerated || false
54 })
55 }
56
57 return fallback(p.type)
58 })
59
60 return Promise.all(promises)
61 }
62
63 async function setVideoTags (options: {
64 video: MVideoTag
65 tags: string[]
66 transaction?: Transaction
67 defaultValue?: MTag[]
68 }) {
69 const { video, tags, transaction, defaultValue } = options
70 // Set tags to the video
71 if (tags) {
72 const tagInstances = await TagModel.findOrCreateTags(tags, transaction)
73
74 await video.$set('Tags', tagInstances, { transaction })
75 video.Tags = tagInstances
76 } else {
77 video.Tags = defaultValue || []
78 }
79 }
80
81 // ---------------------------------------------------------------------------
82
83 export {
84 buildLocalVideoFromReq,
85 buildVideoThumbnailsFromReq,
86 setVideoTags
87 }