aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video/video.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-10-19 14:58:28 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-10-19 14:58:28 +0200
commita96aed15188174c50885dda0df3164a67295e11f (patch)
tree7e3941324614e52a2f6ab7755224ab8ae8f8c19f /server/models/video/video.ts
parentbda65bdc9f3ce7d4b6e97cb9afaca25b71added3 (diff)
downloadPeerTube-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.ts75
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
53let Video: Sequelize.Model<VideoInstance, VideoAttributes> 53let Video: Sequelize.Model<VideoInstance, VideoAttributes>
54let getOriginalFile: VideoMethods.GetOriginalFile 54let getOriginalFile: VideoMethods.GetOriginalFile
55let generateMagnetUri: VideoMethods.GenerateMagnetUri
56let getVideoFilename: VideoMethods.GetVideoFilename 55let getVideoFilename: VideoMethods.GetVideoFilename
57let getThumbnailName: VideoMethods.GetThumbnailName 56let getThumbnailName: VideoMethods.GetThumbnailName
58let getThumbnailPath: VideoMethods.GetThumbnailPath 57let 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
429generateMagnetUri = 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
456getEmbedPath = function (this: VideoInstance) { 427getEmbedPath = 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
950function 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
965function getTorrentUrl (video: VideoInstance, videoFile: VideoFileInstance, baseUrlHttp: string) {
966 return baseUrlHttp + STATIC_PATHS.TORRENTS + video.getTorrentFileName(videoFile)
967}
968
969function getVideoFileUrl (video: VideoInstance, videoFile: VideoFileInstance, baseUrlHttp: string) {
970 return baseUrlHttp + STATIC_PATHS.WEBSEED + video.getVideoFilename(videoFile)
971}
972
973function 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}