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'
16 selector: 'my-video-import-url',
17 templateUrl: './video-import-url.component.html',
19 '../shared/video-edit.component.scss',
23 export class VideoImportUrlComponent extends VideoSend implements OnInit, AfterViewInit, CanComponentDeactivate {
24 @Output() firstStepDone = new EventEmitter<string>()
25 @Output() firstStepError = new EventEmitter<void>()
29 isImportingVideo = false
30 hasImportedVideo = false
31 isUpdatingVideo = false
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
56 this.hooks.runAction('action:video-url-import.init', 'video-edit')
60 return { canDeactivate: true }
64 return this.targetUrl?.match(/https?:\/\//)
68 this.isImportingVideo = true
70 const videoUpdate: VideoUpdate = {
71 privacy: this.highestPrivacy,
72 waitTranscoding: false,
73 channelId: this.firstStepChannelId
76 this.loadingBar.useRef().start()
78 this.videoImportService
79 .importVideoUrl(this.targetUrl, videoUpdate)
81 switchMap(previous => {
83 this.videoCaptionService.listCaptions(previous.video.uuid),
84 this.videoService.getVideo({ videoId: previous.video.uuid })
85 ]).pipe(map(([ videoCaptionsResult, video ]) => ({ videoCaptions: videoCaptionsResult.data, video })))
89 next: ({ video, videoCaptions }) => {
90 this.loadingBar.useRef().complete()
91 this.firstStepDone.emit(video.name)
92 this.isImportingVideo = false
93 this.hasImportedVideo = true
95 this.video = new VideoEdit(video)
96 this.video.patch({ privacy: this.firstStepPrivacyId })
98 this.videoCaptions = videoCaptions
100 hydrateFormFromVideo(this.form, this.video, true)
104 this.loadingBar.useRef().complete()
105 this.isImportingVideo = false
106 this.firstStepError.emit()
107 this.notifier.error(err.message)
112 async updateSecondStep () {
113 if (!await this.isFormValid()) return
115 this.video.patch(this.form.value)
117 this.isUpdatingVideo = true
120 this.updateVideoAndCaptions(this.video)
123 this.isUpdatingVideo = false
124 this.notifier.success($localize`Video to import updated.`)
126 this.router.navigate([ '/my-library', 'video-imports' ])
130 this.error = err.message