]>
Commit | Line | Data |
---|---|---|
0d0e8dd0 | 1 | import * as magnetUtil from 'magnet-uri' |
54141398 C |
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' | |
20494f12 C |
11 | |
12 | function videoChannelActivityObjectToDBAttributes (videoChannelObject: VideoChannelObject, account: AccountInstance) { | |
13 | return { | |
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), | |
20 | remote: true, | |
21 | accountId: account.id | |
22 | } | |
23 | } | |
0d0e8dd0 | 24 | |
571389d4 C |
25 | async function videoActivityObjectToDBAttributes ( |
26 | videoChannel: VideoChannelInstance, | |
9a27cdc2 C |
27 | videoObject: VideoTorrentObject, |
28 | to: string[] = [], | |
29 | cc: string[] = [] | |
571389d4 | 30 | ) { |
9a27cdc2 C |
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 | |
34 | ||
0d0e8dd0 | 35 | const duration = videoObject.duration.replace(/[^\d]+/, '') |
40ff5707 C |
36 | let language = null |
37 | if (videoObject.language) { | |
38 | language = parseInt(videoObject.language.identifier, 10) | |
39 | } | |
40 | ||
0d0e8dd0 C |
41 | const videoData: VideoAttributes = { |
42 | name: videoObject.name, | |
43 | uuid: videoObject.uuid, | |
44 | url: videoObject.id, | |
45 | category: parseInt(videoObject.category.identifier, 10), | |
46 | licence: parseInt(videoObject.licence.identifier, 10), | |
40ff5707 | 47 | language, |
0d0e8dd0 C |
48 | nsfw: videoObject.nsfw, |
49 | description: videoObject.content, | |
50 | channelId: videoChannel.id, | |
51 | duration: parseInt(duration, 10), | |
efc32059 | 52 | createdAt: new Date(videoObject.published), |
0d0e8dd0 | 53 | // FIXME: updatedAt does not seems to be considered by Sequelize |
efc32059 | 54 | updatedAt: new Date(videoObject.updated), |
0d0e8dd0 C |
55 | views: videoObject.views, |
56 | likes: 0, | |
57 | dislikes: 0, | |
0d0e8dd0 | 58 | remote: true, |
9a27cdc2 | 59 | privacy |
0d0e8dd0 C |
60 | } |
61 | ||
62 | return videoData | |
63 | } | |
64 | ||
65 | function videoFileActivityUrlToDBAttributes (videoCreated: VideoInstance, videoObject: VideoTorrentObject) { | |
20494f12 C |
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/') | |
69 | }) | |
70 | ||
71 | if (fileUrls.length === 0) { | |
72 | throw new Error('Cannot find video files for ' + videoCreated.url) | |
73 | } | |
0d0e8dd0 C |
74 | |
75 | const attributes: VideoFileAttributes[] = [] | |
20494f12 | 76 | for (const fileUrl of fileUrls) { |
0d0e8dd0 | 77 | // Fetch associated magnet uri |
20494f12 C |
78 | const magnet = videoObject.url.find(u => { |
79 | return u.mimeType === 'application/x-bittorrent;x-scheme-handler/magnet' && u.width === fileUrl.width | |
80 | }) | |
81 | ||
82 | if (!magnet) throw new Error('Cannot find associated magnet uri for file ' + fileUrl.url) | |
0d0e8dd0 C |
83 | |
84 | const parsed = magnetUtil.decode(magnet.url) | |
85 | if (!parsed || isVideoFileInfoHashValid(parsed.infoHash) === false) throw new Error('Cannot parse magnet URI ' + magnet.url) | |
86 | ||
87 | const attribute = { | |
20494f12 | 88 | extname: VIDEO_MIMETYPE_EXT[fileUrl.mimeType], |
0d0e8dd0 | 89 | infoHash: parsed.infoHash, |
20494f12 C |
90 | resolution: fileUrl.width, |
91 | size: fileUrl.size, | |
0d0e8dd0 C |
92 | videoId: videoCreated.id |
93 | } | |
94 | attributes.push(attribute) | |
95 | } | |
96 | ||
97 | return attributes | |
98 | } | |
99 | ||
100 | // --------------------------------------------------------------------------- | |
101 | ||
102 | export { | |
103 | videoFileActivityUrlToDBAttributes, | |
20494f12 C |
104 | videoActivityObjectToDBAttributes, |
105 | videoChannelActivityObjectToDBAttributes | |
0d0e8dd0 | 106 | } |