]>
Commit | Line | Data |
---|---|---|
0d0e8dd0 | 1 | import * as magnetUtil from 'magnet-uri' |
0d0e8dd0 | 2 | import { VideoTorrentObject } from '../../../shared' |
21e0727a | 3 | import { VideoChannelObject } from '../../../shared/models/activitypub/objects/video-channel-object' |
0d0e8dd0 | 4 | import { isVideoFileInfoHashValid } from '../../helpers/custom-validators/videos' |
0d0e8dd0 | 5 | import { VIDEO_MIMETYPE_EXT } from '../../initializers/constants' |
21e0727a | 6 | import { AccountInstance } from '../../models/account/account-interface' |
0d0e8dd0 C |
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' | |
20494f12 C |
10 | |
11 | function videoChannelActivityObjectToDBAttributes (videoChannelObject: VideoChannelObject, account: AccountInstance) { | |
12 | return { | |
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), | |
19 | remote: true, | |
20 | accountId: account.id | |
21 | } | |
22 | } | |
0d0e8dd0 | 23 | |
571389d4 C |
24 | async function videoActivityObjectToDBAttributes ( |
25 | videoChannel: VideoChannelInstance, | |
d7d5611c | 26 | videoObject: VideoTorrentObject |
571389d4 | 27 | ) { |
0d0e8dd0 C |
28 | const duration = videoObject.duration.replace(/[^\d]+/, '') |
29 | const videoData: VideoAttributes = { | |
30 | name: videoObject.name, | |
31 | uuid: videoObject.uuid, | |
32 | url: videoObject.id, | |
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), | |
efc32059 | 40 | createdAt: new Date(videoObject.published), |
0d0e8dd0 | 41 | // FIXME: updatedAt does not seems to be considered by Sequelize |
efc32059 | 42 | updatedAt: new Date(videoObject.updated), |
0d0e8dd0 C |
43 | views: videoObject.views, |
44 | likes: 0, | |
45 | dislikes: 0, | |
46 | // likes: videoToCreateData.likes, | |
47 | // dislikes: videoToCreateData.dislikes, | |
48 | remote: true, | |
49 | privacy: 1 | |
50 | // privacy: videoToCreateData.privacy | |
51 | } | |
52 | ||
53 | return videoData | |
54 | } | |
55 | ||
56 | function videoFileActivityUrlToDBAttributes (videoCreated: VideoInstance, videoObject: VideoTorrentObject) { | |
20494f12 C |
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/') | |
60 | }) | |
61 | ||
62 | if (fileUrls.length === 0) { | |
63 | throw new Error('Cannot find video files for ' + videoCreated.url) | |
64 | } | |
0d0e8dd0 C |
65 | |
66 | const attributes: VideoFileAttributes[] = [] | |
20494f12 | 67 | for (const fileUrl of fileUrls) { |
0d0e8dd0 | 68 | // Fetch associated magnet uri |
20494f12 C |
69 | const magnet = videoObject.url.find(u => { |
70 | return u.mimeType === 'application/x-bittorrent;x-scheme-handler/magnet' && u.width === fileUrl.width | |
71 | }) | |
72 | ||
73 | if (!magnet) throw new Error('Cannot find associated magnet uri for file ' + fileUrl.url) | |
0d0e8dd0 C |
74 | |
75 | const parsed = magnetUtil.decode(magnet.url) | |
76 | if (!parsed || isVideoFileInfoHashValid(parsed.infoHash) === false) throw new Error('Cannot parse magnet URI ' + magnet.url) | |
77 | ||
78 | const attribute = { | |
20494f12 | 79 | extname: VIDEO_MIMETYPE_EXT[fileUrl.mimeType], |
0d0e8dd0 | 80 | infoHash: parsed.infoHash, |
20494f12 C |
81 | resolution: fileUrl.width, |
82 | size: fileUrl.size, | |
0d0e8dd0 C |
83 | videoId: videoCreated.id |
84 | } | |
85 | attributes.push(attribute) | |
86 | } | |
87 | ||
88 | return attributes | |
89 | } | |
90 | ||
91 | // --------------------------------------------------------------------------- | |
92 | ||
93 | export { | |
94 | videoFileActivityUrlToDBAttributes, | |
20494f12 C |
95 | videoActivityObjectToDBAttributes, |
96 | videoChannelActivityObjectToDBAttributes | |
0d0e8dd0 | 97 | } |