1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
import { remove } from 'fs-extra'
import { join } from 'path'
import { AfterDestroy, AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
import { CONFIG } from '@server/initializers/config'
import { MStoryboard, MStoryboardVideo, MVideo } from '@server/types/models'
import { Storyboard } from '@shared/models'
import { AttributesOnly } from '@shared/typescript-utils'
import { logger } from '../../helpers/logger'
import { CONSTRAINTS_FIELDS, LAZY_STATIC_PATHS, WEBSERVER } from '../../initializers/constants'
import { VideoModel } from './video'
import { Transaction } from 'sequelize'
@Table({
tableName: 'storyboard',
indexes: [
{
fields: [ 'videoId' ],
unique: true
},
{
fields: [ 'filename' ],
unique: true
}
]
})
export class StoryboardModel extends Model<Partial<AttributesOnly<StoryboardModel>>> {
@AllowNull(false)
@Column
filename: string
@AllowNull(false)
@Column
totalHeight: number
@AllowNull(false)
@Column
totalWidth: number
@AllowNull(false)
@Column
spriteHeight: number
@AllowNull(false)
@Column
spriteWidth: number
@AllowNull(false)
@Column
spriteDuration: number
@AllowNull(true)
@Column(DataType.STRING(CONSTRAINTS_FIELDS.COMMONS.URL.max))
fileUrl: string
@ForeignKey(() => VideoModel)
@Column
videoId: number
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: true
},
onDelete: 'CASCADE'
})
Video: VideoModel
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@AfterDestroy
static removeInstanceFile (instance: StoryboardModel) {
logger.info('Removing storyboard file %s.', instance.filename)
// Don't block the transaction
instance.removeFile()
.catch(err => logger.error('Cannot remove storyboard file %s.', instance.filename, { err }))
}
static loadByVideo (videoId: number, transaction?: Transaction): Promise<MStoryboard> {
const query = {
where: {
videoId
},
transaction
}
return StoryboardModel.findOne(query)
}
static loadByFilename (filename: string): Promise<MStoryboard> {
const query = {
where: {
filename
}
}
return StoryboardModel.findOne(query)
}
static loadWithVideoByFilename (filename: string): Promise<MStoryboardVideo> {
const query = {
where: {
filename
},
include: [
{
model: VideoModel.unscoped(),
required: true
}
]
}
return StoryboardModel.findOne(query)
}
// ---------------------------------------------------------------------------
static async listStoryboardsOf (video: MVideo): Promise<MStoryboardVideo[]> {
const query = {
where: {
videoId: video.id
}
}
const storyboards = await StoryboardModel.findAll<MStoryboard>(query)
return storyboards.map(s => Object.assign(s, { Video: video }))
}
// ---------------------------------------------------------------------------
getOriginFileUrl (video: MVideo) {
if (video.isOwned()) {
return WEBSERVER.URL + this.getLocalStaticPath()
}
return this.fileUrl
}
getLocalStaticPath () {
return LAZY_STATIC_PATHS.STORYBOARDS + this.filename
}
getPath () {
return join(CONFIG.STORAGE.STORYBOARDS_DIR, this.filename)
}
removeFile () {
return remove(this.getPath())
}
toFormattedJSON (this: MStoryboardVideo): Storyboard {
return {
storyboardPath: this.getLocalStaticPath(),
totalHeight: this.totalHeight,
totalWidth: this.totalWidth,
spriteWidth: this.spriteWidth,
spriteHeight: this.spriteHeight,
spriteDuration: this.spriteDuration
}
}
}
|