]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+video-studio/edit/video-studio-edit.component.ts
Refactor form reactive
[github/Chocobozzz/PeerTube.git] / client / src / app / +video-studio / edit / video-studio-edit.component.ts
CommitLineData
c729caf6
C
1import { Component, OnInit } from '@angular/core'
2import { ActivatedRoute, Router } from '@angular/router'
3import { ConfirmService, Notifier, ServerService } from '@app/core'
5c5bcea2 4import { FormReactive, FormReactiveService } from '@app/shared/shared-forms'
92e66e04 5import { VideoDetails } from '@app/shared/shared-main'
c729caf6 6import { LoadingBarService } from '@ngx-loading-bar/core'
42b40636 7import { logger } from '@root-helpers/logger'
c729caf6 8import { secondsToTime } from '@shared/core-utils'
92e66e04
C
9import { VideoStudioTask, VideoStudioTaskCut } from '@shared/models'
10import { VideoStudioService } from '../shared'
c729caf6
C
11
12@Component({
92e66e04
C
13 selector: 'my-video-studio-edit',
14 templateUrl: './video-studio-edit.component.html',
15 styleUrls: [ './video-studio-edit.component.scss' ]
c729caf6 16})
92e66e04 17export class VideoStudioEditComponent extends FormReactive implements OnInit {
c729caf6
C
18 isRunningEdition = false
19
20 video: VideoDetails
21
22 constructor (
5c5bcea2 23 protected formReactiveService: FormReactiveService,
c729caf6
C
24 private serverService: ServerService,
25 private notifier: Notifier,
26 private router: Router,
27 private route: ActivatedRoute,
92e66e04 28 private videoStudioService: VideoStudioService,
c729caf6
C
29 private loadingBar: LoadingBarService,
30 private confirmService: ConfirmService
31 ) {
32 super()
33 }
34
35 ngOnInit () {
36 this.video = this.route.snapshot.data.video
37
38 const defaultValues = {
39 cut: {
40 start: 0,
41 end: this.video.duration
42 }
43 }
44
45 this.buildForm({
46 cut: {
47 start: null,
48 end: null
49 },
50 'add-intro': {
51 file: null
52 },
53 'add-outro': {
54 file: null
55 },
56 'add-watermark': {
57 file: null
58 }
59 }, defaultValues)
60 }
61
62 get videoExtensions () {
63 return this.serverService.getHTMLConfig().video.file.extensions
64 }
65
66 get imageExtensions () {
67 return this.serverService.getHTMLConfig().video.image.extensions
68 }
69
70 async runEdition () {
71 if (this.isRunningEdition) return
72
73 const title = $localize`Are you sure you want to edit "${this.video.name}"?`
74 const listHTML = this.getTasksSummary().map(t => `<li>${t}</li>`).join('')
75
76 // eslint-disable-next-line max-len
77 const confirmHTML = $localize`The current video will be overwritten by this edited video and <strong>you won't be able to recover it</strong>.<br /><br />` +
78 $localize`As a reminder, the following tasks will be executed: <ol>${listHTML}</ol>`
79
80 if (await this.confirmService.confirm(confirmHTML, title) !== true) return
81
82 this.isRunningEdition = true
83
84 const tasks = this.buildTasks()
85
86 this.loadingBar.useRef().start()
87
92e66e04 88 return this.videoStudioService.editVideo(this.video.uuid, tasks)
c729caf6
C
89 .subscribe({
90 next: () => {
92e66e04
C
91 this.notifier.success($localize`Edition tasks created.`)
92
93 // Don't redirect to old video version watch page that could be confusing for users
94 this.router.navigateByUrl('/my-library/videos')
c729caf6
C
95 },
96
97 error: err => {
98 this.loadingBar.useRef().complete()
99 this.isRunningEdition = false
100 this.notifier.error(err.message)
42b40636 101 logger.error(err)
c729caf6
C
102 }
103 })
104 }
105
106 getIntroOutroTooltip () {
107 return $localize`(extensions: ${this.videoExtensions.join(', ')})`
108 }
109
110 getWatermarkTooltip () {
111 return $localize`(extensions: ${this.imageExtensions.join(', ')})`
112 }
113
114 noEdition () {
115 return this.buildTasks().length === 0
116 }
117
118 getTasksSummary () {
119 const tasks = this.buildTasks()
120
121 return tasks.map(t => {
122 if (t.name === 'add-intro') {
052bdb7c 123 return $localize`"${this.getFilename(t.options.file)}" will be added at the beginning of the video`
c729caf6
C
124 }
125
126 if (t.name === 'add-outro') {
127 return $localize`"${this.getFilename(t.options.file)}" will be added at the end of the video`
128 }
129
130 if (t.name === 'add-watermark') {
131 return $localize`"${this.getFilename(t.options.file)}" image watermark will be added to the video`
132 }
133
134 if (t.name === 'cut') {
135 const { start, end } = t.options
136
137 if (start !== undefined && end !== undefined) {
138 return $localize`Video will begin at ${secondsToTime(start)} and stop at ${secondsToTime(end)}`
139 }
140
141 if (start !== undefined) {
142 return $localize`Video will begin at ${secondsToTime(start)}`
143 }
144
145 if (end !== undefined) {
146 return $localize`Video will stop at ${secondsToTime(end)}`
147 }
148 }
149
150 return ''
151 })
152 }
153
154 private getFilename (obj: any) {
155 return obj.name
156 }
157
158 private buildTasks () {
92e66e04 159 const tasks: VideoStudioTask[] = []
c729caf6
C
160 const value = this.form.value
161
162 const cut = value['cut']
163 if (cut['start'] !== 0 || cut['end'] !== this.video.duration) {
164
92e66e04 165 const options: VideoStudioTaskCut['options'] = {}
c729caf6
C
166 if (cut['start'] !== 0) options.start = cut['start']
167 if (cut['end'] !== this.video.duration) options.end = cut['end']
168
169 tasks.push({
170 name: 'cut',
171 options
172 })
173 }
174
175 if (value['add-intro']?.['file']) {
176 tasks.push({
177 name: 'add-intro',
178 options: {
179 file: value['add-intro']['file']
180 }
181 })
182 }
183
184 if (value['add-outro']?.['file']) {
185 tasks.push({
186 name: 'add-outro',
187 options: {
188 file: value['add-outro']['file']
189 }
190 })
191 }
192
193 if (value['add-watermark']?.['file']) {
194 tasks.push({
195 name: 'add-watermark',
196 options: {
197 file: value['add-watermark']['file']
198 }
199 })
200 }
201
202 return tasks
203 }
204
205}