1 import * as magnetUtil from 'magnet-uri'
2 import { VideoTorrentObject } from '../../../../shared'
3 import { VideoChannelObject } from '../../../../shared/models/activitypub/objects/video-channel-object'
4 import { isVideoFileInfoHashValid } from '../../../helpers/custom-validators/videos'
5 import { ACTIVITY_PUB, VIDEO_MIMETYPE_EXT } from '../../../initializers/constants'
6 import { AccountInstance } from '../../../models/account/account-interface'
7 import { VideoChannelInstance } from '../../../models/video/video-channel-interface'
8 import { VideoFileAttributes } from '../../../models/video/video-file-interface'
9 import { VideoAttributes, VideoInstance } from '../../../models/video/video-interface'
10 import { VideoPrivacy } from '../../../../shared/models/videos/video-privacy.enum'
12 function videoChannelActivityObjectToDBAttributes (videoChannelObject: VideoChannelObject, account: AccountInstance) {
14 name: videoChannelObject.name,
15 description: videoChannelObject.content,
16 uuid: videoChannelObject.uuid,
17 url: videoChannelObject.id,
18 createdAt: new Date(videoChannelObject.published),
19 updatedAt: new Date(videoChannelObject.updated),
25 async function videoActivityObjectToDBAttributes (
26 videoChannel: VideoChannelInstance,
27 videoObject: VideoTorrentObject,
31 let privacy = VideoPrivacy.PRIVATE
32 if (to.indexOf(ACTIVITY_PUB.PUBLIC) !== -1) privacy = VideoPrivacy.PUBLIC
33 else if (cc.indexOf(ACTIVITY_PUB.PUBLIC) !== -1) privacy = VideoPrivacy.UNLISTED
35 const duration = videoObject.duration.replace(/[^\d]+/, '')
37 if (videoObject.language) {
38 language = parseInt(videoObject.language.identifier, 10)
41 const videoData: VideoAttributes = {
42 name: videoObject.name,
43 uuid: videoObject.uuid,
45 category: parseInt(videoObject.category.identifier, 10),
46 licence: parseInt(videoObject.licence.identifier, 10),
48 nsfw: videoObject.nsfw,
49 description: videoObject.content,
50 channelId: videoChannel.id,
51 duration: parseInt(duration, 10),
52 createdAt: new Date(videoObject.published),
53 // FIXME: updatedAt does not seems to be considered by Sequelize
54 updatedAt: new Date(videoObject.updated),
55 views: videoObject.views,
65 function videoFileActivityUrlToDBAttributes (videoCreated: VideoInstance, videoObject: VideoTorrentObject) {
66 const mimeTypes = Object.keys(VIDEO_MIMETYPE_EXT)
67 const fileUrls = videoObject.url.filter(u => {
68 return mimeTypes.indexOf(u.mimeType) !== -1 && u.mimeType.startsWith('video/')
71 if (fileUrls.length === 0) {
72 throw new Error('Cannot find video files for ' + videoCreated.url)
75 const attributes: VideoFileAttributes[] = []
76 for (const fileUrl of fileUrls) {
77 // Fetch associated magnet uri
78 const magnet = videoObject.url.find(u => {
79 return u.mimeType === 'application/x-bittorrent;x-scheme-handler/magnet' && u.width === fileUrl.width
82 if (!magnet) throw new Error('Cannot find associated magnet uri for file ' + fileUrl.url)
84 const parsed = magnetUtil.decode(magnet.url)
85 if (!parsed || isVideoFileInfoHashValid(parsed.infoHash) === false) throw new Error('Cannot parse magnet URI ' + magnet.url)
88 extname: VIDEO_MIMETYPE_EXT[fileUrl.mimeType],
89 infoHash: parsed.infoHash,
90 resolution: fileUrl.width,
92 videoId: videoCreated.id
94 attributes.push(attribute)
100 // ---------------------------------------------------------------------------
103 videoFileActivityUrlToDBAttributes,
104 videoActivityObjectToDBAttributes,
105 videoChannelActivityObjectToDBAttributes