]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+videos/+video-edit/video-add-components/video-import-url.component.ts
Merge remote-tracking branch 'weblate/develop' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-edit / video-add-components / video-import-url.component.ts
1 import { forkJoin } from 'rxjs'
2 import { map, switchMap } from 'rxjs/operators'
3 import { AfterViewInit, Component, EventEmitter, OnInit, Output } from '@angular/core'
4 import { Router } from '@angular/router'
5 import { AuthService, CanComponentDeactivate, HooksService, Notifier, ServerService } from '@app/core'
6 import { scrollToTop } from '@app/helpers'
7 import { FormValidatorService } from '@app/shared/shared-forms'
8 import { VideoCaptionService, VideoEdit, VideoImportService, VideoService } from '@app/shared/shared-main'
9 import { LoadingBarService } from '@ngx-loading-bar/core'
10 import { logger } from '@root-helpers/logger'
11 import { VideoUpdate } from '@shared/models'
12 import { hydrateFormFromVideo } from '../shared/video-edit-utils'
13 import { VideoSend } from './video-send'
14
15 @Component({
16 selector: 'my-video-import-url',
17 templateUrl: './video-import-url.component.html',
18 styleUrls: [
19 '../shared/video-edit.component.scss',
20 './video-send.scss'
21 ]
22 })
23 export class VideoImportUrlComponent extends VideoSend implements OnInit, AfterViewInit, CanComponentDeactivate {
24 @Output() firstStepDone = new EventEmitter<string>()
25 @Output() firstStepError = new EventEmitter<void>()
26
27 targetUrl = ''
28
29 isImportingVideo = false
30 hasImportedVideo = false
31 isUpdatingVideo = false
32
33 video: VideoEdit
34 error: string
35
36 constructor (
37 protected formValidatorService: FormValidatorService,
38 protected loadingBar: LoadingBarService,
39 protected notifier: Notifier,
40 protected authService: AuthService,
41 protected serverService: ServerService,
42 protected videoService: VideoService,
43 protected videoCaptionService: VideoCaptionService,
44 private router: Router,
45 private videoImportService: VideoImportService,
46 private hooks: HooksService
47 ) {
48 super()
49 }
50
51 ngOnInit () {
52 super.ngOnInit()
53 }
54
55 ngAfterViewInit () {
56 this.hooks.runAction('action:video-url-import.init', 'video-edit')
57 }
58
59 canDeactivate () {
60 return { canDeactivate: true }
61 }
62
63 isTargetUrlValid () {
64 return this.targetUrl?.match(/https?:\/\//)
65 }
66
67 importVideo () {
68 this.isImportingVideo = true
69
70 const videoUpdate: VideoUpdate = {
71 privacy: this.highestPrivacy,
72 waitTranscoding: false,
73 channelId: this.firstStepChannelId
74 }
75
76 this.loadingBar.useRef().start()
77
78 this.videoImportService
79 .importVideoUrl(this.targetUrl, videoUpdate)
80 .pipe(
81 switchMap(previous => {
82 return forkJoin([
83 this.videoCaptionService.listCaptions(previous.video.uuid),
84 this.videoService.getVideo({ videoId: previous.video.uuid })
85 ]).pipe(map(([ videoCaptionsResult, video ]) => ({ videoCaptions: videoCaptionsResult.data, video })))
86 })
87 )
88 .subscribe({
89 next: ({ video, videoCaptions }) => {
90 this.loadingBar.useRef().complete()
91 this.firstStepDone.emit(video.name)
92 this.isImportingVideo = false
93 this.hasImportedVideo = true
94
95 this.video = new VideoEdit(video)
96 this.video.patch({ privacy: this.firstStepPrivacyId })
97
98 this.videoCaptions = videoCaptions
99
100 hydrateFormFromVideo(this.form, this.video, true)
101 },
102
103 error: err => {
104 this.loadingBar.useRef().complete()
105 this.isImportingVideo = false
106 this.firstStepError.emit()
107 this.notifier.error(err.message)
108 }
109 })
110 }
111
112 async updateSecondStep () {
113 if (!await this.isFormValid()) return
114
115 this.video.patch(this.form.value)
116
117 this.isUpdatingVideo = true
118
119 // Update the video
120 this.updateVideoAndCaptions(this.video)
121 .subscribe({
122 next: () => {
123 this.isUpdatingVideo = false
124 this.notifier.success($localize`Video to import updated.`)
125
126 this.router.navigate([ '/my-library', 'video-imports' ])
127 },
128
129 error: err => {
130 this.error = err.message
131 scrollToTop()
132 logger.error(err)
133 }
134 })
135 }
136 }