import { isTestInstance } from '../../helpers/core-utils'
import * as Bluebird from 'bluebird'
import * as Sequelize from 'sequelize'
+import { VideoStreamingPlaylistModel } from '../video/video-streaming-playlist'
export enum ScopeNames {
WITH_VIDEO = 'WITH_VIDEO'
include: [
{
model: () => VideoFileModel,
- required: true,
+ required: false,
+ include: [
+ {
+ model: () => VideoModel,
+ required: true
+ }
+ ]
+ },
+ {
+ model: () => VideoStreamingPlaylistModel,
+ required: false,
include: [
{
model: () => VideoModel,
@BelongsTo(() => VideoFileModel, {
foreignKey: {
- allowNull: false
+ allowNull: true
},
onDelete: 'cascade'
})
VideoFile: VideoFileModel
+ @ForeignKey(() => VideoStreamingPlaylistModel)
+ @Column
+ videoStreamingPlaylistId: number
+
+ @BelongsTo(() => VideoStreamingPlaylistModel, {
+ foreignKey: {
+ allowNull: true
+ },
+ onDelete: 'cascade'
+ })
+ VideoStreamingPlaylist: VideoStreamingPlaylistModel
+
@ForeignKey(() => ActorModel)
@Column
actorId: number
static async removeFile (instance: VideoRedundancyModel) {
if (!instance.isOwned()) return
- const videoFile = await VideoFileModel.loadWithVideo(instance.videoFileId)
+ if (instance.videoFileId) {
+ const videoFile = await VideoFileModel.loadWithVideo(instance.videoFileId)
- const logIdentifier = `${videoFile.Video.uuid}-${videoFile.resolution}`
- logger.info('Removing duplicated video file %s.', logIdentifier)
+ const logIdentifier = `${videoFile.Video.uuid}-${videoFile.resolution}`
+ logger.info('Removing duplicated video file %s.', logIdentifier)
- videoFile.Video.removeFile(videoFile, true)
- .catch(err => logger.error('Cannot delete %s files.', logIdentifier, { err }))
+ videoFile.Video.removeFile(videoFile, true)
+ .catch(err => logger.error('Cannot delete %s files.', logIdentifier, { err }))
+ }
+
+ if (instance.videoStreamingPlaylistId) {
+ const videoStreamingPlaylist = await VideoStreamingPlaylistModel.loadWithVideo(instance.videoStreamingPlaylistId)
+
+ const videoUUID = videoStreamingPlaylist.Video.uuid
+ logger.info('Removing duplicated video streaming playlist %s.', videoUUID)
+
+ videoStreamingPlaylist.Video.removeStreamingPlaylist(true)
+ .catch(err => logger.error('Cannot delete video streaming playlist files of %s.', videoUUID, { err }))
+ }
return undefined
}
return VideoRedundancyModel.scope(ScopeNames.WITH_VIDEO).findOne(query)
}
+ static async loadLocalByStreamingPlaylistId (videoStreamingPlaylistId: number) {
+ const actor = await getServerActor()
+
+ const query = {
+ where: {
+ actorId: actor.id,
+ videoStreamingPlaylistId
+ }
+ }
+
+ return VideoRedundancyModel.scope(ScopeNames.WITH_VIDEO).findOne(query)
+ }
+
static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
const query = {
where: {
const ids = rows.map(r => r.id)
const id = sample(ids)
- return VideoModel.loadWithFile(id, undefined, !isTestInstance())
+ return VideoModel.loadWithFiles(id, undefined, !isTestInstance())
}
static async findMostViewToDuplicate (randomizedFactor: number) {
static async listLocalOfServer (serverId: number) {
const actor = await getServerActor()
-
- const query = {
- where: {
- actorId: actor.id
- },
+ const buildVideoInclude = () => ({
+ model: VideoModel,
+ required: true,
include: [
{
- model: VideoFileModel,
+ attributes: [],
+ model: VideoChannelModel.unscoped(),
required: true,
include: [
{
- model: VideoModel,
+ attributes: [],
+ model: ActorModel.unscoped(),
required: true,
- include: [
- {
- attributes: [],
- model: VideoChannelModel.unscoped(),
- required: true,
- include: [
- {
- attributes: [],
- model: ActorModel.unscoped(),
- required: true,
- where: {
- serverId
- }
- }
- ]
- }
- ]
+ where: {
+ serverId
+ }
}
]
}
]
+ })
+
+ const query = {
+ where: {
+ actorId: actor.id
+ },
+ include: [
+ {
+ model: VideoFileModel,
+ required: false,
+ include: [ buildVideoInclude() ]
+ },
+ {
+ model: VideoStreamingPlaylistModel,
+ required: false,
+ include: [ buildVideoInclude() ]
+ }
+ ]
}
return VideoRedundancyModel.findAll(query)
}))
}
+ getVideo () {
+ if (this.VideoFile) return this.VideoFile.Video
+
+ return this.VideoStreamingPlaylist.Video
+ }
+
isOwned () {
return !!this.strategy
}
toActivityPubObject (): CacheFileObject {
+ if (this.VideoStreamingPlaylist) {
+ return {
+ id: this.url,
+ type: 'CacheFile' as 'CacheFile',
+ object: this.VideoStreamingPlaylist.Video.url,
+ expires: this.expiresOn.toISOString(),
+ url: {
+ type: 'Link',
+ mimeType: 'application/x-mpegURL',
+ mediaType: 'application/x-mpegURL',
+ href: this.fileUrl
+ }
+ }
+ }
+
return {
id: this.url,
type: 'CacheFile' as 'CacheFile',
const notIn = Sequelize.literal(
'(' +
- `SELECT "videoFileId" FROM "videoRedundancy" WHERE "actorId" = ${actor.id}` +
+ `SELECT "videoFileId" FROM "videoRedundancy" WHERE "actorId" = ${actor.id} AND "videoFileId" IS NOT NULL` +
')'
)