2024-08-06 15:43:24 +02:00
|
|
|
import { getResolutionLabel } from '@peertube/peertube-core-utils'
|
2024-07-23 16:38:51 +02:00
|
|
|
import { ActivityVideoUrlObject, type FileStorageType, type VideoSource } from '@peertube/peertube-models'
|
|
|
|
import { DOWNLOAD_PATHS, WEBSERVER } from '@server/initializers/constants.js'
|
|
|
|
import { getVideoFileMimeType } from '@server/lib/video-file.js'
|
2024-06-03 16:37:44 +02:00
|
|
|
import { MVideoSource } from '@server/types/models/video/video-source.js'
|
2024-07-23 16:38:51 +02:00
|
|
|
import { extname, 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'
|
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
|
|
|
|
|
2024-07-23 16:38:51 +02:00
|
|
|
return WEBSERVER.URL + join(DOWNLOAD_PATHS.ORIGINAL_VIDEO_FILE, this.keptOriginalFilename)
|
2024-03-15 15:47:18 +01:00
|
|
|
}
|
|
|
|
|
2024-07-23 16:38:51 +02:00
|
|
|
toActivityPubObject (this: MVideoSource): ActivityVideoUrlObject {
|
|
|
|
const mimeType = getVideoFileMimeType(extname(this.inputFilename), false)
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: 'Link',
|
|
|
|
mediaType: mimeType as ActivityVideoUrlObject['mediaType'],
|
|
|
|
href: null,
|
|
|
|
height: this.height || this.resolution,
|
|
|
|
width: this.width,
|
|
|
|
size: this.size,
|
|
|
|
fps: this.fps,
|
|
|
|
attachment: []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toFormattedJSON (this: MVideoSource): VideoSource {
|
2022-06-21 15:31:25 +02:00
|
|
|
return {
|
2024-03-15 15:47:18 +01:00
|
|
|
filename: this.inputFilename,
|
|
|
|
inputFilename: this.inputFilename,
|
2024-06-05 09:01:40 +02:00
|
|
|
|
|
|
|
fileUrl: this.fileUrl,
|
2024-03-15 15:47:18 +01:00
|
|
|
fileDownloadUrl: this.getFileDownloadUrl(),
|
|
|
|
|
|
|
|
resolution: {
|
|
|
|
id: this.resolution,
|
2024-03-26 14:05:19 +01:00
|
|
|
label: this.resolution !== null
|
2024-08-06 15:43:24 +02:00
|
|
|
? getResolutionLabel({ resolution: this.resolution, height: this.height, width: this.width })
|
2024-03-26 14:05:19 +01:00
|
|
|
: 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|