diff options
Diffstat (limited to 'server/lib/activitypub/misc.ts')
-rw-r--r-- | server/lib/activitypub/misc.ts | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/server/lib/activitypub/misc.ts b/server/lib/activitypub/misc.ts new file mode 100644 index 000000000..05e77ebc3 --- /dev/null +++ b/server/lib/activitypub/misc.ts | |||
@@ -0,0 +1,77 @@ | |||
1 | import * as magnetUtil from 'magnet-uri' | ||
2 | import * as Sequelize from 'sequelize' | ||
3 | import { VideoTorrentObject } from '../../../shared' | ||
4 | import { isVideoFileInfoHashValid } from '../../helpers/custom-validators/videos' | ||
5 | import { database as db } from '../../initializers' | ||
6 | import { VIDEO_MIMETYPE_EXT } from '../../initializers/constants' | ||
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 | async function videoActivityObjectToDBAttributes (videoChannel: VideoChannelInstance, videoObject: VideoTorrentObject, t: Sequelize.Transaction) { | ||
12 | const videoFromDatabase = await db.Video.loadByUUIDOrURL(videoObject.uuid, videoObject.id, t) | ||
13 | if (videoFromDatabase) throw new Error('Video with this UUID/Url already exists.') | ||
14 | |||
15 | const duration = videoObject.duration.replace(/[^\d]+/, '') | ||
16 | const videoData: VideoAttributes = { | ||
17 | name: videoObject.name, | ||
18 | uuid: videoObject.uuid, | ||
19 | url: videoObject.id, | ||
20 | category: parseInt(videoObject.category.identifier, 10), | ||
21 | licence: parseInt(videoObject.licence.identifier, 10), | ||
22 | language: parseInt(videoObject.language.identifier, 10), | ||
23 | nsfw: videoObject.nsfw, | ||
24 | description: videoObject.content, | ||
25 | channelId: videoChannel.id, | ||
26 | duration: parseInt(duration, 10), | ||
27 | createdAt: videoObject.published, | ||
28 | // FIXME: updatedAt does not seems to be considered by Sequelize | ||
29 | updatedAt: videoObject.updated, | ||
30 | views: videoObject.views, | ||
31 | likes: 0, | ||
32 | dislikes: 0, | ||
33 | // likes: videoToCreateData.likes, | ||
34 | // dislikes: videoToCreateData.dislikes, | ||
35 | remote: true, | ||
36 | privacy: 1 | ||
37 | // privacy: videoToCreateData.privacy | ||
38 | } | ||
39 | |||
40 | return videoData | ||
41 | } | ||
42 | |||
43 | function videoFileActivityUrlToDBAttributes (videoCreated: VideoInstance, videoObject: VideoTorrentObject) { | ||
44 | const fileUrls = videoObject.url | ||
45 | .filter(u => Object.keys(VIDEO_MIMETYPE_EXT).indexOf(u.mimeType) !== -1) | ||
46 | |||
47 | const attributes: VideoFileAttributes[] = [] | ||
48 | for (const url of fileUrls) { | ||
49 | // Fetch associated magnet uri | ||
50 | const magnet = videoObject.url | ||
51 | .find(u => { | ||
52 | return u.mimeType === 'application/x-bittorrent;x-scheme-handler/magnet' && u.width === url.width | ||
53 | }) | ||
54 | if (!magnet) throw new Error('Cannot find associated magnet uri for file ' + url.url) | ||
55 | |||
56 | const parsed = magnetUtil.decode(magnet.url) | ||
57 | if (!parsed || isVideoFileInfoHashValid(parsed.infoHash) === false) throw new Error('Cannot parse magnet URI ' + magnet.url) | ||
58 | |||
59 | const attribute = { | ||
60 | extname: VIDEO_MIMETYPE_EXT[url.mimeType], | ||
61 | infoHash: parsed.infoHash, | ||
62 | resolution: url.width, | ||
63 | size: url.size, | ||
64 | videoId: videoCreated.id | ||
65 | } | ||
66 | attributes.push(attribute) | ||
67 | } | ||
68 | |||
69 | return attributes | ||
70 | } | ||
71 | |||
72 | // --------------------------------------------------------------------------- | ||
73 | |||
74 | export { | ||
75 | videoFileActivityUrlToDBAttributes, | ||
76 | videoActivityObjectToDBAttributes | ||
77 | } | ||