Skip to content

Commit

Permalink
Add ability to sort videos by file size
Browse files Browse the repository at this point in the history
  • Loading branch information
Chocobozzz committed Mar 8, 2024
1 parent b080ccc commit 4596ec2
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ <h1>
</th>
<th scope="col" i18n>Video</th>
<th scope="col" i18n>Info</th>
<th scope="col" i18n>Files</th>
<th scope="col" i18n [ngbTooltip]="sortTooltip" container="body" pSortableColumn="localVideoFilesSize">Files <p-sortIcon field="localVideoFilesSize"></p-sortIcon></th>
<th scope="col" style="width: 150px;" i18n [ngbTooltip]="sortTooltip" container="body" pSortableColumn="publishedAt">Published <p-sortIcon field="publishedAt"></p-sortIcon></th>
</tr>
</ng-template>
Expand Down
28 changes: 22 additions & 6 deletions packages/tests/src/api/videos/multiple-servers.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import { expect } from 'chai'
import request from 'supertest'
import { wait } from '@peertube/peertube-core-utils'
import { HttpStatusCode, VideoCommentThreadTree, VideoPrivacy } from '@peertube/peertube-models'
import { buildAbsoluteFixturePath } from '@peertube/peertube-node-utils'
import {
PeerTubeServer,
cleanupTests,
createMultipleServers,
doubleFollow,
makeGetRequest,
PeerTubeServer,
setAccessTokensToServers,
setDefaultAccountAvatar,
setDefaultChannelAvatar,
waitJobs
} from '@peertube/peertube-server-commands'
import { testImageGeneratedByFFmpeg, dateIsValid } from '@tests/shared/checks.js'
import { dateIsValid, testImageGeneratedByFFmpeg } from '@tests/shared/checks.js'
import { checkTmpIsEmpty } from '@tests/shared/directories.js'
import { completeVideoCheck, saveVideoInServers, checkVideoFilesWereRemoved } from '@tests/shared/videos.js'
import { checkVideoFilesWereRemoved, completeVideoCheck, saveVideoInServers } from '@tests/shared/videos.js'
import { checkWebTorrentWorks } from '@tests/shared/webtorrent.js'
import { expect } from 'chai'
import request from 'supertest'

describe('Test multiple servers', function () {
let servers: PeerTubeServer[] = []
Expand Down Expand Up @@ -367,7 +367,8 @@ describe('Test multiple servers', function () {
})
})

describe('It should list local videos', function () {
describe('Local videos listing', function () {

it('Should list only local videos on server 1', async function () {
const { data, total } = await servers[0].videos.list({ isLocal: true })

Expand Down Expand Up @@ -397,6 +398,21 @@ describe('Test multiple servers', function () {
})
})

describe('All videos listing', function () {

it('Should list and sort by "localVideoFilesSize"', async function () {
const { data, total } = await servers[2].videos.list({ sort: '-localVideoFilesSize' })

expect(total).to.equal(4)
expect(data).to.be.an('array')
expect(data.length).to.equal(4)
expect(data[0].name).to.equal('my super name for server 3')
expect(data[1].name).to.equal('my super name for server 3-2')
expect(data[2].isLocal).to.be.false
expect(data[3].isLocal).to.be.false
})
})

describe('Should seed the uploaded video', function () {

it('Should add the file 1 by asking server 3', async function () {
Expand Down
14 changes: 13 additions & 1 deletion server/core/initializers/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,19 @@ const SORTABLE_COLUMNS = {
RUNNER_REGISTRATION_TOKENS: [ 'createdAt' ],
RUNNER_JOBS: [ 'updatedAt', 'createdAt', 'priority', 'state', 'progress' ],

VIDEOS: [ 'name', 'duration', 'createdAt', 'publishedAt', 'originallyPublishedAt', 'views', 'likes', 'trending', 'hot', 'best' ],
VIDEOS: [
'name',
'duration',
'createdAt',
'publishedAt',
'originallyPublishedAt',
'views',
'likes',
'trending',
'hot',
'best',
'localVideoFilesSize'
],

// Don't forget to update peertube-search-index with the same values
VIDEOS_SEARCH: [ 'name', 'duration', 'createdAt', 'publishedAt', 'originallyPublishedAt', 'views', 'likes', 'match' ],
Expand Down
19 changes: 19 additions & 0 deletions server/core/models/video/sql/video/videos-id-list-query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,23 @@ export class VideosIdListQueryBuilder extends AbstractRunQuery {
this.attributes.push('COALESCE("video"."originallyPublishedAt", "video"."publishedAt") AS "publishedAtForOrder"')
}

if (sort === '-localVideoFilesSize' || sort === 'localVideoFilesSize') {
this.attributes.push(
'(' +
'CASE ' +
'WHEN "video"."remote" IS TRUE THEN 0 ' + // Consider remote videos with size of 0
'ELSE (' +
'(SELECT COALESCE(SUM(size), 0) FROM "videoFile" WHERE "videoFile"."videoId" = "video"."id")' +
' + ' +
'(SELECT COALESCE(SUM(size), 0) FROM "videoFile" ' +
'INNER JOIN "videoStreamingPlaylist" ON "videoStreamingPlaylist"."id" = "videoFile"."videoStreamingPlaylistId" ' +
'AND "videoStreamingPlaylist"."videoId" = "video"."id"' +
')' +
') END' +
') AS "localVideoFilesSize"'
)
}

this.sort = this.buildOrder(sort)
}

Expand All @@ -712,6 +729,8 @@ export class VideosIdListQueryBuilder extends AbstractRunQuery {
firstSort = '"similarity"'
} else if (field === 'originallyPublishedAt') {
firstSort = '"publishedAtForOrder"'
} else if (field === 'localVideoFilesSize') {
firstSort = '"localVideoFilesSize"'
} else if (field.includes('.')) {
firstSort = field
} else {
Expand Down

0 comments on commit 4596ec2

Please sign in to comment.