2017-11-10 07:34:45 -06:00
|
|
|
import * as magnetUtil from 'magnet-uri'
|
2017-11-20 02:43:39 -06:00
|
|
|
import { VideoTorrentObject } from '../../../../shared'
|
|
|
|
import { VideoChannelObject } from '../../../../shared/models/activitypub/objects/video-channel-object'
|
|
|
|
import { isVideoFileInfoHashValid } from '../../../helpers/custom-validators/videos'
|
|
|
|
import { ACTIVITY_PUB, VIDEO_MIMETYPE_EXT } from '../../../initializers/constants'
|
|
|
|
import { AccountInstance } from '../../../models/account/account-interface'
|
|
|
|
import { VideoChannelInstance } from '../../../models/video/video-channel-interface'
|
|
|
|
import { VideoFileAttributes } from '../../../models/video/video-file-interface'
|
|
|
|
import { VideoAttributes, VideoInstance } from '../../../models/video/video-interface'
|
|
|
|
import { VideoPrivacy } from '../../../../shared/models/videos/video-privacy.enum'
|
2017-11-16 08:22:39 -06:00
|
|
|
|
|
|
|
function videoChannelActivityObjectToDBAttributes (videoChannelObject: VideoChannelObject, account: AccountInstance) {
|
|
|
|
return {
|
|
|
|
name: videoChannelObject.name,
|
|
|
|
description: videoChannelObject.content,
|
|
|
|
uuid: videoChannelObject.uuid,
|
|
|
|
url: videoChannelObject.id,
|
|
|
|
createdAt: new Date(videoChannelObject.published),
|
|
|
|
updatedAt: new Date(videoChannelObject.updated),
|
|
|
|
remote: true,
|
|
|
|
accountId: account.id
|
|
|
|
}
|
|
|
|
}
|
2017-11-10 07:34:45 -06:00
|
|
|
|
2017-11-10 10:27:49 -06:00
|
|
|
async function videoActivityObjectToDBAttributes (
|
|
|
|
videoChannel: VideoChannelInstance,
|
2017-11-17 08:20:42 -06:00
|
|
|
videoObject: VideoTorrentObject,
|
|
|
|
to: string[] = [],
|
|
|
|
cc: string[] = []
|
2017-11-10 10:27:49 -06:00
|
|
|
) {
|
2017-11-17 08:20:42 -06:00
|
|
|
let privacy = VideoPrivacy.PRIVATE
|
|
|
|
if (to.indexOf(ACTIVITY_PUB.PUBLIC) !== -1) privacy = VideoPrivacy.PUBLIC
|
|
|
|
else if (cc.indexOf(ACTIVITY_PUB.PUBLIC) !== -1) privacy = VideoPrivacy.UNLISTED
|
|
|
|
|
2017-11-10 07:34:45 -06:00
|
|
|
const duration = videoObject.duration.replace(/[^\d]+/, '')
|
2017-11-22 09:25:03 -06:00
|
|
|
let language = null
|
|
|
|
if (videoObject.language) {
|
|
|
|
language = parseInt(videoObject.language.identifier, 10)
|
|
|
|
}
|
|
|
|
|
2017-11-10 07:34:45 -06:00
|
|
|
const videoData: VideoAttributes = {
|
|
|
|
name: videoObject.name,
|
|
|
|
uuid: videoObject.uuid,
|
|
|
|
url: videoObject.id,
|
|
|
|
category: parseInt(videoObject.category.identifier, 10),
|
|
|
|
licence: parseInt(videoObject.licence.identifier, 10),
|
2017-11-22 09:25:03 -06:00
|
|
|
language,
|
2017-11-10 07:34:45 -06:00
|
|
|
nsfw: videoObject.nsfw,
|
|
|
|
description: videoObject.content,
|
|
|
|
channelId: videoChannel.id,
|
|
|
|
duration: parseInt(duration, 10),
|
2017-11-16 04:08:25 -06:00
|
|
|
createdAt: new Date(videoObject.published),
|
2017-11-10 07:34:45 -06:00
|
|
|
// FIXME: updatedAt does not seems to be considered by Sequelize
|
2017-11-16 04:08:25 -06:00
|
|
|
updatedAt: new Date(videoObject.updated),
|
2017-11-10 07:34:45 -06:00
|
|
|
views: videoObject.views,
|
|
|
|
likes: 0,
|
|
|
|
dislikes: 0,
|
|
|
|
remote: true,
|
2017-11-17 08:20:42 -06:00
|
|
|
privacy
|
2017-11-10 07:34:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return videoData
|
|
|
|
}
|
|
|
|
|
|
|
|
function videoFileActivityUrlToDBAttributes (videoCreated: VideoInstance, videoObject: VideoTorrentObject) {
|
2017-11-16 08:22:39 -06:00
|
|
|
const mimeTypes = Object.keys(VIDEO_MIMETYPE_EXT)
|
|
|
|
const fileUrls = videoObject.url.filter(u => {
|
|
|
|
return mimeTypes.indexOf(u.mimeType) !== -1 && u.mimeType.startsWith('video/')
|
|
|
|
})
|
|
|
|
|
|
|
|
if (fileUrls.length === 0) {
|
|
|
|
throw new Error('Cannot find video files for ' + videoCreated.url)
|
|
|
|
}
|
2017-11-10 07:34:45 -06:00
|
|
|
|
|
|
|
const attributes: VideoFileAttributes[] = []
|
2017-11-16 08:22:39 -06:00
|
|
|
for (const fileUrl of fileUrls) {
|
2017-11-10 07:34:45 -06:00
|
|
|
// Fetch associated magnet uri
|
2017-11-16 08:22:39 -06:00
|
|
|
const magnet = videoObject.url.find(u => {
|
|
|
|
return u.mimeType === 'application/x-bittorrent;x-scheme-handler/magnet' && u.width === fileUrl.width
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!magnet) throw new Error('Cannot find associated magnet uri for file ' + fileUrl.url)
|
2017-11-10 07:34:45 -06:00
|
|
|
|
|
|
|
const parsed = magnetUtil.decode(magnet.url)
|
|
|
|
if (!parsed || isVideoFileInfoHashValid(parsed.infoHash) === false) throw new Error('Cannot parse magnet URI ' + magnet.url)
|
|
|
|
|
|
|
|
const attribute = {
|
2017-11-16 08:22:39 -06:00
|
|
|
extname: VIDEO_MIMETYPE_EXT[fileUrl.mimeType],
|
2017-11-10 07:34:45 -06:00
|
|
|
infoHash: parsed.infoHash,
|
2017-11-16 08:22:39 -06:00
|
|
|
resolution: fileUrl.width,
|
|
|
|
size: fileUrl.size,
|
2017-11-10 07:34:45 -06:00
|
|
|
videoId: videoCreated.id
|
|
|
|
}
|
|
|
|
attributes.push(attribute)
|
|
|
|
}
|
|
|
|
|
|
|
|
return attributes
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
videoFileActivityUrlToDBAttributes,
|
2017-11-16 08:22:39 -06:00
|
|
|
videoActivityObjectToDBAttributes,
|
|
|
|
videoChannelActivityObjectToDBAttributes
|
2017-11-10 07:34:45 -06:00
|
|
|
}
|