2024-06-03 16:37:44 +02:00
|
|
|
import { type FileStorageType, type VideoSource } from '@peertube/peertube-models'
|
2024-03-15 15:47:18 +01:00
|
|
|
import { STATIC_DOWNLOAD_PATHS, WEBSERVER } from '@server/initializers/constants.js'
|
2024-06-03 16:37:44 +02:00
|
|
|
import { MVideoSource } from '@server/types/models/video/video-source.js'
|
2024-03-15 15:47:18 +01:00
|
|
|
import { join } from 'path'
|
2023-07-19 16:02:49 +02:00
|
|
|
import { Transaction } from 'sequelize'
|
2024-03-15 15:47:18 +01:00
|
|
|
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Table, UpdatedAt } from 'sequelize-typescript'
|
2024-06-03 16:37:44 +02:00
|
|
|
import { SequelizeModel, doesExist, getSort } from '../shared/index.js'
|
2024-03-15 15:47:18 +01:00
|
|
|
import { getResolutionLabel } from './formatter/video-api-format.js'
|
2023-07-31 14:34:36 +02:00
|
|
|
import { VideoModel } from './video.js'
|
2022-06-21 15:31:25 +02:00
|
|
|
|
|
|
|
@Table({
|
|
|
|
tableName: 'videoSource',
|
|
|
|
indexes: [
|
|
|
|
{
|
2023-07-19 16:02:49 +02:00
|
|
|
fields: [ 'videoId' ]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ { name: 'createdAt', order: 'DESC' } ]
|
2024-03-15 15:47:18 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'keptOriginalFilename' ],
|
|
|
|
unique: true
|
2022-06-21 15:31:25 +02:00
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
2024-02-22 10:12:04 +01:00
|
|
|
export class VideoSourceModel extends SequelizeModel<VideoSourceModel> {
|
2022-06-21 15:31:25 +02:00
|
|
|
@CreatedAt
|
|
|
|
createdAt: Date
|
|
|
|
|
|
|
|
@UpdatedAt
|
|
|
|
updatedAt: Date
|
|
|
|
|
|
|
|
@AllowNull(false)
|
|
|
|
@Column
|
2024-03-15 15:47:18 +01:00
|
|
|
inputFilename: string
|
|
|
|
|
|
|
|
@AllowNull(true)
|
|
|
|
@Column
|
|
|
|
keptOriginalFilename: string
|
|
|
|
|
|
|
|
@AllowNull(true)
|
|
|
|
@Column
|
|
|
|
resolution: number
|
|
|
|
|
|
|
|
@AllowNull(true)
|
|
|
|
@Column
|
|
|
|
width: number
|
|
|
|
|
|
|
|
@AllowNull(true)
|
|
|
|
@Column
|
|
|
|
height: number
|
|
|
|
|
|
|
|
@AllowNull(true)
|
|
|
|
@Column
|
|
|
|
fps: number
|
|
|
|
|
|
|
|
@AllowNull(true)
|
|
|
|
@Column(DataType.BIGINT)
|
|
|
|
size: number
|
|
|
|
|
|
|
|
@AllowNull(true)
|
|
|
|
@Column(DataType.JSONB)
|
|
|
|
metadata: any
|
|
|
|
|
|
|
|
@AllowNull(true)
|
|
|
|
@Column
|
|
|
|
storage: FileStorageType
|
|
|
|
|
|
|
|
@AllowNull(true)
|
|
|
|
@Column
|
|
|
|
fileUrl: string
|
2022-06-21 15:31:25 +02:00
|
|
|
|
|
|
|
@ForeignKey(() => VideoModel)
|
|
|
|
@Column
|
|
|
|
videoId: number
|
|
|
|
|
2023-07-19 16:02:49 +02:00
|
|
|
@BelongsTo(() => VideoModel, {
|
|
|
|
foreignKey: {
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
2023-07-31 14:34:36 +02:00
|
|
|
Video: Awaited<VideoModel>
|
2022-06-21 15:31:25 +02:00
|
|
|
|
2023-07-19 16:02:49 +02:00
|
|
|
static loadLatest (videoId: number, transaction?: Transaction) {
|
2024-03-15 15:47:18 +01:00
|
|
|
return VideoSourceModel.findOne<MVideoSource>({
|
2023-07-19 16:02:49 +02:00
|
|
|
where: { videoId },
|
|
|
|
order: getSort('-createdAt'),
|
|
|
|
transaction
|
|
|
|
})
|
2022-06-21 15:31:25 +02:00
|
|
|
}
|
|
|
|
|
2024-03-15 15:47:18 +01:00
|
|
|
static loadByKeptOriginalFilename (keptOriginalFilename: string) {
|
|
|
|
return VideoSourceModel.findOne<MVideoSource>({
|
|
|
|
where: { keptOriginalFilename }
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
static listAll (videoId: number, transaction?: Transaction) {
|
|
|
|
return VideoSourceModel.findAll<MVideoSource>({
|
|
|
|
where: { videoId },
|
|
|
|
transaction
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-06-03 16:37:44 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static async doesOwnedFileExist (filename: string, storage: FileStorageType) {
|
|
|
|
const query = 'SELECT 1 FROM "videoSource" ' +
|
|
|
|
'INNER JOIN "video" ON "video"."id" = "videoSource"."videoId" AND "video"."remote" IS FALSE ' +
|
|
|
|
`WHERE "keptOriginalFilename" = $filename AND "storage" = $storage LIMIT 1`
|
|
|
|
|
|
|
|
return doesExist({ sequelize: this.sequelize, query, bind: { filename, storage } })
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2024-03-15 15:47:18 +01:00
|
|
|
getFileDownloadUrl () {
|
|
|
|
if (!this.keptOriginalFilename) return null
|
|
|
|
|
|
|
|
return WEBSERVER.URL + join(STATIC_DOWNLOAD_PATHS.ORIGINAL_VIDEO_FILE, this.keptOriginalFilename)
|
|
|
|
}
|
|
|
|
|
2023-07-19 16:02:49 +02:00
|
|
|
toFormattedJSON (): VideoSource {
|
2022-06-21 15:31:25 +02:00
|
|
|
return {
|
2024-03-15 15:47:18 +01:00
|
|
|
filename: this.inputFilename,
|
|
|
|
inputFilename: this.inputFilename,
|
|
|
|
fileDownloadUrl: this.getFileDownloadUrl(),
|
|
|
|
|
|
|
|
resolution: {
|
|
|
|
id: this.resolution,
|
2024-03-26 14:05:19 +01:00
|
|
|
label: this.resolution !== null
|
|
|
|
? getResolutionLabel(this.resolution)
|
|
|
|
: null
|
2024-03-15 15:47:18 +01:00
|
|
|
},
|
|
|
|
size: this.size,
|
|
|
|
|
|
|
|
width: this.width,
|
|
|
|
height: this.height,
|
|
|
|
|
|
|
|
fps: this.fps,
|
|
|
|
|
|
|
|
metadata: this.metadata,
|
|
|
|
|
2023-07-19 16:02:49 +02:00
|
|
|
createdAt: this.createdAt.toISOString()
|
2022-06-21 15:31:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|