Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

Also check download enabled in server side

Chocobozzzcf58d1ed11e2
diff --git a/packages/tests/src/api/videos/video-download-enabled.ts b/packages/tests/src/api/videos/video-download-enabled.ts
new file mode 100644
index 0000000000..50a1d87104
--- /dev/null
+++ b/packages/tests/src/api/videos/video-download-enabled.ts
@@ -0,0 +1,91 @@
+/* oxlint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
+
+import { getHLS } from '@peertube/peertube-core-utils'
+import { HttpStatusCode, PeerTubeProblemDocument } from '@peertube/peertube-models'
+import {
+ PeerTubeServer,
+ cleanupTests,
+ createSingleServer,
+ makeRawRequest,
+ setAccessTokensToServers,
+ setDefaultVideoChannel,
+ waitJobs
+} from '@peertube/peertube-server-commands'
+import { expect } from 'chai'
+
+describe('Test video downloads enabled/disabled', function () {
+ let server: PeerTubeServer
+ let videoUUID: string
+
+ before(async function () {
+ this.timeout(120000)
+
+ server = await createSingleServer(1)
+
+ await setAccessTokensToServers([ server ])
+ await setDefaultVideoChannel([ server ])
+ await server.config.enableTranscoding({ hls: true, webVideo: true, resolutions: 'min' })
+
+ const { uuid } = await server.videos.quickUpload({ name: 'download enabled' })
+ videoUUID = uuid
+
+ await waitJobs([ server ])
+ })
+
+ async function getVideoUrls () {
+ const video = await server.videos.get({ id: videoUUID })
+
+ return {
+ webVideoUrl: video.files[0].fileDownloadUrl,
+ torrentUrl: video.files[0].torrentDownloadUrl,
+ hlsVideoUrl: getHLS(video).files[0].fileDownloadUrl,
+ hlsTorrentUrl: getHLS(video).files[0].torrentDownloadUrl,
+ generateFileIds: [ getHLS(video).files[0].id ]
+ }
+ }
+
+ it('Should download video files when download is enabled', async function () {
+ const { webVideoUrl, torrentUrl, hlsVideoUrl, hlsTorrentUrl, generateFileIds } = await getVideoUrls()
+
+ await makeRawRequest({ url: webVideoUrl, expectedStatus: HttpStatusCode.OK_200 })
+ await makeRawRequest({ url: torrentUrl, expectedStatus: HttpStatusCode.OK_200 })
+ await makeRawRequest({ url: hlsVideoUrl, expectedStatus: HttpStatusCode.OK_200 })
+ await makeRawRequest({ url: hlsTorrentUrl, expectedStatus: HttpStatusCode.OK_200 })
+
+ await server.videos.generateDownload({ videoId: videoUUID, videoFileIds: generateFileIds, expectedStatus: HttpStatusCode.OK_200 })
+ })
+
+ it('Should not download video files when download is disabled', async function () {
+ await server.videos.update({ id: videoUUID, attributes: { downloadEnabled: false } })
+
+ const { webVideoUrl, torrentUrl, hlsVideoUrl, hlsTorrentUrl, generateFileIds } = await getVideoUrls()
+
+ for (const url of [ webVideoUrl, torrentUrl, hlsVideoUrl, hlsTorrentUrl ]) {
+ const res = await makeRawRequest({ url, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
+ expect((res.body as PeerTubeProblemDocument).detail).to.equal('Video download is disabled for this video')
+ }
+
+ await server.videos.generateDownload({
+ videoId: videoUUID,
+ videoFileIds: generateFileIds,
+ expectedStatus: HttpStatusCode.FORBIDDEN_403
+ })
+ })
+
+ it('Should download video files again when download is reenabled', async function () {
+ await server.videos.update({ id: videoUUID, attributes: { downloadEnabled: true } })
+
+ const { webVideoUrl, torrentUrl, hlsVideoUrl, hlsTorrentUrl, generateFileIds } = await getVideoUrls()
+
+ await makeRawRequest({ url: webVideoUrl, expectedStatus: HttpStatusCode.OK_200 })
+ await makeRawRequest({ url: torrentUrl, expectedStatus: HttpStatusCode.OK_200 })
+ await makeRawRequest({ url: hlsVideoUrl, expectedStatus: HttpStatusCode.OK_200 })
+ await makeRawRequest({ url: hlsTorrentUrl, expectedStatus: HttpStatusCode.OK_200 })
+
+ await server.videos.generateDownload({ videoId: videoUUID, videoFileIds: generateFileIds, expectedStatus: HttpStatusCode.OK_200 })
+ })
+
+ after(async function () {
+ await cleanupTests([ server ])
+ })
+})
diff --git a/server/core/controllers/download.ts b/server/core/controllers/download.ts
index 5a3c005866..244e73d3a5 100644
--- a/server/core/controllers/download.ts
+++ b/server/core/controllers/download.ts
@@ -118,6 +118,7 @@ async function downloadTorrent (req: express.Request, res: express.Response) {
const allowParameters = {
req,
res,
+ video,
torrentFilename: req.params.filename,
torrentPath: path,
downloadName: downloadFilename
@@ -365,24 +366,37 @@ type AllowedResult = {
errorMessage?: string
}
-function isTorrentDownloadAllowed (_object: {
+function isTorrentDownloadAllowed (object: {
+ video: MVideo
torrentPath: string
}): AllowedResult {
+ if (object.video.downloadEnabled !== true) {
+ return { allowed: false, errorMessage: 'Video download is disabled for this video' }
+ }
+
return { allowed: true }
}
-function isVideoDownloadAllowed (_object: {
+function isVideoDownloadAllowed (object: {
video: MVideo
videoFile: MVideoFile
streamingPlaylist?: MStreamingPlaylist
}): AllowedResult {
+ if (object.video.downloadEnabled !== true) {
+ return { allowed: false, errorMessage: 'Video download is disabled for this video' }
+ }
+
return { allowed: true }
}
-function isGeneratedVideoDownloadAllowed (_object: {
+function isGeneratedVideoDownloadAllowed (object: {
video: MVideo
videoFiles: MVideoFile[]
}): AllowedResult {
+ if (object.video.downloadEnabled !== true) {
+ return { allowed: false, errorMessage: 'Video download is disabled for this video' }
+ }
+
return { allowed: true }
}