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 { 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'
11 function videoChannelActivityObjectToDBAttributes (videoChannelObject: VideoChannelObject, account: AccountInstance) {
13 name: videoChannelObject.name,
14 description: videoChannelObject.content,
15 uuid: videoChannelObject.uuid,
16 url: videoChannelObject.id,
17 createdAt: new Date(videoChannelObject.published),
18 updatedAt: new Date(videoChannelObject.updated),
24 async function videoActivityObjectToDBAttributes (
25 videoChannel: VideoChannelInstance,
26 videoObject: VideoTorrentObject
28 const duration = videoObject.duration.replace(/[^\d]+/, '')
29 const videoData: VideoAttributes = {
30 name: videoObject.name,
31 uuid: videoObject.uuid,
33 category: parseInt(videoObject.category.identifier, 10),
34 licence: parseInt(videoObject.licence.identifier, 10),
35 language: parseInt(videoObject.language.identifier, 10),
36 nsfw: videoObject.nsfw,
37 description: videoObject.content,
38 channelId: videoChannel.id,
39 duration: parseInt(duration, 10),
40 createdAt: new Date(videoObject.published),
41 // FIXME: updatedAt does not seems to be considered by Sequelize
42 updatedAt: new Date(videoObject.updated),
43 views: videoObject.views,
46 // likes: videoToCreateData.likes,
47 // dislikes: videoToCreateData.dislikes,
50 // privacy: videoToCreateData.privacy
56 function videoFileActivityUrlToDBAttributes (videoCreated: VideoInstance, videoObject: VideoTorrentObject) {
57 const mimeTypes = Object.keys(VIDEO_MIMETYPE_EXT)
58 const fileUrls = videoObject.url.filter(u => {
59 return mimeTypes.indexOf(u.mimeType) !== -1 && u.mimeType.startsWith('video/')
62 if (fileUrls.length === 0) {
63 throw new Error('Cannot find video files for ' + videoCreated.url)
66 const attributes: VideoFileAttributes[] = []
67 for (const fileUrl of fileUrls) {
68 // Fetch associated magnet uri
69 const magnet = videoObject.url.find(u => {
70 return u.mimeType === 'application/x-bittorrent;x-scheme-handler/magnet' && u.width === fileUrl.width
73 if (!magnet) throw new Error('Cannot find associated magnet uri for file ' + fileUrl.url)
75 const parsed = magnetUtil.decode(magnet.url)
76 if (!parsed || isVideoFileInfoHashValid(parsed.infoHash) === false) throw new Error('Cannot parse magnet URI ' + magnet.url)
79 extname: VIDEO_MIMETYPE_EXT[fileUrl.mimeType],
80 infoHash: parsed.infoHash,
81 resolution: fileUrl.width,
83 videoId: videoCreated.id
85 attributes.push(attribute)
91 // ---------------------------------------------------------------------------
94 videoFileActivityUrlToDBAttributes,
95 videoActivityObjectToDBAttributes,
96 videoChannelActivityObjectToDBAttributes