]>
Commit | Line | Data |
---|---|---|
0d0e8dd0 | 1 | import * as magnetUtil from 'magnet-uri' |
54141398 | 2 | import { VideoTorrentObject } from '../../../../shared' |
3fd3ab2d C |
3 | import { VideoPrivacy } from '../../../../shared/models/videos' |
4 | import { doRequest } from '../../../helpers' | |
54141398 | 5 | import { isVideoFileInfoHashValid } from '../../../helpers/custom-validators/videos' |
3fd3ab2d | 6 | import { ACTIVITY_PUB, VIDEO_MIMETYPE_EXT } from '../../../initializers' |
3fd3ab2d C |
7 | import { VideoModel } from '../../../models/video/video' |
8 | import { VideoChannelModel } from '../../../models/video/video-channel' | |
3fd3ab2d | 9 | import { VideoShareModel } from '../../../models/video/video-share' |
50d6de9c | 10 | import { getOrCreateActorAndServerAndModel } from '../actor' |
0d0e8dd0 | 11 | |
571389d4 | 12 | async function videoActivityObjectToDBAttributes ( |
3fd3ab2d | 13 | videoChannel: VideoChannelModel, |
9a27cdc2 C |
14 | videoObject: VideoTorrentObject, |
15 | to: string[] = [], | |
16 | cc: string[] = [] | |
571389d4 | 17 | ) { |
9a27cdc2 C |
18 | let privacy = VideoPrivacy.PRIVATE |
19 | if (to.indexOf(ACTIVITY_PUB.PUBLIC) !== -1) privacy = VideoPrivacy.PUBLIC | |
20 | else if (cc.indexOf(ACTIVITY_PUB.PUBLIC) !== -1) privacy = VideoPrivacy.UNLISTED | |
21 | ||
0d0e8dd0 | 22 | const duration = videoObject.duration.replace(/[^\d]+/, '') |
40ff5707 C |
23 | let language = null |
24 | if (videoObject.language) { | |
25 | language = parseInt(videoObject.language.identifier, 10) | |
26 | } | |
27 | ||
f595d394 C |
28 | let category = null |
29 | if (videoObject.category) { | |
30 | category = parseInt(videoObject.category.identifier, 10) | |
31 | } | |
32 | ||
33 | let licence = null | |
34 | if (videoObject.licence) { | |
35 | licence = parseInt(videoObject.licence.identifier, 10) | |
36 | } | |
37 | ||
38 | let description = null | |
39 | if (videoObject.content) { | |
40 | description = videoObject.content | |
41 | } | |
42 | ||
3fd3ab2d | 43 | return { |
0d0e8dd0 C |
44 | name: videoObject.name, |
45 | uuid: videoObject.uuid, | |
46 | url: videoObject.id, | |
f595d394 C |
47 | category, |
48 | licence, | |
40ff5707 | 49 | language, |
f595d394 | 50 | description, |
0d0e8dd0 | 51 | nsfw: videoObject.nsfw, |
0d0e8dd0 C |
52 | channelId: videoChannel.id, |
53 | duration: parseInt(duration, 10), | |
efc32059 | 54 | createdAt: new Date(videoObject.published), |
0d0e8dd0 | 55 | // FIXME: updatedAt does not seems to be considered by Sequelize |
efc32059 | 56 | updatedAt: new Date(videoObject.updated), |
0d0e8dd0 C |
57 | views: videoObject.views, |
58 | likes: 0, | |
59 | dislikes: 0, | |
0d0e8dd0 | 60 | remote: true, |
9a27cdc2 | 61 | privacy |
0d0e8dd0 | 62 | } |
0d0e8dd0 C |
63 | } |
64 | ||
3fd3ab2d | 65 | function videoFileActivityUrlToDBAttributes (videoCreated: VideoModel, 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 | 74 | |
3fd3ab2d | 75 | const attributes = [] |
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 | ||
3fd3ab2d | 100 | async function addVideoShares (instance: VideoModel, shares: string[]) { |
4e50b6a1 C |
101 | for (const share of shares) { |
102 | // Fetch url | |
103 | const json = await doRequest({ | |
104 | uri: share, | |
105 | json: true | |
106 | }) | |
50d6de9c C |
107 | const actorUrl = json['actor'] |
108 | if (!actorUrl) continue | |
4e50b6a1 | 109 | |
50d6de9c | 110 | const actor = await getOrCreateActorAndServerAndModel(actorUrl) |
4e50b6a1 C |
111 | |
112 | const entry = { | |
50d6de9c | 113 | actorId: actor.id, |
4e50b6a1 C |
114 | videoId: instance.id |
115 | } | |
116 | ||
3fd3ab2d | 117 | await VideoShareModel.findOrCreate({ |
4e50b6a1 C |
118 | where: entry, |
119 | defaults: entry | |
120 | }) | |
121 | } | |
122 | } | |
123 | ||
0d0e8dd0 C |
124 | // --------------------------------------------------------------------------- |
125 | ||
126 | export { | |
127 | videoFileActivityUrlToDBAttributes, | |
20494f12 | 128 | videoActivityObjectToDBAttributes, |
4e50b6a1 | 129 | addVideoShares |
0d0e8dd0 | 130 | } |