PeerTube/server/models/video/sql/shared/video-file-query-builder.ts

70 lines
1.7 KiB
TypeScript
Raw Normal View History

2021-06-10 16:57:13 +02:00
import { Sequelize } from 'sequelize'
import { BuildVideoGetQueryOptions } from '../video-model-get-query-builder'
import { AbstractVideosModelQueryBuilder } from './abstract-videos-model-query-builder'
/**
*
* Fetch files (webtorrent and streaming playlist) according to a video
*
*/
export class VideoFileQueryBuilder extends AbstractVideosModelQueryBuilder {
protected attributes: { [key: string]: string }
constructor (protected readonly sequelize: Sequelize) {
super('get')
}
queryWebTorrentVideos (options: BuildVideoGetQueryOptions) {
this.buildWebtorrentFilesQuery(options)
2021-06-11 14:09:33 +02:00
return this.runQuery(options)
2021-06-10 16:57:13 +02:00
}
queryStreamingPlaylistVideos (options: BuildVideoGetQueryOptions) {
this.buildVideoStreamingPlaylistFilesQuery(options)
2021-06-11 14:09:33 +02:00
return this.runQuery(options)
2021-06-10 16:57:13 +02:00
}
private buildWebtorrentFilesQuery (options: BuildVideoGetQueryOptions) {
this.attributes = {
'"video"."id"': ''
}
2021-06-11 16:02:26 +02:00
this.includeWebtorrentFiles()
2021-06-10 16:57:13 +02:00
2021-06-11 14:09:33 +02:00
if (this.shouldIncludeRedundancies(options)) {
2021-06-10 16:57:13 +02:00
this.includeWebTorrentRedundancies()
}
2021-06-11 14:09:33 +02:00
this.whereId(options)
2021-06-10 16:57:13 +02:00
this.query = this.buildQuery()
}
private buildVideoStreamingPlaylistFilesQuery (options: BuildVideoGetQueryOptions) {
this.attributes = {
'"video"."id"': ''
}
2021-06-11 16:02:26 +02:00
this.includeStreamingPlaylistFiles()
2021-06-10 16:57:13 +02:00
2021-06-11 14:09:33 +02:00
if (this.shouldIncludeRedundancies(options)) {
2021-06-10 16:57:13 +02:00
this.includeStreamingPlaylistRedundancies()
}
2021-06-11 14:09:33 +02:00
this.whereId(options)
2021-06-10 16:57:13 +02:00
this.query = this.buildQuery()
}
private buildQuery () {
2021-06-11 11:27:45 +02:00
return `${this.buildSelect()} FROM "video" ${this.joins} ${this.where}`
2021-06-10 16:57:13 +02:00
}
2021-06-11 14:09:33 +02:00
private shouldIncludeRedundancies (options: BuildVideoGetQueryOptions) {
return options.type === 'api'
}
2021-06-10 16:57:13 +02:00
}