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?:\/\//)
67 isChannelSyncEnabled () {
68 return this.serverConfig.import.videoChannelSynchronization.enabled
72 this.isImportingVideo = true
74 const videoUpdate: VideoUpdate = {
75 privacy: this.highestPrivacy,
76 waitTranscoding: false,
77 channelId: this.firstStepChannelId
80 this.loadingBar.useRef().start()
82 this.videoImportService
83 .importVideoUrl(this.targetUrl, videoUpdate)
85 switchMap(previous => {
87 this.videoCaptionService.listCaptions(previous.video.uuid),
88 this.videoService.getVideo({ videoId: previous.video.uuid })
89 ]).pipe(map(([ videoCaptionsResult, video ]) => ({ videoCaptions: videoCaptionsResult.data, video })))
93 next: ({ video, videoCaptions }) => {
94 this.loadingBar.useRef().complete()
95 this.firstStepDone.emit(video.name)
96 this.isImportingVideo = false
97 this.hasImportedVideo = true
99 this.video = new VideoEdit(video)
100 this.video.patch({ privacy: this.firstStepPrivacyId })
102 this.videoCaptions = videoCaptions
104 hydrateFormFromVideo(this.form, this.video, true)
108 this.loadingBar.useRef().complete()
109 this.isImportingVideo = false
110 this.firstStepError.emit()
111 this.notifier.error(err.message)
116 async updateSecondStep () {
117 if (!await this.isFormValid()) return
119 this.video.patch(this.form.value)
121 this.isUpdatingVideo = true
124 this.updateVideoAndCaptions(this.video)
127 this.isUpdatingVideo = false
128 this.notifier.success($localize`Video to import updated.`)
130 this.router.navigate([ '/my-library', 'video-imports' ])
134 this.error = err.message