]>
Commit | Line | Data |
---|---|---|
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' | |
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 | } | |
23 | ||
24 | async function videoActivityObjectToDBAttributes ( | |
25 | videoChannel: VideoChannelInstance, | |
26 | videoObject: VideoTorrentObject | |
27 | ) { | |
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), | |
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, | |
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) { | |
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 | } | |
65 | ||
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 | |
71 | }) | |
72 | ||
73 | if (!magnet) throw new Error('Cannot find associated magnet uri for file ' + fileUrl.url) | |
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 = { | |
79 | extname: VIDEO_MIMETYPE_EXT[fileUrl.mimeType], | |
80 | infoHash: parsed.infoHash, | |
81 | resolution: fileUrl.width, | |
82 | size: fileUrl.size, | |
83 | videoId: videoCreated.id | |
84 | } | |
85 | attributes.push(attribute) | |
86 | } | |
87 | ||
88 | return attributes | |
89 | } | |
90 | ||
91 | // --------------------------------------------------------------------------- | |
92 | ||
93 | export { | |
94 | videoFileActivityUrlToDBAttributes, | |
95 | videoActivityObjectToDBAttributes, | |
96 | videoChannelActivityObjectToDBAttributes | |
97 | } |