aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
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
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')
-rw-r--r--server/models/video/video-interface.ts2
-rw-r--r--server/models/video/video.ts75
-rw-r--r--server/tests/api/multiple-pods.ts2
-rw-r--r--server/tests/api/single-pod.ts2
4 files changed, 48 insertions, 33 deletions
diff --git a/server/models/video/video-interface.ts b/server/models/video/video-interface.ts
index 1402df26a..86ce84dd9 100644
--- a/server/models/video/video-interface.ts
+++ b/server/models/video/video-interface.ts
@@ -18,7 +18,6 @@ export namespace VideoMethods {
18 export type ToFormattedJSON = (this: VideoInstance) => FormattedVideo 18 export type ToFormattedJSON = (this: VideoInstance) => FormattedVideo
19 19
20 export type GetOriginalFile = (this: VideoInstance) => VideoFileInstance 20 export type GetOriginalFile = (this: VideoInstance) => VideoFileInstance
21 export type GenerateMagnetUri = (this: VideoInstance, videoFile: VideoFileInstance) => string
22 export type GetTorrentFileName = (this: VideoInstance, videoFile: VideoFileInstance) => string 21 export type GetTorrentFileName = (this: VideoInstance, videoFile: VideoFileInstance) => string
23 export type GetVideoFilename = (this: VideoInstance, videoFile: VideoFileInstance) => string 22 export type GetVideoFilename = (this: VideoInstance, videoFile: VideoFileInstance) => string
24 export type CreatePreview = (this: VideoInstance, videoFile: VideoFileInstance) => Promise<string> 23 export type CreatePreview = (this: VideoInstance, videoFile: VideoFileInstance) => Promise<string>
@@ -108,7 +107,6 @@ export interface VideoInstance extends VideoClass, VideoAttributes, Sequelize.In
108 createThumbnail: VideoMethods.CreateThumbnail 107 createThumbnail: VideoMethods.CreateThumbnail
109 createTorrentAndSetInfoHash: VideoMethods.CreateTorrentAndSetInfoHash 108 createTorrentAndSetInfoHash: VideoMethods.CreateTorrentAndSetInfoHash
110 getOriginalFile: VideoMethods.GetOriginalFile 109 getOriginalFile: VideoMethods.GetOriginalFile
111 generateMagnetUri: VideoMethods.GenerateMagnetUri
112 getPreviewName: VideoMethods.GetPreviewName 110 getPreviewName: VideoMethods.GetPreviewName
113 getPreviewPath: VideoMethods.GetPreviewPath 111 getPreviewPath: VideoMethods.GetPreviewPath
114 getThumbnailName: VideoMethods.GetThumbnailName 112 getThumbnailName: VideoMethods.GetThumbnailName
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}
diff --git a/server/tests/api/multiple-pods.ts b/server/tests/api/multiple-pods.ts
index 6c11aace5..e0ccb3058 100644
--- a/server/tests/api/multiple-pods.ts
+++ b/server/tests/api/multiple-pods.ts
@@ -106,6 +106,8 @@ describe('Test multiple pods', function () {
106 const file = video.files[0] 106 const file = video.files[0]
107 const magnetUri = file.magnetUri 107 const magnetUri = file.magnetUri
108 expect(file.magnetUri).to.have.lengthOf.above(2) 108 expect(file.magnetUri).to.have.lengthOf.above(2)
109 expect(file.torrentUrl).to.equal(`http://${video.podHost}/static/torrents/${video.uuid}-${file.resolution}.torrent`)
110 expect(file.fileUrl).to.equal(`http://${video.podHost}/static/webseed/${video.uuid}-${file.resolution}.webm`)
109 expect(file.resolution).to.equal(720) 111 expect(file.resolution).to.equal(720)
110 expect(file.resolutionLabel).to.equal('720p') 112 expect(file.resolutionLabel).to.equal('720p')
111 expect(file.size).to.equal(572456) 113 expect(file.size).to.equal(572456)
diff --git a/server/tests/api/single-pod.ts b/server/tests/api/single-pod.ts
index 82bc51a3e..71017b2b3 100644
--- a/server/tests/api/single-pod.ts
+++ b/server/tests/api/single-pod.ts
@@ -127,6 +127,8 @@ describe('Test a single pod', function () {
127 const file = video.files[0] 127 const file = video.files[0]
128 const magnetUri = file.magnetUri 128 const magnetUri = file.magnetUri
129 expect(file.magnetUri).to.have.lengthOf.above(2) 129 expect(file.magnetUri).to.have.lengthOf.above(2)
130 expect(file.torrentUrl).to.equal(`${server.url}/static/torrents/${video.uuid}-${file.resolution}.torrent`)
131 expect(file.fileUrl).to.equal(`${server.url}/static/webseed/${video.uuid}-${file.resolution}.webm`)
130 expect(file.resolution).to.equal(720) 132 expect(file.resolution).to.equal(720)
131 expect(file.resolutionLabel).to.equal('720p') 133 expect(file.resolutionLabel).to.equal('720p')
132 expect(file.size).to.equal(218910) 134 expect(file.size).to.equal(218910)