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 { VideoUpdate } from '@shared/models'
11 import { hydrateFormFromVideo } from '../shared/video-edit-utils'
12 import { VideoSend } from './video-send'
15 selector: 'my-video-import-url',
16 templateUrl: './video-import-url.component.html',
18 '../shared/video-edit.component.scss',
22 export class VideoImportUrlComponent extends VideoSend implements OnInit, AfterViewInit, CanComponentDeactivate {
23 @Output() firstStepDone = new EventEmitter<string>()
24 @Output() firstStepError = new EventEmitter<void>()
28 isImportingVideo = false
29 hasImportedVideo = false
30 isUpdatingVideo = false
36 protected formValidatorService: FormValidatorService,
37 protected loadingBar: LoadingBarService,
38 protected notifier: Notifier,
39 protected authService: AuthService,
40 protected serverService: ServerService,
41 protected videoService: VideoService,
42 protected videoCaptionService: VideoCaptionService,
43 private router: Router,
44 private videoImportService: VideoImportService,
45 private hooks: HooksService
55 this.hooks.runAction('action:video-url-import.init', 'video-edit')
59 return { canDeactivate: true }
63 return this.targetUrl?.match(/https?:\/\//)
67 this.isImportingVideo = true
69 const videoUpdate: VideoUpdate = {
70 privacy: this.highestPrivacy,
71 waitTranscoding: false,
72 channelId: this.firstStepChannelId
75 this.loadingBar.useRef().start()
77 this.videoImportService
78 .importVideoUrl(this.targetUrl, videoUpdate)
80 switchMap(previous => {
82 this.videoCaptionService.listCaptions(previous.video.uuid),
83 this.videoService.getVideo({ videoId: previous.video.uuid })
84 ]).pipe(map(([ videoCaptionsResult, video ]) => ({ videoCaptions: videoCaptionsResult.data, video })))
88 next: ({ video, videoCaptions }) => {
89 this.loadingBar.useRef().complete()
90 this.firstStepDone.emit(video.name)
91 this.isImportingVideo = false
92 this.hasImportedVideo = true
94 this.video = new VideoEdit(video)
95 this.video.patch({ privacy: this.firstStepPrivacyId })
97 this.videoCaptions = videoCaptions
99 hydrateFormFromVideo(this.form, this.video, true)
103 this.loadingBar.useRef().complete()
104 this.isImportingVideo = false
105 this.firstStepError.emit()
106 this.notifier.error(err.message)
111 async updateSecondStep () {
112 if (!await this.isFormValid()) return
114 this.video.patch(this.form.value)
116 this.isUpdatingVideo = true
119 this.updateVideoAndCaptions(this.video)
122 this.isUpdatingVideo = false
123 this.notifier.success($localize`Video to import updated.`)
125 this.router.navigate([ '/my-library', 'video-imports' ])
129 this.error = err.message