2024-03-15 09:47:18 -05:00
|
|
|
import type { FileStorageType, VideoSource } from '@peertube/peertube-models'
|
|
|
|
import { STATIC_DOWNLOAD_PATHS, WEBSERVER } from '@server/initializers/constants.js'
|
|
|
|
import { join } from 'path'
|
2023-07-19 09:02:49 -05:00
|
|
|
import { Transaction } from 'sequelize'
|
2024-03-15 09:47:18 -05:00
|
|
|
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Table, UpdatedAt } from 'sequelize-typescript'
|
2024-02-22 03:12:04 -06:00
|
|
|
import { SequelizeModel, getSort } from '../shared/index.js'
|
2024-03-15 09:47:18 -05:00
|
|
|
import { getResolutionLabel } from './formatter/video-api-format.js'
|
2023-07-31 07:34:36 -05:00
|
|
|
import { VideoModel } from './video.js'
|
2024-03-15 09:47:18 -05:00
|
|
|
import { MVideoSource } from '@server/types/models/video/video-source.js'
|
2022-06-21 08:31:25 -05:00
|
|
|
|
|
|
|
@Table({
|
|
|
|
tableName: 'videoSource',
|
|
|
|
indexes: [
|
|
|
|
{
|
2023-07-19 09:02:49 -05:00
|
|
|
fields: [ 'videoId' ]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ { name: 'createdAt', order: 'DESC' } ]
|
2024-03-15 09:47:18 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'keptOriginalFilename' ],
|
|
|
|
unique: true
|
2022-06-21 08:31:25 -05:00
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
2024-02-22 03:12:04 -06:00
|
|
|
export class VideoSourceModel extends SequelizeModel<VideoSourceModel> {
|
2022-06-21 08:31:25 -05:00
|
|
|
@CreatedAt
|
|
|
|
createdAt: Date
|
|
|
|
|
|
|
|
@UpdatedAt
|
|
|
|
updatedAt: Date
|
|
|
|
|
|
|
|
@AllowNull(false)
|
|
|
|
@Column
|
2024-03-15 09:47:18 -05: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 08:31:25 -05:00
|
|
|
|
|
|
|
@ForeignKey(() => VideoModel)
|
|
|
|
@Column
|
|
|
|
videoId: number
|
|
|
|
|
2023-07-19 09:02:49 -05:00
|
|
|
@BelongsTo(() => VideoModel, {
|
|
|
|
foreignKey: {
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
2023-07-31 07:34:36 -05:00
|
|
|
Video: Awaited<VideoModel>
|
2022-06-21 08:31:25 -05:00
|
|
|
|
2023-07-19 09:02:49 -05:00
|
|
|
static loadLatest (videoId: number, transaction?: Transaction) {
|
2024-03-15 09:47:18 -05:00
|
|
|
return VideoSourceModel.findOne<MVideoSource>({
|
2023-07-19 09:02:49 -05:00
|
|
|
where: { videoId },
|
|
|
|
order: getSort('-createdAt'),
|
|
|
|
transaction
|
|
|
|
})
|
2022-06-21 08:31:25 -05:00
|
|
|
}
|
|
|
|
|
2024-03-15 09:47:18 -05:00
|
|
|
static loadByKeptOriginalFilename (keptOriginalFilename: string) {
|
|
|
|
return VideoSourceModel.findOne<MVideoSource>({
|
|
|
|
where: { keptOriginalFilename }
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
static listAll (videoId: number, transaction?: Transaction) {
|
|
|
|
return VideoSourceModel.findAll<MVideoSource>({
|
|
|
|
where: { videoId },
|
|
|
|
transaction
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
getFileDownloadUrl () {
|
|
|
|
if (!this.keptOriginalFilename) return null
|
|
|
|
|
|
|
|
return WEBSERVER.URL + join(STATIC_DOWNLOAD_PATHS.ORIGINAL_VIDEO_FILE, this.keptOriginalFilename)
|
|
|
|
}
|
|
|
|
|
2023-07-19 09:02:49 -05:00
|
|
|
toFormattedJSON (): VideoSource {
|
2022-06-21 08:31:25 -05:00
|
|
|
return {
|
2024-03-15 09:47:18 -05:00
|
|
|
filename: this.inputFilename,
|
|
|
|
inputFilename: this.inputFilename,
|
|
|
|
fileDownloadUrl: this.getFileDownloadUrl(),
|
|
|
|
|
|
|
|
resolution: {
|
|
|
|
id: this.resolution,
|
2024-03-26 08:05:19 -05:00
|
|
|
label: this.resolution !== null
|
|
|
|
? getResolutionLabel(this.resolution)
|
|
|
|
: null
|
2024-03-15 09:47:18 -05:00
|
|
|
},
|
|
|
|
size: this.size,
|
|
|
|
|
|
|
|
width: this.width,
|
|
|
|
height: this.height,
|
|
|
|
|
|
|
|
fps: this.fps,
|
|
|
|
|
|
|
|
metadata: this.metadata,
|
|
|
|
|
2023-07-19 09:02:49 -05:00
|
|
|
createdAt: this.createdAt.toISOString()
|
2022-06-21 08:31:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|