diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-10-19 14:58:28 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-10-19 14:58:28 +0200 |
commit | a96aed15188174c50885dda0df3164a67295e11f (patch) | |
tree | 7e3941324614e52a2f6ab7755224ab8ae8f8c19f /server/models/video/video.ts | |
parent | bda65bdc9f3ce7d4b6e97cb9afaca25b71added3 (diff) | |
download | PeerTube-a96aed15188174c50885dda0df3164a67295e11f.tar.gz PeerTube-a96aed15188174c50885dda0df3164a67295e11f.tar.zst PeerTube-a96aed15188174c50885dda0df3164a67295e11f.zip |
Add ability to download a video from direct link or torrent file
Diffstat (limited to 'server/models/video/video.ts')
-rw-r--r-- | server/models/video/video.ts | 75 |
1 files changed, 44 insertions, 31 deletions
diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 4bd8eb98f..0b1af4d21 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts | |||
@@ -52,7 +52,6 @@ import { PREVIEWS_SIZE } from '../../initializers/constants' | |||
52 | 52 | ||
53 | let Video: Sequelize.Model<VideoInstance, VideoAttributes> | 53 | let Video: Sequelize.Model<VideoInstance, VideoAttributes> |
54 | let getOriginalFile: VideoMethods.GetOriginalFile | 54 | let getOriginalFile: VideoMethods.GetOriginalFile |
55 | let generateMagnetUri: VideoMethods.GenerateMagnetUri | ||
56 | let getVideoFilename: VideoMethods.GetVideoFilename | 55 | let getVideoFilename: VideoMethods.GetVideoFilename |
57 | let getThumbnailName: VideoMethods.GetThumbnailName | 56 | let getThumbnailName: VideoMethods.GetThumbnailName |
58 | let getThumbnailPath: VideoMethods.GetThumbnailPath | 57 | let getThumbnailPath: VideoMethods.GetThumbnailPath |
@@ -254,7 +253,6 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da | |||
254 | createPreview, | 253 | createPreview, |
255 | createThumbnail, | 254 | createThumbnail, |
256 | createTorrentAndSetInfoHash, | 255 | createTorrentAndSetInfoHash, |
257 | generateMagnetUri, | ||
258 | getPreviewName, | 256 | getPreviewName, |
259 | getPreviewPath, | 257 | getPreviewPath, |
260 | getThumbnailName, | 258 | getThumbnailName, |
@@ -426,33 +424,6 @@ createTorrentAndSetInfoHash = function (this: VideoInstance, videoFile: VideoFil | |||
426 | }) | 424 | }) |
427 | } | 425 | } |
428 | 426 | ||
429 | generateMagnetUri = function (this: VideoInstance, videoFile: VideoFileInstance) { | ||
430 | let baseUrlHttp | ||
431 | let baseUrlWs | ||
432 | |||
433 | if (this.isOwned()) { | ||
434 | baseUrlHttp = CONFIG.WEBSERVER.URL | ||
435 | baseUrlWs = CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT | ||
436 | } else { | ||
437 | baseUrlHttp = REMOTE_SCHEME.HTTP + '://' + this.Author.Pod.host | ||
438 | baseUrlWs = REMOTE_SCHEME.WS + '://' + this.Author.Pod.host | ||
439 | } | ||
440 | |||
441 | const xs = baseUrlHttp + STATIC_PATHS.TORRENTS + this.getTorrentFileName(videoFile) | ||
442 | const announce = [ baseUrlWs + '/tracker/socket' ] | ||
443 | const urlList = [ baseUrlHttp + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile) ] | ||
444 | |||
445 | const magnetHash = { | ||
446 | xs, | ||
447 | announce, | ||
448 | urlList, | ||
449 | infoHash: videoFile.infoHash, | ||
450 | name: this.name | ||
451 | } | ||
452 | |||
453 | return magnetUtil.encode(magnetHash) | ||
454 | } | ||
455 | |||
456 | getEmbedPath = function (this: VideoInstance) { | 427 | getEmbedPath = function (this: VideoInstance) { |
457 | return '/videos/embed/' + this.uuid | 428 | return '/videos/embed/' + this.uuid |
458 | } | 429 | } |
@@ -516,6 +487,7 @@ toFormattedJSON = function (this: VideoInstance) { | |||
516 | } | 487 | } |
517 | 488 | ||
518 | // Format and sort video files | 489 | // Format and sort video files |
490 | const { baseUrlHttp, baseUrlWs } = getBaseUrls(this) | ||
519 | json.files = this.VideoFiles | 491 | json.files = this.VideoFiles |
520 | .map(videoFile => { | 492 | .map(videoFile => { |
521 | let resolutionLabel = videoFile.resolution + 'p' | 493 | let resolutionLabel = videoFile.resolution + 'p' |
@@ -523,8 +495,10 @@ toFormattedJSON = function (this: VideoInstance) { | |||
523 | const videoFileJson = { | 495 | const videoFileJson = { |
524 | resolution: videoFile.resolution, | 496 | resolution: videoFile.resolution, |
525 | resolutionLabel, | 497 | resolutionLabel, |
526 | magnetUri: this.generateMagnetUri(videoFile), | 498 | magnetUri: generateMagnetUri(this, videoFile, baseUrlHttp, baseUrlWs), |
527 | size: videoFile.size | 499 | size: videoFile.size, |
500 | torrentUrl: getTorrentUrl(this, videoFile, baseUrlHttp), | ||
501 | fileUrl: getVideoFileUrl(this, videoFile, baseUrlHttp) | ||
528 | } | 502 | } |
529 | 503 | ||
530 | return videoFileJson | 504 | return videoFileJson |
@@ -972,3 +946,42 @@ function createBaseVideosWhere () { | |||
972 | } | 946 | } |
973 | } | 947 | } |
974 | } | 948 | } |
949 | |||
950 | function getBaseUrls (video: VideoInstance) { | ||
951 | let baseUrlHttp | ||
952 | let baseUrlWs | ||
953 | |||
954 | if (video.isOwned()) { | ||
955 | baseUrlHttp = CONFIG.WEBSERVER.URL | ||
956 | baseUrlWs = CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT | ||
957 | } else { | ||
958 | baseUrlHttp = REMOTE_SCHEME.HTTP + '://' + video.Author.Pod.host | ||
959 | baseUrlWs = REMOTE_SCHEME.WS + '://' + video.Author.Pod.host | ||
960 | } | ||
961 | |||
962 | return { baseUrlHttp, baseUrlWs } | ||
963 | } | ||
964 | |||
965 | function getTorrentUrl (video: VideoInstance, videoFile: VideoFileInstance, baseUrlHttp: string) { | ||
966 | return baseUrlHttp + STATIC_PATHS.TORRENTS + video.getTorrentFileName(videoFile) | ||
967 | } | ||
968 | |||
969 | function getVideoFileUrl (video: VideoInstance, videoFile: VideoFileInstance, baseUrlHttp: string) { | ||
970 | return baseUrlHttp + STATIC_PATHS.WEBSEED + video.getVideoFilename(videoFile) | ||
971 | } | ||
972 | |||
973 | function generateMagnetUri (video: VideoInstance, videoFile: VideoFileInstance, baseUrlHttp: string, baseUrlWs: string) { | ||
974 | const xs = getTorrentUrl(video, videoFile, baseUrlHttp) | ||
975 | const announce = [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ] | ||
976 | const urlList = [ getVideoFileUrl(video, videoFile, baseUrlHttp) ] | ||
977 | |||
978 | const magnetHash = { | ||
979 | xs, | ||
980 | announce, | ||
981 | urlList, | ||
982 | infoHash: videoFile.infoHash, | ||
983 | name: video.name | ||
984 | } | ||
985 | |||
986 | return magnetUtil.encode(magnetHash) | ||
987 | } | ||